/
kube.go
56 lines (45 loc) · 1.37 KB
/
kube.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
package kube
import (
"fmt"
"path/filepath"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"
)
const (
kubeconfigDir = ".kube"
kubeconfigFilename = "config"
)
// Create a Configclient
func createConfigClient(kubeconfigPath string) clientcmd.ClientConfig {
if kubeconfigPath == "" {
kubeconfigPath = filepath.Join(homedir.HomeDir(), kubeconfigDir, kubeconfigFilename)
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ConfigOverrides{},
)
}
// Create a Rawconfig
func GetRawAPIConfig(kubeconfigPath string) (*api.Config, error) {
configclient := createConfigClient(kubeconfigPath)
rawConfig, err := configclient.RawConfig()
if err != nil {
return nil, fmt.Errorf("unable to get kube rawconfig: %w", err)
}
return &rawConfig, nil
}
// Create a new Clientset
func GetClientSet(kubeconfigPath string) (*kubernetes.Clientset, error) {
configclient := createConfigClient(kubeconfigPath)
clientcfg, err := configclient.ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not create clientconfig: %w", err)
}
clientset, err := kubernetes.NewForConfig(clientcfg)
if err != nil {
return nil, fmt.Errorf("could not create clientset: %w", err)
}
return clientset, nil
}