@@ -33,20 +33,20 @@ import (
33
33
const defaultConfigLocation = ".skaffold/config"
34
34
35
35
func resolveKubectlContext () {
36
- if kubectx != "" {
36
+ if kubecontext != "" {
37
37
return
38
38
}
39
39
40
40
context , err := context .CurrentContext ()
41
41
if err != nil {
42
42
logrus .Warn (errors .Wrap (err , "retrieving current kubectl context" ))
43
- kubectx = "default"
43
+ kubecontext = "default"
44
44
}
45
45
if context == "" {
46
46
logrus .Infof ("no context currently set, falling back to default" )
47
- kubectx = "default"
47
+ kubecontext = "default"
48
48
}
49
- kubectx = context
49
+ kubecontext = context
50
50
}
51
51
52
52
func resolveConfigFile () error {
@@ -95,7 +95,7 @@ func readConfig() (*Config, error) {
95
95
return ReadConfigForFile (configFile )
96
96
}
97
97
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 .
99
99
// either returns the config corresponding to the provided or current context,
100
100
// or the global config if that is specified (or if no current context is set).
101
101
func getConfigForKubectx () (* ContextConfig , error ) {
@@ -107,9 +107,34 @@ func getConfigForKubectx() (*ContextConfig, error) {
107
107
return cfg .Global , nil
108
108
}
109
109
for _ , contextCfg := range cfg .ContextConfigs {
110
- if contextCfg .Kubectx == kubectx {
110
+ if contextCfg .Kubecontext == kubecontext {
111
111
return contextCfg , nil
112
112
}
113
113
}
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
115
140
}
0 commit comments