Skip to content

Commit

Permalink
feat: add kind-disable-load and k3d-disable-load config values
Browse files Browse the repository at this point in the history
  • Loading branch information
shaxbee committed Nov 9, 2020
1 parent 0bbcd9a commit 20dd35e
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 30 deletions.
74 changes: 74 additions & 0 deletions cmd/skaffold/app/cmd/config/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,80 @@ func TestSetAndUnsetConfig(t *testing.T) {
},
},
},
{
description: "set kind disable load",
key: "kind-disable-load",
value: "true",
kubecontext: "this_is_a_context",
expectedSetCfg: &config.GlobalConfig{
ContextConfigs: []*config.ContextConfig{
{
Kubecontext: "this_is_a_context",
KindDisableLoad: util.BoolPtr(true),
},
},
},
expectedUnsetCfg: &config.GlobalConfig{
ContextConfigs: []*config.ContextConfig{
{
Kubecontext: "this_is_a_context",
},
},
},
},
{
description: "set global kind disable load",
key: "kind-disable-load",
value: "true",
global: true,
expectedSetCfg: &config.GlobalConfig{
Global: &config.ContextConfig{
KindDisableLoad: util.BoolPtr(true),
},
ContextConfigs: []*config.ContextConfig{},
},
expectedUnsetCfg: &config.GlobalConfig{
Global: &config.ContextConfig{},
ContextConfigs: []*config.ContextConfig{},
},
},
{
description: "set k3d disable load",
key: "k3d-disable-load",
value: "true",
kubecontext: "this_is_a_context",
expectedSetCfg: &config.GlobalConfig{
ContextConfigs: []*config.ContextConfig{
{
Kubecontext: "this_is_a_context",
K3dDisableLoad: util.BoolPtr(true),
},
},
},
expectedUnsetCfg: &config.GlobalConfig{
ContextConfigs: []*config.ContextConfig{
{
Kubecontext: "this_is_a_context",
},
},
},
},
{
description: "set global k3d disable load",
key: "k3d-disable-load",
value: "true",
global: true,
expectedSetCfg: &config.GlobalConfig{
Global: &config.ContextConfig{
K3dDisableLoad: util.BoolPtr(true),
},
ContextConfigs: []*config.ContextConfig{},
},
expectedUnsetCfg: &config.GlobalConfig{
Global: &config.ContextConfig{},
ContextConfigs: []*config.ContextConfig{},
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/skaffold/config/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type ContextConfig struct {
DebugHelpersRegistry string `yaml:"debug-helpers-registry,omitempty"`
UpdateCheck *bool `yaml:"update-check,omitempty"`
Survey *SurveyConfig `yaml:"survey,omitempty"`
KindDisableLoad *bool `yaml:"kind-disable-load,omitempty"`
K3dDisableLoad *bool `yaml:"k3d-disable-load,omitempty"`
}

// SurveyConfig is the survey config information
Expand Down
22 changes: 17 additions & 5 deletions pkg/skaffold/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ func GetDebugHelpersRegistry(configFile string) (string, error) {
return constants.DefaultDebugHelpersRegistry, nil
}

// IsImageLoadingRequired checks if the cluster requires loading images into it
func IsImageLoadingRequired(configFile string) (bool, error) {
cfg, err := GetConfigForCurrentKubectx(configFile)
if err != nil {
return false, err
}

kubeContext := cfg.Kubecontext
kindDisableLoad := cfg.KindDisableLoad != nil && *cfg.KindDisableLoad
k3dDisableLoad := cfg.K3dDisableLoad != nil && *cfg.K3dDisableLoad

fmt.Println(cfg.Kubecontext, kindDisableLoad, k3dDisableLoad)

return (IsKindCluster(kubeContext) && !kindDisableLoad) ||
(IsK3dCluster(kubeContext) && !k3dDisableLoad), nil
}

func isDefaultLocal(kubeContext string, detectMinikubeCluster bool) bool {
if kubeContext == constants.DefaultMinikubeContext ||
kubeContext == constants.DefaultDockerForDesktopContext ||
Expand All @@ -228,11 +245,6 @@ func isDefaultLocal(kubeContext string, detectMinikubeCluster bool) bool {
return false
}

// IsImageLoadingRequired checks if the cluster requires loading images into it
func IsImageLoadingRequired(kubeContext string) bool {
return IsKindCluster(kubeContext) || IsK3dCluster(kubeContext)
}

// IsKindCluster checks that the given `kubeContext` is talking to `kind`.
func IsKindCluster(kubeContext string) bool {
switch {
Expand Down
67 changes: 53 additions & 14 deletions pkg/skaffold/config/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,25 +300,64 @@ func (fakeClient) MinikubeExec(...string) (*exec.Cmd, error) { return nil, nil }

func TestIsImageLoadingRequired(t *testing.T) {
tests := []struct {
context string
expectedImageLoadingRequired bool
cfg *ContextConfig
expected bool
}{
{context: "kind-other", expectedImageLoadingRequired: true},
{context: "kind@kind", expectedImageLoadingRequired: true},
{context: "k3d-k3s-default", expectedImageLoadingRequired: true},
{context: "docker-for-desktop", expectedImageLoadingRequired: false},
{context: "minikube", expectedImageLoadingRequired: false},
{context: "docker-desktop", expectedImageLoadingRequired: false},
{context: "anything-else", expectedImageLoadingRequired: false},
{context: "kind@blah", expectedImageLoadingRequired: false},
{context: "other-kind", expectedImageLoadingRequired: false},
{context: "not-k3d", expectedImageLoadingRequired: false},
{
cfg: &ContextConfig{Kubecontext: "kind-other"},
expected: true,
},
{

cfg: &ContextConfig{Kubecontext: "kind-other", KindDisableLoad: util.BoolPtr(true)},
expected: false,
},
{
cfg: &ContextConfig{Kubecontext: "kind@kind"},
expected: true,
},
{
cfg: &ContextConfig{Kubecontext: "k3d-k3s-default"},
expected: true,
},
{
cfg: &ContextConfig{Kubecontext: "k3d-k3s-default", K3dDisableLoad: util.BoolPtr(true)},
expected: false,
},
{
cfg: &ContextConfig{Kubecontext: "docker-for-desktop"},
expected: false,
},
{
cfg: &ContextConfig{Kubecontext: "minikube"},
expected: false,
},
{
cfg: &ContextConfig{Kubecontext: "docker-desktop"},
expected: false,
},
{
cfg: &ContextConfig{Kubecontext: "anything-else"},
expected: false},
{
cfg: &ContextConfig{Kubecontext: "kind@blah"},
expected: false},
{
cfg: &ContextConfig{Kubecontext: "other-kind"},
expected: false,
},
{
cfg: &ContextConfig{Kubecontext: "not-k3d"},
expected: false,
},
}
for _, test := range tests {
testutil.Run(t, "", func(t *testutil.T) {
imageLoadingRequired := IsImageLoadingRequired(test.context)
t.Override(&GetConfigForCurrentKubectx, func(string) (*ContextConfig, error) { return test.cfg, nil })

imageLoadingRequired, _ := IsImageLoadingRequired("dummyname")

t.CheckDeepEqual(test.expectedImageLoadingRequired, imageLoadingRequired)
t.CheckDeepEqual(test.expected, imageLoadingRequired)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ See https://skaffold.dev/docs/pipeline-stages/taggers/#how-tagging-works`)
return fmt.Errorf("unable to connect to Kubernetes: %w", err)
}

if r.imagesAreLocal && config.IsImageLoadingRequired(r.runCtx.GetKubeContext()) {
if r.runCtx.ImageLoadingRequired && r.imagesAreLocal {
err := r.loadImagesIntoCluster(ctx, out, artifacts)
if err != nil {
return err
Expand Down
27 changes: 17 additions & 10 deletions pkg/skaffold/runner/runcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ type RunContext struct {
Opts config.SkaffoldOptions
Cfg latest.Pipeline

KubeContext string
Namespaces []string
WorkingDir string
InsecureRegistries map[string]bool
KubeContext string
Namespaces []string
WorkingDir string
InsecureRegistries map[string]bool
ImageLoadingRequired bool
}

func (rc *RunContext) GetKubeContext() string { return rc.KubeContext }
Expand Down Expand Up @@ -114,13 +115,19 @@ func GetRunContext(opts config.SkaffoldOptions, cfg latest.Pipeline) (*RunContex
insecureRegistries[r] = true
}

imageLoadingRequired, err := config.IsImageLoadingRequired(opts.GlobalConfig)
if err != nil {
logrus.Warnf("error reading image loading settings from global config: images will be not loaded")
}

return &RunContext{
Opts: opts,
Cfg: cfg,
WorkingDir: cwd,
KubeContext: kubeContext,
Namespaces: namespaces,
InsecureRegistries: insecureRegistries,
Opts: opts,
Cfg: cfg,
WorkingDir: cwd,
KubeContext: kubeContext,
Namespaces: namespaces,
InsecureRegistries: insecureRegistries,
ImageLoadingRequired: imageLoadingRequired,
}, nil
}

Expand Down

0 comments on commit 20dd35e

Please sign in to comment.