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

Support TemplateField for build.artifacts.docker.cliFlags #9582

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions pkg/skaffold/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func (b *Builder) dockerCLIBuild(ctx context.Context, out io.Writer, name string
if err != nil {
return "", fmt.Errorf("unable to evaluate build args: %w", err)
}
for i, cliFlag := range a.CliFlags {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that can be moved to where the other cli args are built: https://github.com/GoogleContainerTools/skaffold/blob/main/pkg/skaffold/docker/image.go#L629? I believe that would also add support for the GCB case.

a.CliFlags[i], err = util.ExpandEnvTemplate(cliFlag, imageInfoEnv)
if err != nil {
return "", fmt.Errorf("unable to evaluate cli flags: %w", err)
}
}
cliArgs, err := docker.ToCLIBuildArgs(a, ba)
if err != nil {
return "", fmt.Errorf("getting docker build args: %w", err)
Expand Down
38 changes: 26 additions & 12 deletions pkg/skaffold/build/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestDockerCLIBuild(t *testing.T) {
cliFlags []string // CLI flags to pass through to command.
cfg mockConfig
extraEnv []string
imageName string
err error
expectedEnv []string
expectedCLIFlags []string // CLI flags expected to be autogenerated.
Expand Down Expand Up @@ -79,11 +80,20 @@ func TestDockerCLIBuild(t *testing.T) {
expectedEnv: []string{"KEY=VALUE", "DOCKER_BUILDKIT=1"},
},
{
description: "cliFlags",
cliFlags: []string{"--platform", "linux/amd64"},
localBuild: latest.LocalBuild{},
wantDockerCLI: true,
expectedEnv: []string{"KEY=VALUE"},
description: "cliFlags",
cliFlags: []string{"--platform", "linux/amd64"},
localBuild: latest.LocalBuild{},
wantDockerCLI: true,
expectedCLIFlags: []string{"--platform", "linux/amd64"},
expectedEnv: []string{"KEY=VALUE"},
},
{
description: "cliFlags replace template",
imageName: "docker.io/library/image:tag",
cliFlags: []string{"--cache-to=type=registry,ref={{ .IMAGE_REPO }}/cache-image:cache"},
localBuild: latest.LocalBuild{},
wantDockerCLI: true,
expectedCLIFlags: []string{"--cache-to=type=registry,ref=docker.io/library/cache-image:cache"},
},
{
description: "buildkit and extra env",
Expand Down Expand Up @@ -151,23 +161,26 @@ func TestDockerCLIBuild(t *testing.T) {
t.Override(&docker.DefaultAuthHelper, stubAuth{})
var mockCmd *testutil.FakeCmd

imageName := "tag"
if test.imageName != "" {
imageName = test.imageName
}

if test.err != nil {
var pruneFlag string
if test.cfg.Prune() {
pruneFlag = " --force-rm"
}
mockCmd = testutil.CmdRunErr(
"docker build . --file "+dockerfilePath+" -t tag"+pruneFlag,
"docker build . --file "+dockerfilePath+" -t "+imageName+pruneFlag,
test.err,
)
t.Override(&util.DefaultExecCommand, mockCmd)
}
if test.wantDockerCLI {
cmdLine := "docker build . --file " + dockerfilePath + " -t tag"
if len(test.cliFlags) > 0 || len(test.expectedCLIFlags) > 0 {
flags := append(test.cliFlags, test.expectedCLIFlags...)

cmdLine += " " + strings.Join(flags, " ")
cmdLine := "docker build . --file " + dockerfilePath + " -t " + imageName
if len(test.expectedCLIFlags) > 0 {
cmdLine += " " + strings.Join(test.expectedCLIFlags, " ")
}
mockCmd = testutil.CmdRunEnv(cmdLine, test.expectedEnv)
t.Override(&util.DefaultExecCommand, mockCmd)
Expand All @@ -186,7 +199,7 @@ func TestDockerCLIBuild(t *testing.T) {
},
}

_, err := builder.Build(context.Background(), io.Discard, artifact, "tag", platform.Matcher{})
_, err := builder.Build(context.Background(), io.Discard, artifact, imageName, platform.Matcher{})
t.CheckError(test.err != nil, err)
if mockCmd != nil {
t.CheckDeepEqual(1, mockCmd.TimesCalled())
Expand Down Expand Up @@ -283,6 +296,7 @@ type stubAuth struct{}
func (t stubAuth) GetAuthConfig(context.Context, string) (registry.AuthConfig, error) {
return registry.AuthConfig{}, nil
}

func (t stubAuth) GetAllAuthConfigs(context.Context) (map[string]registry.AuthConfig, error) {
return nil, nil
}
Expand Down