This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkubeclient.go
122 lines (108 loc) · 3.17 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package testutils
import (
"fmt"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
// get auth clients for gcp
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
// KubeConfigPath ...
var KubeConfigPath string
// Restconfig ...
var Restconfig *rest.Config
// KubeClient ...
var KubeClient *kubernetes.Clientset
var topErr = SetUpClusterClients()
// SetUpClusterClients ...
func SetUpClusterClients() error {
var err error
KubeConfigPath, err = getGlobalKubeConfigPath()
if err != nil {
log.Fatalf("%+v", err)
return fmt.Errorf("%+v", err)
}
Restconfig, err = GetKubeConfig(KubeConfigPath, false)
if err != nil {
log.Fatalf("%+v", err)
return fmt.Errorf("%+v", err)
}
KubeClient, err = getKubeClient(Restconfig)
if err != nil {
log.Fatalf("%+v", err)
return fmt.Errorf("%+v", err)
}
return nil
}
// getKubeClient gets the kubernetes client
func getKubeClient(kubeConfig *rest.Config) (*kubernetes.Clientset, error) {
client, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return nil, err
}
log.Infof("Set Kube Client with Rest Config")
return client, nil
}
func getGlobalKubeConfigPath() (string, error) {
var path string
if kubeconfigEnvVal, exists := os.LookupEnv("KUBECONFIG"); exists { // set kubeconfig if environ is set
path = kubeconfigEnvVal
}
// if the path was set, verify that the file exists
if path != "" {
if _, err := os.Stat(path); os.IsNotExist(err) {
return "", fmt.Errorf("the kubeconfig path '%s' does not point to a file", path)
}
}
log.Infof("Kube Config Path: '%+v'", path)
return path, nil
}
// GetKubeConfig returns a kubeconfig for inside or outside the cluster
func GetKubeConfig(kubeconfigpath string, insecureSkipTLSVerify bool) (*rest.Config, error) {
var err error
var kubeConfig *rest.Config
// creates the in-cluster config
kubeConfig, err = rest.InClusterConfig()
if err != nil {
kubeConfig, err = GetKubeClientFromOutsideCluster(kubeconfigpath, insecureSkipTLSVerify)
if err != nil {
return nil, err
}
log.Infof("Getting Rest Config for Outside Cluster")
return kubeConfig, nil
}
log.Infof("Getting Rest Config for Inside Cluster")
return kubeConfig, nil
}
// GetKubeClientFromOutsideCluster returns the rest config of outside cluster
func GetKubeClientFromOutsideCluster(kubeconfigpath string, insecureSkipTLSVerify bool) (*rest.Config, error) {
// Determine Config Paths
if home := homeDir(); len(kubeconfigpath) == 0 && home != "" {
kubeconfigpath = filepath.Join(home, ".kube", "config")
}
kubeConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{
ExplicitPath: kubeconfigpath,
},
&clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: "",
InsecureSkipTLSVerify: insecureSkipTLSVerify,
},
}).ClientConfig()
if err != nil {
return nil, err
}
return kubeConfig, nil
}
// homeDir determines the user's home directory path
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}