-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8s.go
62 lines (56 loc) · 1.66 KB
/
k8s.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
package utils
import (
"os"
"path/filepath"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func GetDynamicClient() (dynamic.Interface, error) {
config, err := rest.InClusterConfig()
if err != nil {
// If not running in cluster, use kubeconfig from C:\Users\<user>\.kube
homeDir, _ := os.UserHomeDir()
kubeconfig := filepath.Join(homeDir, ".kube", "config")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
}
return dynamic.NewForConfig(config)
}
func GetKubernetesClientset() (*kubernetes.Clientset, error) {
// if running in cluster, use in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
// if not running in cluster, use kubeconfig
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" {
kubeconfig = os.Getenv("HOME") + "/.kube/config"
}
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return clientset, nil
}
func GetApiextensionsClientset() (*apiextensionsclientset.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
// If not running in cluster, use kubeconfig from C:\Users\<user>\.kube
homeDir, _ := os.UserHomeDir()
kubeconfig := filepath.Join(homeDir, ".kube", "config")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
}
return apiextensionsclientset.NewForConfig(config)
}