Skip to content

Commit 410cd9f

Browse files
committed
[WIP] Global skaffold config, version 2
1 parent f6d5f48 commit 410cd9f

File tree

11 files changed

+84
-183
lines changed

11 files changed

+84
-183
lines changed

Gopkg.lock

+1-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ limitations under the License.
1616

1717
package config
1818

19-
type Config = []*ContextConfig
20-
21-
type ContextConfig struct {
22-
Context string `yaml:"context"`
23-
Values ContextValues `yaml:"values"`
19+
type Config struct {
20+
Global *ContextConfig
21+
ContextConfigs []*ContextConfig `yaml:"kubeContexts"`
2422
}
2523

26-
type ContextValues struct {
27-
// config values are added here
24+
type ContextConfig struct {
25+
Kubectx string `yaml:"kubectx,omitempty"`
26+
DefaultRepo string `yaml:"default-repo"`
2827
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ import (
2121
)
2222

2323
var configFile, kubectx string
24+
var showAll, global bool
2425

2526
func AddConfigFlags(cmd *cobra.Command) {
2627
cmd.Flags().StringVarP(&configFile, "config", "c", "", "path to skaffold config")
2728
cmd.Flags().StringVarP(&kubectx, "kubectx", "k", "", "kubectl context to set values against")
2829
}
30+
31+
func AddListFlags(cmd *cobra.Command) {
32+
cmd.Flags().BoolVarP(&showAll, "all", "a", false, "show values for all kubecontexts")
33+
}
34+
35+
func AddSetFlags(cmd *cobra.Command) {
36+
cmd.Flags().BoolVarP(&global, "global", "g", false, "set value for global config")
37+
}

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

+20-7
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,30 @@ func NewCmdList(out io.Writer) *cobra.Command {
3535
},
3636
}
3737
AddConfigFlags(cmd)
38+
AddListFlags(cmd)
3839
return cmd
3940
}
4041

4142
func runList(out io.Writer) error {
42-
configs, err := getConfigsForKubectx()
43-
if err != nil {
44-
return err
45-
}
46-
configYaml, err := yaml.Marshal(&configs)
47-
if err != nil {
48-
return errors.Wrap(err, "marshaling config")
43+
var configYaml []byte
44+
if showAll {
45+
cfg, err := readConfig()
46+
if err != nil {
47+
return err
48+
}
49+
configYaml, err = yaml.Marshal(&cfg)
50+
if err != nil {
51+
return errors.Wrap(err, "marshaling config")
52+
}
53+
} else {
54+
configs, err := getConfigForKubectx()
55+
if err != nil {
56+
return err
57+
}
58+
configYaml, err = yaml.Marshal(&configs)
59+
if err != nil {
60+
return errors.Wrap(err, "marshaling config")
61+
}
4962
}
5063

5164
out.Write(configYaml)

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

+38-31
Original file line numberDiff line numberDiff line change
@@ -39,52 +39,59 @@ func NewCmdSet(out io.Writer) *cobra.Command {
3939
},
4040
}
4141
AddConfigFlags(cmd)
42+
AddSetFlags(cmd)
4243
return cmd
4344
}
4445

4546
func setConfigValue(name string, value interface{}) error {
46-
configs, err := readConfig()
47+
cfg, err := getConfigForKubectx()
4748
if err != nil {
4849
return err
4950
}
50-
var cfg *ContextConfig
51-
for _, contextCfg := range *configs {
52-
if kubectx == "all" || contextCfg.Context == kubectx {
53-
cfg = contextCfg
54-
cfgValue := reflect.ValueOf(cfg.Values)
55-
var fieldName string
56-
for i := 0; i < cfgValue.NumField(); i++ {
57-
fieldType := reflect.TypeOf(cfg.Values).Field(i)
58-
for _, tag := range strings.Split(fieldType.Tag.Get("yaml"), ",") {
59-
if tag == name {
60-
fieldName = fieldType.Name
61-
}
62-
}
63-
}
64-
if fieldName == "" {
65-
return fmt.Errorf("%s is not a valid config field", name)
66-
}
67-
fieldValue := cfgValue.FieldByName(fieldName)
68-
69-
fieldType := fieldValue.Type()
70-
val := reflect.ValueOf(value)
7151

72-
if fieldType != val.Type() {
73-
return fmt.Errorf("%s is not a valid value for field %s", value, fieldName)
52+
cfgValue := reflect.Indirect(reflect.ValueOf(cfg))
53+
var fieldName string
54+
for i := 0; i < cfgValue.NumField(); i++ {
55+
fieldType := reflect.TypeOf(*cfg).Field(i)
56+
fmt.Printf("fieldType: %+v\n", fieldType)
57+
for _, tag := range strings.Split(fieldType.Tag.Get("yaml"), ",") {
58+
if tag == name {
59+
fieldName = fieldType.Name
7460
}
75-
76-
reflect.ValueOf(&cfg.Values).Elem().FieldByName(fieldName).Set(val)
7761
}
7862
}
79-
if cfg == nil {
80-
return fmt.Errorf("no config entry found for kubectx %s", kubectx)
63+
if fieldName == "" {
64+
return fmt.Errorf("%s is not a valid config field", name)
65+
}
66+
fieldValue := cfgValue.FieldByName(fieldName)
67+
68+
fieldType := fieldValue.Type()
69+
val := reflect.ValueOf(value)
70+
71+
if fieldType != val.Type() {
72+
return fmt.Errorf("%s is not a valid value for field %s", value, fieldName)
8173
}
8274

83-
return writeConfig(configs)
75+
reflect.ValueOf(cfg).Elem().FieldByName(fieldName).Set(val)
76+
77+
return writeConfig(cfg)
8478
}
8579

86-
func writeConfig(cfg *Config) error {
87-
contents, err := yaml.Marshal(cfg)
80+
func writeConfig(cfg *ContextConfig) error {
81+
configs, err := readConfig()
82+
if err != nil {
83+
return err
84+
}
85+
if global {
86+
configs.Global = cfg
87+
} else {
88+
for i, contextCfg := range configs.ContextConfigs {
89+
if contextCfg.Kubectx == kubectx {
90+
configs.ContextConfigs[i] = cfg
91+
}
92+
}
93+
}
94+
contents, err := yaml.Marshal(configs)
8895
if err != nil {
8996
return errors.Wrap(err, "marshaling config")
9097
}

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,20 @@ func readConfig() (*Config, error) {
9191
return &config, nil
9292
}
9393

94-
func getConfigsForKubectx() (*Config, error) {
95-
configs, err := readConfig()
94+
// return the specific config to be modified based on the provided kubectx.
95+
// either returns the config corresponding to the provided or current context,
96+
// or the global config if that is specified (or if no current context is set).
97+
func getConfigForKubectx() (*ContextConfig, error) {
98+
cfg, err := readConfig()
9699
if err != nil {
97100
return nil, err
98101
}
99-
if kubectx == "all" {
100-
return configs, nil
102+
if global {
103+
return cfg.Global, nil
101104
}
102-
for _, cfg := range *configs {
103-
if cfg.Context == kubectx {
104-
return &[]*ContextConfig{cfg}, nil
105+
for _, contextCfg := range cfg.ContextConfigs {
106+
if contextCfg.Kubectx == kubectx {
107+
return contextCfg, nil
105108
}
106109
}
107110
return nil, fmt.Errorf("no config entry found for kubectx %s", kubectx)

vendor/golang.org/x/sync/AUTHORS

-3
This file was deleted.

vendor/golang.org/x/sync/CONTRIBUTORS

-3
This file was deleted.

vendor/golang.org/x/sync/LICENSE

-27
This file was deleted.

vendor/golang.org/x/sync/PATENTS

-22
This file was deleted.

vendor/golang.org/x/sync/errgroup/errgroup.go

-67
This file was deleted.

0 commit comments

Comments
 (0)