Skip to content

Commit

Permalink
fix: add --load flag for local buildkit (GoogleContainerTools#9387)
Browse files Browse the repository at this point in the history
* fix: add --load flag for local buildkit

Adds the --load flag when building images using buildkit, and not
pushing to a remote registry.

This is required, to get them loaded into the local docker context.

* test: add test for BuildKit --load flag

Adds testing to ensure that the --load flag is applied correctly.
  • Loading branch information
Crystalix007 authored Apr 17, 2024
1 parent f69774e commit a2f4043
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 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 @@ -120,6 +120,10 @@ func (b *Builder) dockerCLIBuild(ctx context.Context, out io.Writer, name string
args = append(args, "--platform", pl.String())
}

if b.useBuildKit != nil && *b.useBuildKit && !b.pushImages {
args = append(args, "--load")
}

cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Env = append(util.OSEnviron(), b.localDocker.ExtraEnv()...)
if b.useBuildKit != nil {
Expand Down
63 changes: 38 additions & 25 deletions pkg/skaffold/build/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ import (

func TestDockerCLIBuild(t *testing.T) {
tests := []struct {
description string
localBuild latest.LocalBuild
cliFlags []string
cfg mockConfig
extraEnv []string
expectedEnv []string
err error
expectedErr error
wantDockerCLI bool
expectedErrCode proto.StatusCode
description string
localBuild latest.LocalBuild
cliFlags []string // CLI flags to pass through to command.
cfg mockConfig
extraEnv []string
err error
expectedEnv []string
expectedCLIFlags []string // CLI flags expected to be autogenerated.
expectedErr error
wantDockerCLI bool
expectedErrCode proto.StatusCode
}{
{
description: "docker build",
Expand All @@ -64,10 +65,18 @@ func TestDockerCLIBuild(t *testing.T) {
expectedEnv: []string{"KEY=VALUE", "OTHER=VALUE"},
},
{
description: "buildkit",
localBuild: latest.LocalBuild{UseBuildkit: util.Ptr(true)},
wantDockerCLI: true,
expectedEnv: []string{"KEY=VALUE", "DOCKER_BUILDKIT=1"},
description: "buildkit",
localBuild: latest.LocalBuild{UseBuildkit: util.Ptr(true)},
wantDockerCLI: true,
expectedCLIFlags: []string{"--load"},
expectedEnv: []string{"KEY=VALUE", "DOCKER_BUILDKIT=1"},
},
{
description: "buildkit push",
localBuild: latest.LocalBuild{UseBuildkit: util.Ptr(true), Push: util.Ptr(true)},
wantDockerCLI: true,
expectedCLIFlags: []string{"--load"},
expectedEnv: []string{"KEY=VALUE", "DOCKER_BUILDKIT=1"},
},
{
description: "cliFlags",
Expand All @@ -77,17 +86,19 @@ func TestDockerCLIBuild(t *testing.T) {
expectedEnv: []string{"KEY=VALUE"},
},
{
description: "buildkit and extra env",
localBuild: latest.LocalBuild{UseBuildkit: util.Ptr(true)},
wantDockerCLI: true,
extraEnv: []string{"OTHER=VALUE"},
expectedEnv: []string{"KEY=VALUE", "OTHER=VALUE", "DOCKER_BUILDKIT=1"},
description: "buildkit and extra env",
localBuild: latest.LocalBuild{UseBuildkit: util.Ptr(true)},
wantDockerCLI: true,
extraEnv: []string{"OTHER=VALUE"},
expectedCLIFlags: []string{"--load"},
expectedEnv: []string{"KEY=VALUE", "OTHER=VALUE", "DOCKER_BUILDKIT=1"},
},
{
description: "env var collisions",
localBuild: latest.LocalBuild{UseBuildkit: util.Ptr(true)},
wantDockerCLI: true,
extraEnv: []string{"KEY=OTHER_VALUE", "DOCKER_BUILDKIT=0"},
description: "env var collisions",
localBuild: latest.LocalBuild{UseBuildkit: util.Ptr(true)},
wantDockerCLI: true,
extraEnv: []string{"KEY=OTHER_VALUE", "DOCKER_BUILDKIT=0"},
expectedCLIFlags: []string{"--load"},
// DOCKER_BUILDKIT should be overridden
expectedEnv: []string{"KEY=OTHER_VALUE", "DOCKER_BUILDKIT=1"},
},
Expand Down Expand Up @@ -153,8 +164,10 @@ func TestDockerCLIBuild(t *testing.T) {
}
if test.wantDockerCLI {
cmdLine := "docker build . --file " + dockerfilePath + " -t tag"
if len(test.cliFlags) > 0 {
cmdLine += " " + strings.Join(test.cliFlags, " ")
if len(test.cliFlags) > 0 || len(test.expectedCLIFlags) > 0 {
flags := append(test.cliFlags, test.expectedCLIFlags...)

cmdLine += " " + strings.Join(flags, " ")
}
mockCmd = testutil.CmdRunEnv(cmdLine, test.expectedEnv)
t.Override(&util.DefaultExecCommand, mockCmd)
Expand Down

0 comments on commit a2f4043

Please sign in to comment.