/
k8s_client.go
67 lines (62 loc) · 1.83 KB
/
k8s_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
61
62
63
64
65
66
67
package utilities
import (
"flag"
entandoclients "github.com/antromeo/entando-clients/pkg/client/clientset/versioned"
pipelineclients "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
"sync"
)
type KubeClient struct {
ClientSet *kubernetes.Clientset
EntandoClientSet *entandoclients.Clientset
PipelineClientSet *pipelineclients.Clientset
DynamicClient *dynamic.DynamicClient
Namespace string
}
var kubeClient *KubeClient
var onceK8s sync.Once
func GetKubeClientInstance() *KubeClient {
onceK8s.Do(func() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
panic(err)
}
entandoclientset, err := entandoclients.NewForConfig(config)
if err != nil {
panic(err)
}
pipelineclients, err := pipelineclients.NewForConfig(config)
if err != nil {
panic(err)
}
clientCfg, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
namespace := clientCfg.Contexts[clientCfg.CurrentContext].Namespace
kubeClient = &KubeClient{
ClientSet: clientset,
EntandoClientSet: entandoclientset,
PipelineClientSet: pipelineclients,
DynamicClient: dynamicClient,
Namespace: namespace,
}
})
return kubeClient
}