-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
171 lines (145 loc) · 4.19 KB
/
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package client
import (
"os"
"path/filepath"
istioVersioned "istio.io/client-go/pkg/clientset/versioned"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/klog/v2"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
// get istio client
func GetIstioClient() *istioVersioned.Clientset {
return istioVersioned.NewForConfigOrDie(GetRestConfig())
}
// get runtime client
func GetRuntimeClient(r *runtime.Scheme) runtimeclient.Client {
resConfig := GetRestConfig()
// creates the clientset, default behavor
client, err := runtimeclient.New(resConfig, runtimeclient.Options{
Scheme: r,
})
if err != nil {
panic(err.Error())
}
return client
}
// get dynamic client with Context
func GetDynamicClientWithContext(contextName string) *dynamic.DynamicClient {
var resConfig *rest.Config
var err error
kubeconfig := GetKubeConfig()
if fileIsExist(kubeconfig) {
resConfig, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if contextName != "" {
configLoadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: contextName}
resConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(configLoadingRules, configOverrides).ClientConfig()
if err != nil {
klog.Errorf("Switch kubeconfig context err: %v", err)
}
}
if err != nil {
panic(err.Error())
}
}
// creates the clientset, default behavor
dynaClient, err := dynamic.NewForConfig(resConfig)
if err != nil {
panic(err.Error())
}
return dynaClient
}
// get dyn client for use dynamic DynamicClient
func GetDynamicClient() *dynamic.DynamicClient {
resConfig := GetRestConfig()
// creates the clientset, default behavor
dynClient, err := dynamic.NewForConfig(resConfig)
if err != nil {
panic(err.Error())
}
return dynClient
}
// k8s api client with set-context = contextName
// configPath equal merged kubeconfigs, example dev, prod, test
func GetClientWithContext(contextName string, configPath string) *kubernetes.Clientset {
var resConfig *rest.Config
var err error
kubeconfig := GetKubeConfig(configPath)
if fileIsExist(kubeconfig) {
resConfig, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if contextName != "" {
configLoadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: contextName}
resConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(configLoadingRules, configOverrides).ClientConfig()
if err != nil {
klog.Errorf("Switch kubeconfig context err: %v", err)
}
}
if err != nil {
panic(err.Error())
}
}
// creates the clientset, default behavor
clientset, err := kubernetes.NewForConfig(resConfig)
if err != nil {
panic(err.Error())
}
return clientset
}
// k8s api client
func GetClient() *kubernetes.Clientset {
resConfig := GetRestConfig()
// creates the clientset, default behavor
clientSet, err := kubernetes.NewForConfig(resConfig)
if err != nil {
panic(err.Error())
}
return clientSet
}
// if configPath existed, use it first
// or use ENV KUBECONFIG
func GetKubeConfig(configPath ...string) string {
var kubeconfig string
homeDir := homedir.HomeDir()
ENV_KUBUCONFIG := os.Getenv("KUBECONFIG")
switch {
case len(configPath) == 1:
kubeconfig = configPath[0]
case len(ENV_KUBUCONFIG) > 0:
kubeconfig = filepath.Join(ENV_KUBUCONFIG)
case len(homeDir) > 0:
kubeconfig = filepath.Join(homeDir, ".kube", "config")
default:
kubeconfig = ""
}
return kubeconfig
}
// get rest config, if you want use for other resources
// Example: istioClient
func GetRestConfig() *rest.Config {
kubeconfig := GetKubeConfig()
var resConfig *rest.Config
var err error
if fileIsExist(kubeconfig) {
resConfig, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err)
}
} else {
// creates the in-cluster config
resConfig, err = rest.InClusterConfig()
if err != nil {
panic(err)
}
}
return resConfig
}
func fileIsExist(path string) bool {
_, err := os.Lstat(path)
return !os.IsNotExist(err)
}