Skip to content

Commit dce2c12

Browse files
committed
review comments, and small fix for writing config entry that doesn't previously exist
1 parent 20b9866 commit dce2c12

File tree

6 files changed

+49
-48
lines changed

6 files changed

+49
-48
lines changed

cmd/skaffold/app/cmd/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ type Config struct {
2222
}
2323

2424
type ContextConfig struct {
25-
Kubectx string `yaml:"kubectx,omitempty"`
25+
Kubecontext string `yaml:"kube-context,omitempty"`
2626
DefaultRepo string `yaml:"default-repo"`
2727
}

cmd/skaffold/app/cmd/config/flags.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
"github.com/spf13/cobra"
2121
)
2222

23-
var configFile, kubectx string
23+
var configFile, kubecontext string
2424
var showAll, global bool
2525

2626
func AddConfigFlags(cmd *cobra.Command) {
2727
cmd.Flags().StringVarP(&configFile, "config", "c", "", "path to skaffold config")
28-
cmd.Flags().StringVarP(&kubectx, "kubectx", "k", "", "kubectl context to set values against")
28+
cmd.Flags().StringVarP(&kubecontext, "kube-context", "k", "", "kubectl context to set values against")
2929
}
3030

3131
func AddListFlags(cmd *cobra.Command) {

cmd/skaffold/app/cmd/config/list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func runList(out io.Writer) error {
5252
return errors.Wrap(err, "marshaling config")
5353
}
5454
} else {
55-
configs, err := getConfigForKubectx()
55+
config, err := getConfigForKubectx()
5656
if err != nil {
5757
return err
5858
}
59-
configYaml, err = yaml.Marshal(&configs)
59+
configYaml, err = yaml.Marshal(&config)
6060
if err != nil {
6161
return errors.Wrap(err, "marshaling config")
6262
}

cmd/skaffold/app/cmd/config/set.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewCmdSet(out io.Writer) *cobra.Command {
4444
}
4545

4646
func setConfigValue(name string, value interface{}) error {
47-
cfg, err := getConfigForKubectx()
47+
cfg, err := getOrCreateConfigForKubectx()
4848
if err != nil {
4949
return err
5050
}
@@ -77,20 +77,24 @@ func setConfigValue(name string, value interface{}) error {
7777
}
7878

7979
func writeConfig(cfg *ContextConfig) error {
80-
configs, err := readConfig()
80+
fullConfig, err := readConfig()
8181
if err != nil {
8282
return err
8383
}
8484
if global {
85-
configs.Global = cfg
85+
fullConfig.Global = cfg
8686
} else {
87-
for i, contextCfg := range configs.ContextConfigs {
88-
if contextCfg.Kubectx == kubectx {
89-
configs.ContextConfigs[i] = cfg
87+
for i, contextCfg := range fullConfig.ContextConfigs {
88+
if contextCfg.Kubecontext == kubecontext {
89+
fullConfig.ContextConfigs[i] = cfg
9090
}
9191
}
9292
}
93-
contents, err := yaml.Marshal(configs)
93+
return writeFullConfig(fullConfig)
94+
}
95+
96+
func writeFullConfig(cfg *Config) error {
97+
contents, err := yaml.Marshal(cfg)
9498
if err != nil {
9599
return errors.Wrap(err, "marshaling config")
96100
}

cmd/skaffold/app/cmd/config/util.go

+32-7
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ import (
3333
const defaultConfigLocation = ".skaffold/config"
3434

3535
func resolveKubectlContext() {
36-
if kubectx != "" {
36+
if kubecontext != "" {
3737
return
3838
}
3939

4040
context, err := context.CurrentContext()
4141
if err != nil {
4242
logrus.Warn(errors.Wrap(err, "retrieving current kubectl context"))
43-
kubectx = "default"
43+
kubecontext = "default"
4444
}
4545
if context == "" {
4646
logrus.Infof("no context currently set, falling back to default")
47-
kubectx = "default"
47+
kubecontext = "default"
4848
}
49-
kubectx = context
49+
kubecontext = context
5050
}
5151

5252
func resolveConfigFile() error {
@@ -95,7 +95,7 @@ func readConfig() (*Config, error) {
9595
return ReadConfigForFile(configFile)
9696
}
9797

98-
// return the specific config to be modified based on the provided kubectx.
98+
// return the specific config to be modified based on the provided kube context.
9999
// either returns the config corresponding to the provided or current context,
100100
// or the global config if that is specified (or if no current context is set).
101101
func getConfigForKubectx() (*ContextConfig, error) {
@@ -107,9 +107,34 @@ func getConfigForKubectx() (*ContextConfig, error) {
107107
return cfg.Global, nil
108108
}
109109
for _, contextCfg := range cfg.ContextConfigs {
110-
if contextCfg.Kubectx == kubectx {
110+
if contextCfg.Kubecontext == kubecontext {
111111
return contextCfg, nil
112112
}
113113
}
114-
return nil, fmt.Errorf("no config entry found for kubectx %s", kubectx)
114+
return nil, fmt.Errorf("no config entry found for kube-context %s", kubecontext)
115+
}
116+
117+
func getOrCreateConfigForKubectx() (*ContextConfig, error) {
118+
cfg, err := readConfig()
119+
if err != nil {
120+
return nil, err
121+
}
122+
if global {
123+
return cfg.Global, nil
124+
}
125+
for _, contextCfg := range cfg.ContextConfigs {
126+
if contextCfg.Kubecontext == kubecontext {
127+
return contextCfg, nil
128+
}
129+
}
130+
newCfg := &ContextConfig{
131+
Kubecontext: kubecontext,
132+
}
133+
cfg.ContextConfigs = append(cfg.ContextConfigs, newCfg)
134+
135+
if err := writeFullConfig(cfg); err != nil {
136+
return nil, err
137+
}
138+
139+
return newCfg, nil
115140
}

cmd/skaffold/app/cmd/config_test.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var baseConfig = &config.Config{
3131
},
3232
ContextConfigs: []*config.ContextConfig{
3333
{
34-
Kubectx: "test-context",
34+
Kubecontext: "test-context",
3535
DefaultRepo: "context-local-repository",
3636
},
3737
},
@@ -64,31 +64,3 @@ func TestReadConfig(t *testing.T) {
6464
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expectedCfg, cfg)
6565
}
6666
}
67-
68-
// func TestSetConfig(t *testing.T) {
69-
// c, _ := yaml.Marshal(*baseConfig)
70-
// cfg, teardown := testutil.TempFile(t, "config", c)
71-
// defer teardown()
72-
73-
// var tests = []struct {
74-
75-
// expectedCfg *config.Config
76-
// shouldErr bool
77-
// }{
78-
// {
79-
// filename: "",
80-
// shouldErr: true,
81-
// },
82-
// {
83-
// filename: cfg,
84-
// expectedCfg: baseConfig,
85-
// shouldErr: false,
86-
// },
87-
// }
88-
89-
// for _, test := range tests {
90-
// cfg, err := config.ReadConfigForFile(test.filename)
91-
92-
// testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expectedCfg, cfg)
93-
// }
94-
// }

0 commit comments

Comments
 (0)