Skip to content

Commit 8aa4d28

Browse files
authored
Merge pull request #2555 from corneliusweig/w/fix-global-config
Add CLI flag `--config` for configuring the global config location
2 parents dc45181 + 2c75a47 commit 8aa4d28

File tree

17 files changed

+707
-314
lines changed

17 files changed

+707
-314
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import (
2222

2323
"github.com/pkg/errors"
2424
yaml "gopkg.in/yaml.v2"
25+
26+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
2527
)
2628

2729
func List(out io.Writer) error {
2830
var configYaml []byte
2931
if showAll {
30-
cfg, err := readConfig()
32+
cfg, err := config.ReadConfigFile(configFile)
3133
if err != nil {
3234
return err
3335
}
@@ -39,14 +41,14 @@ func List(out io.Writer) error {
3941
return errors.Wrap(err, "marshaling config")
4042
}
4143
} else {
42-
config, err := GetConfigForKubectx()
44+
contextConfig, err := getConfigForKubectx()
4345
if err != nil {
4446
return err
4547
}
46-
if config == nil { // empty config
48+
if contextConfig == nil { // empty config
4749
return nil
4850
}
49-
configYaml, err = yaml.Marshal(&config)
51+
configYaml, err = yaml.Marshal(&contextConfig)
5052
if err != nil {
5153
return errors.Wrap(err, "marshaling config")
5254
}
+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
Copyright 2019 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"bytes"
21+
"strings"
22+
"testing"
23+
24+
"gopkg.in/yaml.v2"
25+
26+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
27+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
28+
"github.com/GoogleContainerTools/skaffold/testutil"
29+
)
30+
31+
func TestList(t *testing.T) {
32+
const dummyContext = "dummy-context"
33+
34+
tests := []struct {
35+
cfg *config.GlobalConfig
36+
name string
37+
kubecontext string
38+
global bool
39+
showAll bool
40+
expectedOutput string
41+
}{
42+
{
43+
name: "print configs of a single kube-context",
44+
kubecontext: "this_is_a_context",
45+
cfg: &config.GlobalConfig{
46+
ContextConfigs: []*config.ContextConfig{
47+
{
48+
Kubecontext: "another_context",
49+
DefaultRepo: "other-value",
50+
LocalCluster: util.BoolPtr(false),
51+
InsecureRegistries: []string{"good.io", "better.io"},
52+
},
53+
{
54+
Kubecontext: "this_is_a_context",
55+
DefaultRepo: "value",
56+
LocalCluster: util.BoolPtr(true),
57+
InsecureRegistries: []string{"bad.io", "worse.io"},
58+
},
59+
},
60+
},
61+
expectedOutput: `kube-context: this_is_a_context
62+
default-repo: value
63+
local-cluster: true
64+
insecure-registries:
65+
- bad.io
66+
- worse.io
67+
`,
68+
},
69+
{
70+
name: "print global configs",
71+
global: true,
72+
cfg: &config.GlobalConfig{
73+
Global: &config.ContextConfig{
74+
DefaultRepo: "default-repo-value",
75+
LocalCluster: util.BoolPtr(true),
76+
InsecureRegistries: []string{"mediocre.io"},
77+
},
78+
ContextConfigs: []*config.ContextConfig{
79+
{
80+
Kubecontext: "this_is_a_context",
81+
DefaultRepo: "value",
82+
},
83+
},
84+
},
85+
expectedOutput: `default-repo: default-repo-value
86+
local-cluster: true
87+
insecure-registries:
88+
- mediocre.io
89+
`,
90+
},
91+
{
92+
name: "show all",
93+
showAll: true,
94+
cfg: &config.GlobalConfig{
95+
Global: &config.ContextConfig{
96+
DefaultRepo: "default-repo-value",
97+
LocalCluster: util.BoolPtr(true),
98+
InsecureRegistries: []string{"mediocre.io"},
99+
},
100+
ContextConfigs: []*config.ContextConfig{
101+
{
102+
Kubecontext: "this_is_a_context",
103+
DefaultRepo: "value",
104+
},
105+
},
106+
},
107+
expectedOutput: `
108+
global:
109+
default-repo: default-repo-value
110+
local-cluster: true
111+
insecure-registries:
112+
- mediocre.io
113+
kubeContexts:
114+
- kube-context: this_is_a_context
115+
default-repo: value
116+
`,
117+
},
118+
{
119+
name: "config has no values for kubecontext",
120+
kubecontext: "context-without-config",
121+
cfg: &config.GlobalConfig{
122+
Global: &config.ContextConfig{
123+
DefaultRepo: "default-repo-value",
124+
LocalCluster: util.BoolPtr(true),
125+
InsecureRegistries: []string{"mediocre.io"},
126+
},
127+
},
128+
},
129+
{
130+
name: "config has no values for global",
131+
kubecontext: "context-without-config",
132+
cfg: &config.GlobalConfig{
133+
ContextConfigs: []*config.ContextConfig{
134+
{
135+
Kubecontext: "this_is_a_context",
136+
DefaultRepo: "value",
137+
},
138+
},
139+
},
140+
},
141+
{
142+
name: "show all with empty config",
143+
showAll: true,
144+
kubecontext: "context-without-config",
145+
cfg: &config.GlobalConfig{},
146+
},
147+
}
148+
for _, test := range tests {
149+
testutil.Run(t, test.name, func(t *testutil.T) {
150+
// create new config file
151+
content, _ := yaml.Marshal(*test.cfg)
152+
cfg := t.TempFile("config", content)
153+
154+
t.Override(&config.ReadConfigFile, config.ReadConfigFileNoCache)
155+
t.Override(&configFile, cfg)
156+
t.Override(&global, test.global)
157+
t.Override(&showAll, test.showAll)
158+
if test.kubecontext != "" {
159+
t.Override(&kubecontext, test.kubecontext)
160+
} else {
161+
t.Override(&kubecontext, dummyContext)
162+
}
163+
164+
buf := &bytes.Buffer{}
165+
// list values
166+
err := List(buf)
167+
t.CheckNoError(err)
168+
169+
if test.expectedOutput != "" && !strings.HasSuffix(buf.String(), test.expectedOutput) {
170+
t.Errorf("expecting output to contain\n\n%s\nbut found\n\n%s\ninstead", test.expectedOutput, buf.String())
171+
}
172+
if test.expectedOutput == "" && buf.String() != "" {
173+
t.Errorf("expecting no output but found\n\n%s", buf.String())
174+
}
175+
})
176+
}
177+
}

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626

2727
"github.com/pkg/errors"
2828
yaml "gopkg.in/yaml.v2"
29+
30+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
2931
)
3032

3133
func Set(out io.Writer, args []string) error {
@@ -37,7 +39,7 @@ func Set(out io.Writer, args []string) error {
3739
}
3840

3941
func setConfigValue(name string, value string) error {
40-
cfg, err := getOrCreateConfigForKubectx()
42+
cfg, err := getConfigForKubectxOrDefault()
4143
if err != nil {
4244
return err
4345
}
@@ -58,7 +60,7 @@ func setConfigValue(name string, value string) error {
5860
return writeConfig(cfg)
5961
}
6062

61-
func getFieldName(cfg *ContextConfig, name string) string {
63+
func getFieldName(cfg *config.ContextConfig, name string) string {
6264
cfgValue := reflect.Indirect(reflect.ValueOf(cfg))
6365
var fieldName string
6466
for i := 0; i < cfgValue.NumField(); i++ {
@@ -96,29 +98,38 @@ func parseAsType(value string, field reflect.Value) (reflect.Value, error) {
9698
}
9799
}
98100

99-
func writeConfig(cfg *ContextConfig) error {
100-
fullConfig, err := readConfig()
101+
func writeConfig(cfg *config.ContextConfig) error {
102+
fullConfig, err := config.ReadConfigFile(configFile)
101103
if err != nil {
102104
return err
103105
}
104106
if global {
105107
fullConfig.Global = cfg
106108
} else {
109+
found := false
107110
for i, contextCfg := range fullConfig.ContextConfigs {
108111
if contextCfg.Kubecontext == kubecontext {
109112
fullConfig.ContextConfigs[i] = cfg
113+
found = true
110114
}
111115
}
116+
if !found {
117+
fullConfig.ContextConfigs = append(fullConfig.ContextConfigs, cfg)
118+
}
112119
}
113120
return writeFullConfig(fullConfig)
114121
}
115122

116-
func writeFullConfig(cfg *Config) error {
123+
func writeFullConfig(cfg *config.GlobalConfig) error {
117124
contents, err := yaml.Marshal(cfg)
118125
if err != nil {
119126
return errors.Wrap(err, "marshaling config")
120127
}
121-
err = ioutil.WriteFile(configFile, contents, 0644)
128+
configFileOrDefault, err := config.ResolveConfigFile(configFile)
129+
if err != nil {
130+
return err
131+
}
132+
err = ioutil.WriteFile(configFileOrDefault, contents, 0644)
122133
if err != nil {
123134
return errors.Wrap(err, "writing config file")
124135
}

0 commit comments

Comments
 (0)