From 32de0343adf02f1539a84bb1b92fe386d53b36b3 Mon Sep 17 00:00:00 2001 From: dlorenc Date: Sat, 11 Aug 2018 11:17:29 -0700 Subject: [PATCH] Add the --target flag as a parameter to the docker builder. --- examples/annotated-skaffold.yaml | 2 + pkg/skaffold/build/local/docker.go | 3 -- pkg/skaffold/docker/image.go | 9 ++++ pkg/skaffold/docker/image_test.go | 68 +++++++++++++++++++++----- pkg/skaffold/schema/v1alpha2/config.go | 1 + 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/examples/annotated-skaffold.yaml b/examples/annotated-skaffold.yaml index 89c139cc607..0742ae321ab 100644 --- a/examples/annotated-skaffold.yaml +++ b/examples/annotated-skaffold.yaml @@ -53,6 +53,8 @@ build: cacheFrom: - image1 - image2 + # Dockerfile target name to build. + target: stageName # bazel requires bazel CLI to be installed and the artifacts sources to # contain Bazel configuration files. diff --git a/pkg/skaffold/build/local/docker.go b/pkg/skaffold/build/local/docker.go index ebb68abf67e..5ff2eb3ddeb 100644 --- a/pkg/skaffold/build/local/docker.go +++ b/pkg/skaffold/build/local/docker.go @@ -40,9 +40,6 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri args := []string{"build", workspace, "--file", dockerfilePath, "-t", initialTag} args = append(args, docker.GetBuildArgs(a)...) - for _, from := range a.CacheFrom { - args = append(args, "--cache-from", from) - } cmd := exec.Command("docker", args...) if b.cfg.UseBuildkit { diff --git a/pkg/skaffold/docker/image.go b/pkg/skaffold/docker/image.go index 799c69b5205..44365a0d530 100644 --- a/pkg/skaffold/docker/image.go +++ b/pkg/skaffold/docker/image.go @@ -66,6 +66,7 @@ func BuildArtifact(ctx context.Context, out io.Writer, cli APIClient, workspace BuildArgs: a.BuildArgs, CacheFrom: a.CacheFrom, AuthConfigs: authConfigs, + Target: a.Target, }) if err != nil { return errors.Wrap(err, "docker build") @@ -194,5 +195,13 @@ func GetBuildArgs(a *v1alpha2.DockerArtifact) []string { } } + for _, from := range a.CacheFrom { + args = append(args, "--cache-from", from) + } + + if a.Target != "" { + args = append(args, "--target", a.Target) + } + return args } diff --git a/pkg/skaffold/docker/image_test.go b/pkg/skaffold/docker/image_test.go index e147fc1bfb5..50d15c1799d 100644 --- a/pkg/skaffold/docker/image_test.go +++ b/pkg/skaffold/docker/image_test.go @@ -171,18 +171,64 @@ func TestDigest(t *testing.T) { } func TestGetBuildArgs(t *testing.T) { - artifact := &v1alpha2.DockerArtifact{ - BuildArgs: map[string]*string{ - "key1": util.StringPtr("value1"), - "key2": nil, + type args struct { + a *v1alpha2.DockerArtifact + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "build args", + args: args{ + a: &v1alpha2.DockerArtifact{ + BuildArgs: map[string]*string{ + "key1": util.StringPtr("value1"), + "key2": nil, + }, + }, + }, + want: []string{"--build-arg", "key1=value1", "--build-arg", "key2"}, + }, + { + name: "cache from", + args: args{ + a: &v1alpha2.DockerArtifact{ + CacheFrom: []string{"gcr.io/foo/bar", "baz:latest"}, + }, + }, + want: []string{"--cache-from", "gcr.io/foo/bar", "--cache-from", "baz:latest"}, + }, + { + name: "target", + args: args{ + a: &v1alpha2.DockerArtifact{ + Target: "stage1", + }, + }, + want: []string{"--target", "stage1"}, + }, + { + name: "all", + args: args{ + a: &v1alpha2.DockerArtifact{ + BuildArgs: map[string]*string{ + "key1": util.StringPtr("value1"), + }, + CacheFrom: []string{"foo"}, + Target: "stage1", + }, + }, + want: []string{"--build-arg", "key1=value1", "--cache-from", "foo", "--target", "stage1"}, }, } - - arg := GetBuildArgs(artifact) - expected := []string{"--build-arg", "key1=value1", "--build-arg", "key2"} - - if diff := cmp.Diff(arg, expected); diff != "" { - t.Errorf("%T differ (-got, +want): %s", expected, diff) - return + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := GetBuildArgs(tt.args.a) + if diff := cmp.Diff(result, tt.want); diff != "" { + t.Errorf("%T differ (-got, +want): %s", tt.want, diff) + } + }) } } diff --git a/pkg/skaffold/schema/v1alpha2/config.go b/pkg/skaffold/schema/v1alpha2/config.go index c2b2fea0b69..0e8506e6fc1 100644 --- a/pkg/skaffold/schema/v1alpha2/config.go +++ b/pkg/skaffold/schema/v1alpha2/config.go @@ -210,6 +210,7 @@ type DockerArtifact struct { DockerfilePath string `yaml:"dockerfilePath,omitempty"` BuildArgs map[string]*string `yaml:"buildArgs,omitempty"` CacheFrom []string `yaml:"cacheFrom,omitempty"` + Target string `yaml:"target,omitempty"` } type BazelArtifact struct {