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

fix: add --load flag for local buildkit #9387

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading