-
Notifications
You must be signed in to change notification settings - Fork 48
/
kubecli.go
457 lines (392 loc) · 13.7 KB
/
kubecli.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Red Hat, Inc.
* Copyright 2018 The Kubernetes Authors.
*
*/
package kubecli
import (
"flag"
"os"
"sync"
routev1 "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
clonev1alpha1 "kubevirt.io/client-go/generated/kubevirt/clientset/versioned/typed/clone/v1alpha1"
secv1 "github.com/openshift/client-go/security/clientset/versioned/typed/security/v1"
"github.com/spf13/pflag"
extclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"kubevirt.io/api/core"
v1 "kubevirt.io/api/core/v1"
cdiclient "kubevirt.io/client-go/generated/containerized-data-importer/clientset/versioned"
k8ssnapshotclient "kubevirt.io/client-go/generated/external-snapshotter/clientset/versioned"
generatedclient "kubevirt.io/client-go/generated/kubevirt/clientset/versioned"
migrationsv1 "kubevirt.io/client-go/generated/kubevirt/clientset/versioned/typed/migrations/v1alpha1"
networkclient "kubevirt.io/client-go/generated/network-attachment-definition-client/clientset/versioned"
promclient "kubevirt.io/client-go/generated/prometheus-operator/clientset/versioned"
)
var (
kubeconfig string
master string
)
var (
SchemeBuilder runtime.SchemeBuilder
Scheme *runtime.Scheme
Codecs serializer.CodecFactory
ParameterCodec runtime.ParameterCodec
)
func init() {
// This allows consumers of the KubeVirt client go package to
// customize what version the client uses. Without specifying a
// version, all versions are registered. While this techincally
// file to register all versions, so k8s ecosystem libraries
// do not work well with this. By explicitly setting the env var,
// consumers of our client go can avoid these scenarios by only
// registering a single version
registerVersion := os.Getenv(v1.KubeVirtClientGoSchemeRegistrationVersionEnvVar)
if registerVersion != "" {
SchemeBuilder = runtime.NewSchemeBuilder(v1.AddKnownTypesGenerator([]schema.GroupVersion{schema.GroupVersion{Group: core.GroupName, Version: registerVersion}}))
} else {
SchemeBuilder = runtime.NewSchemeBuilder(v1.AddKnownTypesGenerator(v1.GroupVersions))
}
Scheme = runtime.NewScheme()
AddToScheme := SchemeBuilder.AddToScheme
Codecs = serializer.NewCodecFactory(Scheme)
ParameterCodec = runtime.NewParameterCodec(Scheme)
AddToScheme(Scheme)
AddToScheme(scheme.Scheme)
}
type RestConfigHookFunc func(*rest.Config)
var restConfigHooks []RestConfigHookFunc
var restConfigHooksLock sync.Mutex
var virtclient KubevirtClient
var once sync.Once
// Init adds the default `kubeconfig` and `master` flags. It is not added by default to allow integration into
// the different controller generators which normally add these flags too.
func Init() {
if flag.CommandLine.Lookup("kubeconfig") == nil {
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
}
if flag.CommandLine.Lookup("master") == nil {
flag.StringVar(&master, "master", "", "master url")
}
}
func RegisterRestConfigHook(fn RestConfigHookFunc) {
restConfigHooksLock.Lock()
defer restConfigHooksLock.Unlock()
restConfigHooks = append(restConfigHooks, fn)
}
func executeRestConfigHooks(config *rest.Config) {
restConfigHooksLock.Lock()
defer restConfigHooksLock.Unlock()
for _, hookFn := range restConfigHooks {
hookFn(config)
}
}
func FlagSet() *flag.FlagSet {
set := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
set.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
set.StringVar(&master, "master", "", "master url")
return set
}
func GetKubevirtSubresourceClientFromFlags(master string, kubeconfig string) (KubevirtClient, error) {
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
config.GroupVersion = &v1.SubresourceStorageGroupVersion
config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: Codecs}
config.APIPath = "/apis"
config.ContentType = runtime.ContentTypeJSON
restClient, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
}
coreClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
generatedKubeVirtClient, err := generatedclient.NewForConfig(config)
if err != nil {
return nil, err
}
cdiClient, err := cdiclient.NewForConfig(config)
if err != nil {
return nil, err
}
networkClient, err := networkclient.NewForConfig(config)
if err != nil {
return nil, err
}
extensionsClient, err := extclient.NewForConfig(config)
if err != nil {
return nil, err
}
secClient, err := secv1.NewForConfig(config)
if err != nil {
return nil, err
}
routeClient, err := routev1.NewForConfig(config)
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
prometheusClient, err := promclient.NewForConfig(config)
if err != nil {
return nil, err
}
snapshotClient, err := k8ssnapshotclient.NewForConfig(config)
if err != nil {
return nil, err
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
migrationsClient, err := migrationsv1.NewForConfig(config)
if err != nil {
return nil, err
}
cloneClient, err := clonev1alpha1.NewForConfig(config)
if err != nil {
return nil, err
}
return &kubevirt{
master,
kubeconfig,
restClient,
config,
generatedKubeVirtClient,
cdiClient,
networkClient,
extensionsClient,
secClient,
routeClient,
discoveryClient,
prometheusClient,
snapshotClient,
dynamicClient,
migrationsClient,
cloneClient,
coreClient,
}, nil
}
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
//
// 1. Use the kubeconfig builder. The number of merges and overrides here gets a little crazy. Stay with me.
//
// 1. Merge the kubeconfig itself. This is done with the following hierarchy rules:
//
// 1. CommandLineLocation - this parsed from the command line, so it must be late bound. If you specify this,
// then no other kubeconfig files are merged. This file must exist.
//
// 2. If $KUBECONFIG is set, then it is treated as a list of files that should be merged.
//
// 3. HomeDirectoryLocation
// Empty filenames are ignored. Files with non-deserializable content produced errors.
// The first file to set a particular value or map key wins and the value or map key is never changed.
// This means that the first file to set CurrentContext will have its context preserved. It also means
// that if two files specify a "red-user", only values from the first file's red-user are used. Even
// non-conflicting entries from the second file's "red-user" are discarded.
//
// 2. Determine the context to use based on the first hit in this chain
//
// 1. command line argument - again, parsed from the command line, so it must be late bound
//
// 2. CurrentContext from the merged kubeconfig file
//
// 3. Empty is allowed at this stage
//
// 3. Determine the cluster info and auth info to use. At this point, we may or may not have a context. They
// are built based on the first hit in this chain. (run it twice, once for auth, once for cluster)
//
// 1. command line argument
//
// 2. If context is present, then use the context value
//
// 3. Empty is allowed
//
// 4. Determine the actual cluster info to use. At this point, we may or may not have a cluster info. Build
// each piece of the cluster info based on the chain:
//
// 1. command line argument
//
// 2. If cluster info is present and a value for the attribute is present, use it.
//
// 3. If you don't have a server location, bail.
//
// 5. Auth info is build using the same rules as cluster info, EXCEPT that you can only have one authentication
// technique per auth info. The following conditions result in an error:
//
// 1. If there are two conflicting techniques specified from the command line, fail.
//
// 2. If the command line does not specify one, and the auth info has conflicting techniques, fail.
//
// 3. If the command line specifies one and the auth info specifies another, honor the command line technique.
//
// 2. Use default values and potentially prompt for auth information
//
// However, if it appears that we're running in a kubernetes cluster
// container environment, then run with the auth info kubernetes mounted for
// us. Specifically:
// The env vars KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT are
// set, and the file /var/run/secrets/kubernetes.io/serviceaccount/token
// exists and is not a directory.
//
// Initially copied from https://github.com/kubernetes/kubernetes/blob/09f321c80bfc9bca63a5530b56d7a1a3ba80ba9b/pkg/kubectl/cmd/util/factory_client_access.go#L174
func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// use the standard defaults for this client command
// DEPRECATED: remove and replace with something more accurate
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
flags.StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
flagNames := clientcmd.RecommendedConfigOverrideFlags("")
// short flagnames are disabled by default. These are here for compatibility with existing scripts
flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"
clientcmd.BindOverrideFlags(overrides, flags, flagNames)
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
return clientConfig
}
// this function is defined as a closure so iut could be overwritten by unit tests
var GetKubevirtClientFromClientConfig = func(cmdConfig clientcmd.ClientConfig) (KubevirtClient, error) {
config, err := cmdConfig.ClientConfig()
if err != nil {
return nil, err
}
return GetKubevirtClientFromRESTConfig(config)
}
func GetKubevirtClientFromRESTConfig(config *rest.Config) (KubevirtClient, error) {
shallowCopy := *config
shallowCopy.GroupVersion = &v1.StorageGroupVersion
shallowCopy.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: Codecs}
shallowCopy.APIPath = "/apis"
shallowCopy.ContentType = runtime.ContentTypeJSON
if config.UserAgent == "" {
config.UserAgent = restclient.DefaultKubernetesUserAgent()
}
executeRestConfigHooks(&shallowCopy)
restClient, err := rest.RESTClientFor(&shallowCopy)
if err != nil {
return nil, err
}
coreClient, err := kubernetes.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
generatedKubeVirtClient, err := generatedclient.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
cdiClient, err := cdiclient.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
networkClient, err := networkclient.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
extensionsClient, err := extclient.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
secClient, err := secv1.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
routeClient, err := routev1.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(&shallowCopy)
if err != nil {
return nil, err
}
prometheusClient, err := promclient.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
snapshotClient, err := k8ssnapshotclient.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
dynamicClient, err := dynamic.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
migrationsClient, err := migrationsv1.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
cloneClient, err := clonev1alpha1.NewForConfig(&shallowCopy)
if err != nil {
return nil, err
}
return &kubevirt{
master,
kubeconfig,
restClient,
&shallowCopy,
generatedKubeVirtClient,
cdiClient,
networkClient,
extensionsClient,
secClient,
routeClient,
discoveryClient,
prometheusClient,
snapshotClient,
dynamicClient,
migrationsClient,
cloneClient,
coreClient,
}, nil
}
func GetKubevirtClientFromFlags(master string, kubeconfig string) (KubevirtClient, error) {
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
return GetKubevirtClientFromRESTConfig(config)
}
func GetKubevirtClient() (KubevirtClient, error) {
var err error
once.Do(func() {
virtclient, err = GetKubevirtClientFromFlags(master, kubeconfig)
})
return virtclient, err
}
func GetKubevirtSubresourceClient() (KubevirtClient, error) {
return GetKubevirtSubresourceClientFromFlags(master, kubeconfig)
}
// Deprecated: Use GetKubevirtClientConfig instead
func GetConfig() (*restclient.Config, error) {
return clientcmd.BuildConfigFromFlags(master, kubeconfig)
}
func GetKubevirtClientConfig() (*rest.Config, error) {
return GetConfig()
}