-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
60 lines (54 loc) · 1.59 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package client
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
shim "github.com/kudobuilder/shim/shim-controller/pkg/generated/clientset/versioned"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// Client provides access different K8S clients
type Client struct {
KubeClient kubernetes.Interface
Discovery discovery.DiscoveryInterface
Shim *shim.Clientset
}
func buildKubeConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
client, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("error creating kubernetes client from %s: %v", kubeconfig, err)
}
return client, err
}
log.Infof("kubeconfig file: using InClusterConfig.")
return rest.InClusterConfig()
}
func GetKubeConfig() (*rest.Config, error) {
kubeConfigPath := os.Getenv("KUBECONFIG")
return buildKubeConfig(kubeConfigPath)
}
func GetKubeClient() (*Client, error) {
config, err := GetKubeConfig()
if err != nil {
return nil, fmt.Errorf("failed to get kube config: %v", err)
}
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("could not get Kubernetes client: %s", err)
}
shim, err := shim.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("could not get Kubernetes client: %s", err)
}
discovery, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, fmt.Errorf("could not get Kubernetes client: %s", err)
}
return &Client{client, discovery, shim}, nil
}