Skip to content

Commit

Permalink
refactor global config to allow buildx builders
Browse files Browse the repository at this point in the history
  • Loading branch information
reingart committed Jan 18, 2025
1 parent 44fb72c commit 0611204
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions pkg/skaffold/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ func (b *Builder) dockerCLIBuild(ctx context.Context, out io.Writer, name string
}
}

if b.buildx {
args = append(args, "--builder", config.GetBuildXBuilder(b.cfg.GlobalConfig()))
}

// temporary file for buildx metadata containing the image digest:
var metadata string
if b.buildx {
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/build/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestDockerCLIBuild(t *testing.T) {
buildx: true,
daemonless: false,
imageName: "gcr.io/k8s-skaffold/example:tag",
expectedCLIFlags: []string{"--cache-from", "gcr.io/k8s-skaffold/example:cache", "--load", "--metadata-file", "metadata.json"},
expectedCLIFlags: []string{"--cache-from", "gcr.io/k8s-skaffold/example:cache", "--load", "--builder", "default", "--metadata-file", "metadata.json"},
expectedEnv: []string{"KEY=VALUE", "DOCKER_BUILDKIT=1"},
},
{
Expand All @@ -106,7 +106,7 @@ func TestDockerCLIBuild(t *testing.T) {
buildx: true,
daemonless: true,
imageName: "gcr.io/k8s-skaffold/example:tag",
expectedCLIFlags: []string{"--cache-from", "gcr.io/k8s-skaffold/example:cache", "--cache-to", "type=registry,ref=gcr.io/k8s-skaffold/example:cache,mode=max", "--push", "--metadata-file", "metadata.json"},
expectedCLIFlags: []string{"--cache-from", "gcr.io/k8s-skaffold/example:cache", "--cache-to", "type=registry,ref=gcr.io/k8s-skaffold/example:cache,mode=max", "--push", "--builder", "default", "--metadata-file", "metadata.json"},
expectedEnv: []string{"KEY=VALUE", "DOCKER_BUILDKIT=1"},
},
{
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestDockerCLIBuild(t *testing.T) {
return os.Open("metadata.json")
})
t.Override(&config.GetConfigForCurrentKubectx, func(configFile string) (*config.ContextConfig, error) {
return &config.ContextConfig{CacheTag: "cache"}, nil
return &config.ContextConfig{CacheTag: "cache", BuildXBuilder: "default"}, nil
})
var mockCmd *testutil.FakeCmd

Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/config/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ContextConfig struct {
// DebugHelpersRegistry is the registry from which the debug helper images are used.
DebugHelpersRegistry string `yaml:"debug-helpers-registry,omitempty"`
CacheTag string `yaml:"cache-tag,omitempty"`
DetectBuildX *bool `yaml:"detect-buildx,omitempty"`
BuildXBuilder string `yaml:"buildx-builder,omitempty"`
UpdateCheck *bool `yaml:"update-check,omitempty"`
Survey *SurveyConfig `yaml:"survey,omitempty"`
KindDisableLoad *bool `yaml:"kind-disable-load,omitempty"`
Expand Down
16 changes: 10 additions & 6 deletions pkg/skaffold/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,19 @@ func GetCacheTag(configFile string) (string, error) {
return cfg.CacheTag, nil
}

func GetDetectBuildX(configFile string) bool {
func GetBuildXBuilder(configFile string) string {
cfg, err := GetConfigForCurrentKubectx(configFile)
if err != nil {
log.Entry(context.TODO()).Errorf("Cannot read detect-buildx option from config: %v", err)
} else if cfg.DetectBuildX != nil {
log.Entry(context.TODO()).Infof("Using detect-buildx=%t from config", *cfg.DetectBuildX)
return *cfg.DetectBuildX
log.Entry(context.TODO()).Errorf("Cannot read buildx-builder option from config: %v", err)
} else if cfg.BuildXBuilder != "" {
log.Entry(context.TODO()).Infof("Using buildx-builder=%s from config", cfg.BuildXBuilder)
return cfg.BuildXBuilder
}
return false
return ""
}

func GetDetectBuildX(configFile string) bool {
return GetBuildXBuilder(configFile) != ""
}

type GetClusterOpts struct {
Expand Down

0 comments on commit 0611204

Please sign in to comment.