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

Merged
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
2 changes: 1 addition & 1 deletion pkg/skaffold/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ 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)
}
cliArgs, err := docker.ToCLIBuildArgs(a, ba)
cliArgs, err := docker.ToCLIBuildArgs(a, ba, imageInfoEnv)
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
2 changes: 1 addition & 1 deletion pkg/skaffold/build/gcb/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (b *Builder) dockerBuildArgs(a *latest.Artifact, tag string, deps []*latest
return nil, fmt.Errorf("unable to evaluate build args: %w", err)
}

ba, err := docker.ToCLIBuildArgs(d, buildArgs)
ba, err := docker.ToCLIBuildArgs(d, buildArgs, nil)
if err != nil {
return nil, fmt.Errorf("getting docker build args: %w", err)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ func (l *localDaemon) DiskUsage(ctx context.Context) (uint64, error) {
return uint64(usage.LayersSize), nil
}

func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string) ([]string, error) {
func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string, env map[string]string) ([]string, error) {
var args []string
var keys []string
for k := range evaluatedArgs {
Expand All @@ -653,7 +653,13 @@ func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string)
args = append(args, "--cache-from", from)
}

args = append(args, a.CliFlags...)
for _, cliFlag := range a.CliFlags {
cliFlag, err := util.ExpandEnvTemplate(cliFlag, env)
if err != nil {
return nil, fmt.Errorf("unable to evaluate cli flags: %w", err)
}
args = append(args, cliFlag)
}

if a.Target != "" {
args = append(args, "--target", a.Target)
Expand Down
10 changes: 9 additions & 1 deletion pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ func TestGetBuildArgs(t *testing.T) {
},
want: []string{"--foo", "--bar"},
},
{
description: "expand env for CLI flags",
artifact: &latest.DockerArtifact{
CliFlags: []string{"--cache-to=type=registry,ref={{ .IMAGE_REPO }}/cache-image:cache"},
},
env: []string{"IMAGE_REPO=docker.io/library"},
want: []string{"--cache-to=type=registry,ref=docker.io/library/cache-image:cache"},
},
{
description: "target",
artifact: &latest.DockerArtifact{
Expand Down Expand Up @@ -430,7 +438,7 @@ func TestGetBuildArgs(t *testing.T) {
return
}

result, err := ToCLIBuildArgs(test.artifact, args)
result, err := ToCLIBuildArgs(test.artifact, args, nil)

t.CheckError(test.shouldErr, err)
if !test.shouldErr {
Expand Down
Loading