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

feat: add kind-disable-load and k3d-disable-load config values #5012

Merged
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
35 changes: 16 additions & 19 deletions pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ func TestNewBuilder(t *testing.T) {
dummyDaemon := dummyLocalDaemon{}

tests := []struct {
description string
shouldErr bool
expectedPush bool
localBuild latest.LocalBuild
localClusterFn func(string, string, bool) (bool, error)
localDockerFn func(docker.Config) (docker.LocalDaemon, error)
description string
shouldErr bool
expectedPush bool
cluster config.Cluster
localBuild latest.LocalBuild
localDockerFn func(docker.Config) (docker.LocalDaemon, error)
}{
{
description: "failed to get docker client",
Expand All @@ -293,25 +293,19 @@ func TestNewBuilder(t *testing.T) {
shouldErr: true,
},
{
description: "pushImages becomes !localCluster when local:push is not defined",
description: "pushImages becomes cluster.PushImages when local:push is not defined",
localDockerFn: func(docker.Config) (docker.LocalDaemon, error) {
return dummyDaemon, nil
},
localClusterFn: func(string, string, bool) (b bool, e error) {
b = false //because this is false and localBuild.push is nil
return
},
cluster: config.Cluster{PushImages: true},
expectedPush: true,
},
{
description: "pushImages defined in config (local:push)",
localDockerFn: func(docker.Config) (docker.LocalDaemon, error) {
return dummyDaemon, nil
},
localClusterFn: func(string, string, bool) (b bool, e error) {
b = false
return
},
cluster: config.Cluster{PushImages: true},
localBuild: latest.LocalBuild{
Push: util.BoolPtr(false),
},
Expand All @@ -324,12 +318,10 @@ func TestNewBuilder(t *testing.T) {
if test.localDockerFn != nil {
t.Override(&docker.NewAPIClient, test.localDockerFn)
}
if test.localClusterFn != nil {
t.Override(&getLocalCluster, test.localClusterFn)
}

builder, err := NewBuilder(&mockConfig{
local: test.localBuild,
local: test.localBuild,
cluster: test.cluster,
})

t.CheckError(test.shouldErr, err)
Expand Down Expand Up @@ -442,6 +434,7 @@ type mockConfig struct {
runcontext.RunContext // Embedded to provide the default values.
local latest.LocalBuild
mode config.RunMode
cluster config.Cluster
}

func (c *mockConfig) Pipeline() latest.Pipeline {
Expand All @@ -453,3 +446,7 @@ func (c *mockConfig) Pipeline() latest.Pipeline {
func (c *mockConfig) Mode() config.RunMode {
return c.mode
}

func (c *mockConfig) GetCluster() config.Cluster {
return c.cluster
}
22 changes: 5 additions & 17 deletions pkg/skaffold/build/local/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,13 @@ type Builder struct {
artifactStore build.ArtifactStore
}

// external dependencies are wrapped
// into private functions for testability

var getLocalCluster = config.GetLocalCluster

type Config interface {
docker.Config

Pipeline() latest.Pipeline
GlobalConfig() string
GetKubeContext() string
DetectMinikube() bool
GetCluster() config.Cluster
SkipTests() bool
Mode() config.RunMode
NoPruneChildren() bool
Expand All @@ -82,19 +77,12 @@ func NewBuilder(cfg Config) (*Builder, error) {
return nil, fmt.Errorf("getting docker client: %w", err)
}

// TODO(https://github.com/GoogleContainerTools/skaffold/issues/3668):
// remove minikubeProfile from here and instead detect it by matching the
// kubecontext API Server to minikube profiles

localCluster, err := getLocalCluster(cfg.GlobalConfig(), cfg.MinikubeProfile(), cfg.DetectMinikube())
if err != nil {
return nil, fmt.Errorf("getting localCluster: %w", err)
}
cluster := cfg.GetCluster()

var pushImages bool
if cfg.Pipeline().Build.LocalBuild.Push == nil {
pushImages = !localCluster
logrus.Debugf("push value not present, defaulting to %t because localCluster is %t", pushImages, localCluster)
pushImages = cluster.PushImages
logrus.Debugf("push value not present, defaulting to %t because cluster.PushImages is %t", pushImages, cluster.PushImages)
} else {
pushImages = *cfg.Pipeline().Build.LocalBuild.Push
}
Expand All @@ -106,7 +94,7 @@ func NewBuilder(cfg Config) (*Builder, error) {
cfg: cfg,
kubeContext: cfg.GetKubeContext(),
localDocker: localDocker,
localCluster: localCluster,
localCluster: cluster.Local,
pushImages: pushImages,
tryImportMissing: tryImportMissing,
skipTests: cfg.SkipTests(),
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
6 changes: 6 additions & 0 deletions pkg/skaffold/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ func (m Muted) MuteDeploy() bool { return m.mute("deploy") }
func (m Muted) mute(phase string) bool {
return util.StrSliceContains(m.Phases, phase) || util.StrSliceContains(m.Phases, "all")
}

type Cluster struct {
Local bool
PushImages bool
LoadImages bool
}
75 changes: 41 additions & 34 deletions pkg/skaffold/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,6 @@ func GetDefaultRepo(configFile string, cliValue *string) (string, error) {
return cfg.DefaultRepo, nil
}

func GetLocalCluster(configFile string, minikubeProfile string, detectMinikubeCluster bool) (bool, error) {
if minikubeProfile != "" {
return true, nil
}
cfg, err := GetConfigForCurrentKubectx(configFile)
if err != nil {
return false, err
}
// when set, the local-cluster config takes precedence
if cfg.LocalCluster != nil {
logrus.Infof("Using local-cluster=%v from config", *cfg.LocalCluster)
return *cfg.LocalCluster, nil
}

config, err := kubectx.CurrentConfig()
if err != nil {
return true, err
}
return isDefaultLocal(config.CurrentContext, detectMinikubeCluster), nil
}

func GetInsecureRegistries(configFile string) ([]string, error) {
cfg, err := GetConfigForCurrentKubectx(configFile)
if err != nil {
Expand All @@ -214,23 +193,51 @@ func GetDebugHelpersRegistry(configFile string) (string, error) {
return constants.DefaultDebugHelpersRegistry, nil
}

func isDefaultLocal(kubeContext string, detectMinikubeCluster bool) bool {
if kubeContext == constants.DefaultMinikubeContext ||
func GetCluster(configFile string, minikubeProfile string, detectMinikube bool) (Cluster, error) {
cfg, err := GetConfigForCurrentKubectx(configFile)
if err != nil {
return Cluster{}, err
}

kubeContext := cfg.Kubecontext
isKindCluster, isK3dCluster := IsKindCluster(kubeContext), IsK3dCluster(kubeContext)

var local bool
switch {
case minikubeProfile != "":
local = true

case cfg.LocalCluster != nil:
logrus.Infof("Using local-cluster=%t from config", *cfg.LocalCluster)
local = *cfg.LocalCluster

case kubeContext == constants.DefaultMinikubeContext ||
kubeContext == constants.DefaultDockerForDesktopContext ||
kubeContext == constants.DefaultDockerDesktopContext ||
IsKindCluster(kubeContext) ||
IsK3dCluster(kubeContext) {
return true
}
if detectMinikubeCluster {
return cluster.GetClient().IsMinikube(kubeContext)
isKindCluster || isK3dCluster:
local = true

case detectMinikube:
local = cluster.GetClient().IsMinikube(kubeContext)

default:
local = false
}
return false
}

// IsImageLoadingRequired checks if the cluster requires loading images into it
func IsImageLoadingRequired(kubeContext string) bool {
return IsKindCluster(kubeContext) || IsK3dCluster(kubeContext)
kindDisableLoad := cfg.KindDisableLoad != nil && *cfg.KindDisableLoad
k3dDisableLoad := cfg.K3dDisableLoad != nil && *cfg.K3dDisableLoad

// load images for local kind/k3d cluster unless explicitly disabled
loadImages := local && ((isKindCluster && !kindDisableLoad) || (isK3dCluster && !k3dDisableLoad))

// push images for remote cluster or local kind/k3d cluster with image loading disabled
pushImages := !local || (isKindCluster && kindDisableLoad) || (isK3dCluster && k3dDisableLoad)

return Cluster{
Local: local,
LoadImages: loadImages,
PushImages: pushImages,
}, nil
}

// IsKindCluster checks that the given `kubeContext` is talking to `kind`.
Expand Down
Loading