Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option in global config to define default kube-contexts #2558

Prev Previous commit
Next Next commit
Fall back to wildcard context configuration when no exact match is found
Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
  • Loading branch information
corneliusweig committed Aug 15, 2019
commit b2a25790966dbafc692a9622aaa285ceb2186d70
2 changes: 2 additions & 0 deletions cmd/skaffold/app/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func NewCmdSetKubeContext() *cobra.Command {
WithDescription("Set a default kube-context for a named skaffold.yaml").
WithExample("Use given kube-context when running current skaffold.yaml", "config set default-context <kube-context>").
WithExample("Use given kube-context for config with the given metadata.name", "config set default-context --skaffold-config <name> <kube-context>").
WithExample("Use given kube-context for every config", "config set default-context --global <kube-context>").
WithFlags(func(f *pflag.FlagSet) {
config.AddSetKubeconfigFlags(f)
}).
Expand All @@ -77,6 +78,7 @@ func NewCmdUnsetKubeContext() *cobra.Command {
WithDescription("Unset a default kube-context for a named skaffold.yaml").
WithExample("Unset the default kube-context for skaffold.yaml", "config unset default-context").
WithExample("Unset the default kube-context for config with the given metadata.name", "config unset default-context --skaffold-config <name>").
WithExample("Unset the wildcard default kube-context", "config unset default-context --global").
WithFlags(func(f *pflag.FlagSet) {
config.AddSetKubeconfigFlags(f)
}).
Expand Down
5 changes: 5 additions & 0 deletions pkg/skaffold/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
const (
defaultConfigDir = ".skaffold"
defaultConfigFile = "config"
wildcardContext = "*"
)

var (
Expand Down Expand Up @@ -196,6 +197,10 @@ func GetKubeContext(configFile, skaffoldConfigName, cliValue string) (string, er
return kubecontext, nil
}

if kubecontext, ok := cfg.SkaffoldConfigs[wildcardContext]; ok {
return kubecontext, nil
}

return "", nil
}

Expand Down
23 changes: 20 additions & 3 deletions pkg/skaffold/config/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func Test_getConfigForKubeContextWithGlobalDefaults(t *testing.T) {
}
}

func TestResolveDefaultKubeContext(t *testing.T) {
func TestGetKubeContext(t *testing.T) {
const (
someKubeContext = "this_is_a_context"
skaffoldConfigName = "skaffold-config-name"
Expand All @@ -235,15 +235,21 @@ func TestResolveDefaultKubeContext(t *testing.T) {
contextCLI: someKubeContext,
configName: skaffoldConfigName,
cfg: &GlobalConfig{
SkaffoldConfigs: map[string]string{skaffoldConfigName: "other_context"},
SkaffoldConfigs: map[string]string{
skaffoldConfigName: "other_context",
wildcardContext: "yet_another_context",
},
},
expectedContext: someKubeContext,
},
{
name: "when mapping is found",
configName: skaffoldConfigName,
cfg: &GlobalConfig{
SkaffoldConfigs: map[string]string{skaffoldConfigName: someKubeContext},
SkaffoldConfigs: map[string]string{
skaffoldConfigName: someKubeContext,
wildcardContext: "yet_another_context",
},
},
expectedContext: someKubeContext,
},
Expand All @@ -255,6 +261,17 @@ func TestResolveDefaultKubeContext(t *testing.T) {
},
expectedContext: "",
},
{
name: "fall back to wildcard context",
configName: skaffoldConfigName,
cfg: &GlobalConfig{
SkaffoldConfigs: map[string]string{
"other-config-name": "yet_another_context",
wildcardContext: someKubeContext,
},
},
expectedContext: someKubeContext,
},
{
name: "when SkaffoldConfigs is not set",
configName: skaffoldConfigName,
Expand Down