-
Notifications
You must be signed in to change notification settings - Fork 19
/
km.go
556 lines (459 loc) · 17.5 KB
/
km.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
Copyright © 2020 Portworx
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.
*/
package config
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// KubernetesConfigManager contains all the Kubernetes configuration
type KubernetesConfigManager struct {
kubeCliOpts *genericclioptions.ConfigFlags
}
var (
km *KubernetesConfigManager
)
// KM returns the Kubernetes configuration flags and settings
func KM() *KubernetesConfigManager {
if km == nil {
km = newKubernetesConfigManager()
}
return km
}
func SetKM(k *KubernetesConfigManager) {
km = k
}
func newKubernetesConfigManager() *KubernetesConfigManager {
return &KubernetesConfigManager{
kubeCliOpts: genericclioptions.NewConfigFlags(true),
}
}
func NewKubernetesConfigManagerForContext(context string) *KubernetesConfigManager {
r := newKubernetesConfigManager()
*r.kubeCliOpts.Context = context
return r
}
// ConfigFlags returns the kubernetes raw configuration object
func (k *KubernetesConfigManager) ConfigFlags() *genericclioptions.ConfigFlags {
return k.kubeCliOpts
}
// ToRawKubeConfigLoader binds config flag values to config overrides
// Returns an interactive clientConfig if the password flag is enabled,
// or a non-interactive clientConfig otherwise.
// comment from k8s.io/cli-runtime
func (k *KubernetesConfigManager) ToRawKubeConfigLoader() clientcmd.ClientConfig {
return k.ConfigFlags().ToRawKubeConfigLoader()
}
// ToRESTConfig implements RESTClientGetter.
// Returns a REST client configuration based on a provided path
// to a .kubeconfig file, loading rules, and config flag overrides.
// Expects the AddFlags method to have been called.
// comment from k8s.io/cli-runtime
func (k *KubernetesConfigManager) ToRESTConfig() (*rest.Config, error) {
return k.ConfigFlags().ToRESTConfig()
}
// GetStartingKubeconfig is used to adjust the current Kubernetes config. You can then
// call ModifyKubeconfig() with the modified configuration
func (k *KubernetesConfigManager) GetStartingKubeconfig() (*clientcmdapi.Config, error) {
return k.ToRawKubeConfigLoader().ConfigAccess().GetStartingConfig()
}
// ModifyKubeconfig takes a modified configuration and seves it to disk
func (k *KubernetesConfigManager) ModifyKubeconfig(newConfig *clientcmdapi.Config) error {
return clientcmd.ModifyConfig(k.ToRawKubeConfigLoader().ConfigAccess(), *newConfig, true)
}
// GetCurrentCluster returns configuration information about the current cluster
func (k *KubernetesConfigManager) GetCurrentCluster() (*clientcmdapi.Cluster, error) {
kConfig, err := k.ToRawKubeConfigLoader().RawConfig()
if err != nil {
return nil, err
}
currentContext, err := k.GetKubernetesCurrentContext()
if err != nil {
return nil, err
}
clusterInfoName := kConfig.Contexts[currentContext].Cluster
if len(clusterInfoName) == 0 {
return nil, fmt.Errorf("Current cluster is not set in Kubeconfig")
}
if clusterInfo, ok := kConfig.Clusters[clusterInfoName]; ok {
return clusterInfo, nil
}
return nil, fmt.Errorf("Current user information not found in Kubeconfig")
}
// GetCurrentAuthInfo returns configuration information about the current user
func (k *KubernetesConfigManager) GetCurrentAuthInfo() (*clientcmdapi.AuthInfo, error) {
kConfig, err := k.ToRawKubeConfigLoader().RawConfig()
if err != nil {
return nil, err
}
currentContext, err := k.GetKubernetesCurrentContext()
if err != nil {
return nil, err
}
authInfoName := kConfig.Contexts[currentContext].AuthInfo
if len(authInfoName) == 0 {
return nil, fmt.Errorf("Current user is not set in Kubeconfig")
}
if authInfo, ok := kConfig.AuthInfos[authInfoName]; ok {
return authInfo, nil
}
return nil, fmt.Errorf("Current user information not found in Kubeconfig")
}
// KubectlFlagsToCliArgs rebuilds the flags as cli args
func (k *KubernetesConfigManager) KubectlFlagsToCliArgs() string {
var args string
if len(*k.kubeCliOpts.KubeConfig) != 0 {
args = "--kubeconfig=" + *k.kubeCliOpts.KubeConfig + " "
}
if len(*k.kubeCliOpts.Context) != 0 {
args += "--context=" + *k.kubeCliOpts.Context + " "
}
if len(*k.kubeCliOpts.BearerToken) != 0 {
args += "--token=" + *k.kubeCliOpts.BearerToken + " "
}
if len(*k.kubeCliOpts.APIServer) != 0 {
args += "--server=" + *k.kubeCliOpts.APIServer + " "
}
if len(*k.kubeCliOpts.CAFile) != 0 {
args += "--certificate-authority=" + *k.kubeCliOpts.CAFile + " "
}
if len(*k.kubeCliOpts.AuthInfoName) != 0 {
args += "--user=" + *k.kubeCliOpts.AuthInfoName + " "
}
if len(*k.kubeCliOpts.CertFile) != 0 {
args += "--client-certificate=" + *k.kubeCliOpts.CertFile + " "
}
if len(*k.kubeCliOpts.KeyFile) != 0 {
args += "--client-key=" + *k.kubeCliOpts.KeyFile + " "
}
if len(*k.kubeCliOpts.Namespace) != 0 {
args += "--namespace=" + *k.kubeCliOpts.Namespace + " "
}
return args
}
// SaveAuthInfoForKubeUser saves the pxc configuration in the kubeconfig file as a new user entry.
// Supply locationOfOrigin so that the Kubernetes saves the object with the appropriate user. LocationOfOrigin
// is found in each of the user objects in the kubernetes Config object.
func (k *KubernetesConfigManager) SaveAuthInfoForKubeUser(user, locationOfOrigin string, a *AuthInfo) error {
pxcName := KubeconfigUserPrefix + user
oldConfig, err := k.GetStartingKubeconfig()
if err != nil {
return err
}
// If one already exists it will be overwritten, if not create a new object
if v := oldConfig.AuthInfos[pxcName]; v == nil {
oldConfig.AuthInfos[pxcName] = clientcmdapi.NewAuthInfo()
}
// Store the pxc auth
oldConfig.AuthInfos[pxcName].LocationOfOrigin = locationOfOrigin
oldConfig.AuthInfos[pxcName].AuthProvider = &clientcmdapi.AuthProviderConfig{
Name: "portworx",
// Change the pxc AuthInfo to a map
Config: a.toMap(),
}
// Save the information in the kubeconfig
return k.ModifyKubeconfig(oldConfig)
}
// SaveClusterInKubeconfig stores pxc cluster configuration information in Kubeconfig
func (k *KubernetesConfigManager) SaveClusterInKubeconfig(clusterName, location string, c *Cluster) error {
pxcName := KubeconfigUserPrefix + clusterName
oldConfig, err := k.GetStartingKubeconfig()
if err != nil {
return err
}
if v := oldConfig.Clusters[pxcName]; v == nil {
oldConfig.Clusters[pxcName] = clientcmdapi.NewCluster()
}
encodedString, err := c.toEncodedString()
if err != nil {
return err
}
oldConfig.Clusters[pxcName].LocationOfOrigin = location
oldConfig.Clusters[pxcName].Server = "portworx-server"
oldConfig.Clusters[pxcName].CertificateAuthorityData = []byte(encodedString)
return k.ModifyKubeconfig(oldConfig)
}
// DeleteClusterInKubeconfig deletes the saved Portworx configuration in the kubeconfig
func (k *KubernetesConfigManager) DeleteClusterInKubeconfig(clusterName string) error {
pxcName := KubeconfigUserPrefix + clusterName
oldConfig, err := k.GetStartingKubeconfig()
if err != nil {
return err
}
if v := oldConfig.Clusters[pxcName]; v == nil {
return nil
}
delete(oldConfig.Clusters, pxcName)
return k.ModifyKubeconfig(oldConfig)
}
// DeleteAuthInfoInKubeconfig deletes the saved Portworx configuration in the kubeconfig
func (k *KubernetesConfigManager) DeleteAuthInfoInKubeconfig(authInfoName string) error {
pxcName := KubeconfigUserPrefix + authInfoName
oldConfig, err := k.GetStartingKubeconfig()
if err != nil {
return err
}
if v := oldConfig.AuthInfos[pxcName]; v == nil {
return nil
}
delete(oldConfig.AuthInfos, pxcName)
return k.ModifyKubeconfig(oldConfig)
}
// GetKubernetesCurrentContext returns the context currently selected by either the config
// file or from the command line
func (k *KubernetesConfigManager) GetKubernetesCurrentContext() (string, error) {
var contextName string
kConfig, err := k.ToRawKubeConfigLoader().RawConfig()
if err != nil {
return "", err
}
// Check if the was passed in the CLI flags
if k.kubeCliOpts.Context != nil && len(*k.kubeCliOpts.Context) != 0 {
contextName = *k.kubeCliOpts.Context
} else {
// Read it from the kubeconfig file
contextName = kConfig.CurrentContext
}
if len(contextName) == 0 {
return "", fmt.Errorf("Current context is not set or kubeconfig is missing")
}
logrus.Infof("CurrentContext = %s\n", contextName)
// Check that it is actually on the kubeconfig file
if _, ok := kConfig.Contexts[contextName]; !ok {
return "", fmt.Errorf("context %q does not exist", contextName)
}
return contextName, nil
}
// Namespace returns the namespace resulting from the merged
// result of all overrides and a boolean indicating if it was
// overridden
func (k *KubernetesConfigManager) Namespace() (string, bool, error) {
n, b, e := k.ToRawKubeConfigLoader().Namespace()
logrus.Infof("Kubernetes namespace: ns=%s b=%v e=%v", n, b, e)
return n, b, e
}
// ConfigSaveCluster saves the cluster configuration as part of an extension to the
// current context cluster in the Kubeconfig
func (k *KubernetesConfigManager) ConfigSaveCluster(clusterInfo *Cluster) error {
cc := k.ToRawKubeConfigLoader()
// This is the raw kubeconfig which may have been overridden by CLI args
kconfig, err := cc.RawConfig()
if err != nil {
return err
}
// Get the current context
currentContextName, err := k.GetKubernetesCurrentContext()
if err != nil {
return err
}
// Get the current context object
currentContext := kconfig.Contexts[currentContextName]
// Override the name to the name of the current cluster
clusterInfo.Name = currentContext.Cluster
// Get the location of the kubeconfig for this specific object. This is necessary
// because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf
location := kconfig.Clusters[currentContext.Cluster].LocationOfOrigin
// Storage the information to the appropriate kubeconfig
if err := k.SaveClusterInKubeconfig(currentContext.Cluster, location, clusterInfo); err != nil {
return err
}
logrus.Infof("Portworx server information saved in %s for Kubernetes cluster %s\n",
location,
currentContext.Cluster)
return nil
}
func (k *KubernetesConfigManager) ConfigDeleteCluster(name string) error {
cc := k.ToRawKubeConfigLoader()
// This is the raw kubeconfig which may have been overridden by CLI args
kconfig, err := cc.RawConfig()
if err != nil {
return err
}
// Get the current context
currentContextName, err := k.GetKubernetesCurrentContext()
if err != nil {
return err
}
currentContext := kconfig.Contexts[currentContextName]
// Get the location of the kubeconfig for this specific object. This is necessary
// because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf
location := kconfig.Clusters[currentContext.Cluster].LocationOfOrigin
// Storage the information to the appropriate kubeconfig
if err := k.DeleteClusterInKubeconfig(currentContext.Cluster); err != nil {
return err
}
logrus.Infof("Portworx server information removed from %s for Kubernetes cluster %s\n",
location,
currentContext.Cluster)
return nil
}
func (k *KubernetesConfigManager) ConfigLoad() (*Config, error) {
clusterConfig := newConfig()
// Get the current context, either from the file or from the args to the CLI
contextName, err := k.GetKubernetesCurrentContext()
if err != nil {
return nil, fmt.Errorf("failed to get kubectl context: %v", err)
}
// If the current context is not set, just create an empty object.
// This happens when there is no kubeconfig.
if len(contextName) == 0 {
clusterConfig.CurrentContext = contextName
clusterConfig.Contexts[contextName] = &Context{
AuthInfo: "",
Cluster: "",
}
return clusterConfig, nil
}
clientConfig := k.ConfigFlags().ToRawKubeConfigLoader()
kConfig, err := clientConfig.RawConfig()
if err != nil {
return nil, fmt.Errorf("unable to read kubernetes configuration: %v", err)
}
// Load all contexts
for k, v := range kConfig.Contexts {
clusterConfig.Contexts[k] = &Context{
Name: k,
AuthInfo: v.AuthInfo,
Cluster: v.Cluster,
}
}
// Initialize the context
clusterConfig.CurrentContext = contextName
clusterConfig.Contexts[contextName] = &Context{
AuthInfo: kConfig.Contexts[contextName].AuthInfo,
Cluster: kConfig.Contexts[contextName].Cluster,
}
// Load all the pxc authentication information from the kubeconfig file
for k, v := range kConfig.AuthInfos {
if strings.HasPrefix(k, KubeconfigUserPrefix) && v.AuthProvider != nil {
logrus.Debugf("Loading user %s from %s", k, v.LocationOfOrigin)
pxcAuthInfo := NewAuthInfoFromMap(v.AuthProvider.Config)
clusterConfig.AuthInfos[pxcAuthInfo.Name] = pxcAuthInfo
} else if _, ok := clusterConfig.AuthInfos[k]; !ok {
clusterConfig.AuthInfos[k] = NewAuthInfo()
clusterConfig.AuthInfos[k].Name = k
}
}
// Load all the pxc cluster information from the kubeconfig file
for k, c := range kConfig.Clusters {
if strings.HasPrefix(k, KubeconfigUserPrefix) {
pxcClusterInfo, err := NewClusterFromEncodedString(string(c.CertificateAuthorityData))
if err == nil {
logrus.Debugf("Loading cluster %s from %s", k, c.LocationOfOrigin)
clusterConfig.Clusters[pxcClusterInfo.Name] = pxcClusterInfo
} else {
logrus.Debugf("Unable to load cluster %s from %s", k, c.LocationOfOrigin)
}
} else if _, ok := clusterConfig.Clusters[k]; !ok {
clusterConfig.Clusters[k] = NewDefaultCluster()
clusterConfig.Clusters[k].Name = k
}
}
return clusterConfig, nil
}
func (k *KubernetesConfigManager) ConfigSaveAuthInfo(authInfo *AuthInfo) error {
if authInfo == nil {
panic("authInfo required")
}
cc := k.ToRawKubeConfigLoader()
save := false
// This is the raw kubeconfig which may have been overridden by CLI args
kconfig, err := cc.RawConfig()
if err != nil {
return err
}
// Get the current context
currentContextName, err := k.GetKubernetesCurrentContext()
if err != nil {
return err
}
currentContext := kconfig.Contexts[currentContextName]
// Initialize authInfo object
authInfo.Name = currentContext.AuthInfo
// Check for token
if len(authInfo.Token) != 0 {
save = true
// TODO: Validate if the token is expired
}
// Check for Kubernetes secret and secret namespace
if len(authInfo.KubernetesAuthInfo.SecretNamespace) != 0 &&
len(authInfo.KubernetesAuthInfo.SecretName) != 0 {
save = true
} else if len(authInfo.KubernetesAuthInfo.SecretNamespace) == 0 && len(authInfo.KubernetesAuthInfo.SecretName) != 0 {
return fmt.Errorf("Must supply secret namespace with secret name")
} else if len(authInfo.KubernetesAuthInfo.SecretNamespace) != 0 && len(authInfo.KubernetesAuthInfo.SecretName) == 0 {
return fmt.Errorf("Must supply secret name with secret namespace")
}
// Check if any information necessary was passed
if !save {
return fmt.Errorf("Must supply authentication information")
}
// Get the location of the kubeconfig for this specific authInfo. This is necessary
// because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf
location := kconfig.AuthInfos[currentContext.AuthInfo].LocationOfOrigin
// Storage the information to the appropriate kubeconfig
if err := k.SaveAuthInfoForKubeUser(currentContext.AuthInfo, location, authInfo); err != nil {
return err
}
logrus.Infof("Portworx login information saved in %s for Kubernetes user context %s\n",
location,
currentContext.AuthInfo)
return nil
}
// ConfigSaveContext does not do anything in kubectl plugin mode because it is managed
// by kubectl
func (k *KubernetesConfigManager) ConfigSaveContext(c *Context) error {
return fmt.Errorf("Use <kubectl config set-context> to set the context instead")
}
// ConfigDeleteAuthInfo deletes auth information for the current context
func (k *KubernetesConfigManager) ConfigDeleteAuthInfo(name string) error {
cc := k.ToRawKubeConfigLoader()
// This is the raw kubeconfig which may have been overridden by CLI args
kconfig, err := cc.RawConfig()
if err != nil {
return err
}
// Get the current context
currentContextName, err := k.GetKubernetesCurrentContext()
if err != nil {
return err
}
currentContext := kconfig.Contexts[currentContextName]
// Get the location of the kubeconfig for this specific object. This is necessary
// because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf
location := kconfig.AuthInfos[currentContext.AuthInfo].LocationOfOrigin
// Storage the information to the appropriate kubeconfig
if err := k.DeleteAuthInfoInKubeconfig(currentContext.AuthInfo); err != nil {
return err
}
logrus.Infof("Portworx server information removed from %s for Kubernetes cluster %s\n",
location,
currentContext.Cluster)
return nil
}
// ConfigDeleteContext deletes auth information for the current context
func (k *KubernetesConfigManager) ConfigDeleteContext(name string) error {
return fmt.Errorf("Use kubectl config to manage context")
}
// ConfigUseContext is not supported by kubectl plugin
func (k *KubernetesConfigManager) ConfigUseContext(name string) error {
return fmt.Errorf("Use kubectl to set the current context")
}
// ConfigGetCurrentContext returns the current context set by kubectl
func (k *KubernetesConfigManager) ConfigGetCurrentContext() (string, error) {
return k.GetKubernetesCurrentContext()
}