diff --git a/cmd/skaffold/app/cmd/config/set_test.go b/cmd/skaffold/app/cmd/config/set_test.go index 3e97732d81f..a89eb44b672 100644 --- a/cmd/skaffold/app/cmd/config/set_test.go +++ b/cmd/skaffold/app/cmd/config/set_test.go @@ -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) { diff --git a/pkg/skaffold/config/global_config.go b/pkg/skaffold/config/global_config.go index f48cde680ce..db748fd09c3 100644 --- a/pkg/skaffold/config/global_config.go +++ b/pkg/skaffold/config/global_config.go @@ -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 diff --git a/pkg/skaffold/config/util.go b/pkg/skaffold/config/util.go index e76897c57d0..b46b82c895f 100644 --- a/pkg/skaffold/config/util.go +++ b/pkg/skaffold/config/util.go @@ -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 || @@ -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 { diff --git a/pkg/skaffold/config/util_test.go b/pkg/skaffold/config/util_test.go index f501eb31825..c2d2060486b 100644 --- a/pkg/skaffold/config/util_test.go +++ b/pkg/skaffold/config/util_test.go @@ -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) }) } } diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index e0ea6e9ff54..0dc6ac8b34a 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -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 diff --git a/pkg/skaffold/runner/runcontext/context.go b/pkg/skaffold/runner/runcontext/context.go index 4214728f566..a629d404759 100644 --- a/pkg/skaffold/runner/runcontext/context.go +++ b/pkg/skaffold/runner/runcontext/context.go @@ -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 } @@ -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 }