This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
149 lines (119 loc) · 4.32 KB
/
config.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
/*
Copyright 2014 Google Inc. All rights reserved.
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"
"io"
"os"
"strconv"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
)
type pathOptions struct {
local bool
global bool
envvar bool
specifiedFile string
}
func NewCmdConfig(out io.Writer) *cobra.Command {
pathOptions := &pathOptions{}
cmd := &cobra.Command{
Use: "config <subcommand>",
Short: "config modifies .kubeconfig files",
Long: `config modifies .kubeconfig files using subcommands like "kubectl config set current-context my-context"`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
// file paths are common to all sub commands
cmd.PersistentFlags().BoolVar(&pathOptions.local, "local", false, "use the .kubeconfig in the current directory")
cmd.PersistentFlags().BoolVar(&pathOptions.global, "global", false, "use the .kubeconfig from "+os.Getenv("HOME"))
cmd.PersistentFlags().BoolVar(&pathOptions.envvar, "envvar", false, "use the .kubeconfig from $KUBECONFIG")
cmd.PersistentFlags().StringVar(&pathOptions.specifiedFile, "kubeconfig", "", "use a particular .kubeconfig file")
cmd.AddCommand(NewCmdConfigView(out, pathOptions))
cmd.AddCommand(NewCmdConfigSetCluster(out, pathOptions))
cmd.AddCommand(NewCmdConfigSetAuthInfo(out, pathOptions))
cmd.AddCommand(NewCmdConfigSetContext(out, pathOptions))
cmd.AddCommand(NewCmdConfigSet(out, pathOptions))
cmd.AddCommand(NewCmdConfigUnset(out, pathOptions))
cmd.AddCommand(NewCmdConfigUseContext(out, pathOptions))
return cmd
}
func (o *pathOptions) getStartingConfig() (*clientcmdapi.Config, string, error) {
filename := ""
config := clientcmdapi.NewConfig()
if len(o.specifiedFile) > 0 {
filename = o.specifiedFile
config = getConfigFromFileOrDie(filename)
}
if o.global {
if len(filename) > 0 {
return nil, "", fmt.Errorf("already loading from %v, cannot specify global as well", filename)
}
filename = os.Getenv("HOME") + "/.kube/.kubeconfig"
config = getConfigFromFileOrDie(filename)
}
if o.envvar {
if len(filename) > 0 {
return nil, "", fmt.Errorf("already loading from %v, cannot specify global as well", filename)
}
filename = os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(filename) == 0 {
return nil, "", fmt.Errorf("environment variable %v does not have a value", clientcmd.RecommendedConfigPathEnvVar)
}
config = getConfigFromFileOrDie(filename)
}
if o.local {
if len(filename) > 0 {
return nil, "", fmt.Errorf("already loading from %v, cannot specify global as well", filename)
}
filename = ".kubeconfig"
config = getConfigFromFileOrDie(filename)
}
// no specific flag was set, first attempt to use the envvar, then use local
if len(filename) == 0 {
if len(os.Getenv(clientcmd.RecommendedConfigPathEnvVar)) > 0 {
filename = os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
config = getConfigFromFileOrDie(filename)
} else {
filename = ".kubeconfig"
config = getConfigFromFileOrDie(filename)
}
}
return config, filename, nil
}
// getConfigFromFileOrDie tries to read a kubeconfig file and if it can't, it calls exit. One exception, missing files result in empty configs, not an exit
func getConfigFromFileOrDie(filename string) *clientcmdapi.Config {
var err error
config, err := clientcmd.LoadFromFile(filename)
if err != nil && !os.IsNotExist(err) {
glog.FatalDepth(1, err)
}
if config == nil {
config = clientcmdapi.NewConfig()
}
return config
}
func toBool(propertyValue string) (bool, error) {
boolValue := false
if len(propertyValue) != 0 {
var err error
boolValue, err = strconv.ParseBool(propertyValue)
if err != nil {
return false, err
}
}
return boolValue, nil
}