/
kubeClient.go
73 lines (60 loc) · 1.93 KB
/
kubeClient.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
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Contributors to the Open Cluster Management project
package config
import (
"os"
"path/filepath"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)
// If env KUBECONFIG is defined, use it. Otherise use default location ~/.kube/config
// NOTE: This may need to be enhanced to support development on different OS.
func getKubeConfigPath() string {
defaultKubePath := filepath.Join(os.Getenv("HOME"), ".kube", "config")
if _, err := os.Stat(defaultKubePath); os.IsNotExist(err) {
// set default to empty string if path does not reslove
defaultKubePath = ""
}
kubeConfig := getEnv("KUBECONFIG", defaultKubePath)
return kubeConfig
}
func getKubeConfig() *rest.Config {
kubeConfigPath := getKubeConfigPath()
var clientConfig *rest.Config
var clientConfigError error
if kubeConfigPath != "" {
klog.Infof("Creating k8s client using KubeConfig at: %s", kubeConfigPath)
clientConfig, clientConfigError = clientcmd.BuildConfigFromFlags("", kubeConfigPath)
} else {
klog.V(2).Info("Creating k8s client using InClusterClientConfig")
clientConfig, clientConfigError = rest.InClusterConfig()
}
if clientConfigError != nil {
klog.Fatal("Error getting Kube Config: ", clientConfigError)
}
return clientConfig
}
func getKubeClient() *kubernetes.Clientset {
config := getKubeConfig()
var kubeClient *kubernetes.Clientset
var err error
if config != nil {
kubeClient, err = kubernetes.NewForConfig(config)
if err != nil {
klog.Fatal("Cannot Construct Kube Client from Config: ", err)
}
} else {
klog.Error("Cannot Construct Kube Client as input Config is nil")
}
return kubeClient
}
// Get the kubernetes dynamic client.
func GetDynamicClient() dynamic.Interface {
newDynamicClient, err := dynamic.NewForConfig(getKubeConfig())
if err != nil {
klog.Fatal("Cannot Construct Dynamic Client ", err)
}
return newDynamicClient
}