Skip to content

Add --target parameter with kaniko on Google Cloud Build #3462

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

Merged
merged 3 commits into from
Jan 2, 2020
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/gcb/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (b *Builder) kanikoBuildSpec(artifact *latest.KanikoArtifact, tag string) (
kanikoArgs = append(kanikoArgs, "--reproducible")
}

if artifact.Target != "" {
kanikoArgs = append(kanikoArgs, "--target", artifact.Target)
}

steps := []*cloudbuild.BuildStep{
{
Name: b.KanikoImage,
Expand Down
113 changes: 84 additions & 29 deletions pkg/skaffold/build/gcb/kaniko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,60 @@ import (
)

func TestKanikoBuildSpec(t *testing.T) {
artifact := &latest.Artifact{
ArtifactType: latest.ArtifactType{
KanikoArtifact: &latest.KanikoArtifact{
tests := []struct {
description string
artifact *latest.KanikoArtifact
expectedArgs []string
}{
{
description: "simple build",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
},
expectedArgs: []string{},
},
{
description: "with BuildArgs",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
BuildArgs: map[string]*string{
"arg1": util.StringPtr("value1"),
"arg2": nil,
},
Cache: &latest.KanikoCache{},
},
expectedArgs: []string{
"--build-arg", "arg1=value1",
"--build-arg", "arg2",
},
},
{
description: "with cache layer",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
Cache: &latest.KanikoCache{},
},
expectedArgs: []string{
"--cache",
},
},
{
description: "with reproduceible",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
Reproducible: true,
},
expectedArgs: []string{
"--reproducible",
},
},
{
description: "with target",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
Target: "builder",
},
expectedArgs: []string{
"--target", "builder",
},
},
}
Expand All @@ -46,32 +91,42 @@ func TestKanikoBuildSpec(t *testing.T) {
MachineType: "n1-standard-1",
Timeout: "10m",
})
desc, err := builder.buildSpec(artifact, "nginx", "bucket", "object")

expected := cloudbuild.Build{
LogsBucket: "bucket",
Source: &cloudbuild.Source{
StorageSource: &cloudbuild.StorageSource{
Bucket: "bucket",
Object: "object",
},
},
Steps: []*cloudbuild.BuildStep{{
Name: "gcr.io/kaniko-project/executor",
Args: []string{
"--destination", "nginx",
"--dockerfile", "Dockerfile",
"--build-arg", "arg1=value1",
"--build-arg", "arg2",
"--cache",
},
}},
Options: &cloudbuild.BuildOptions{
DiskSizeGb: 100,
MachineType: "n1-standard-1",
},
Timeout: "10m",
defaultExpectedArgs := []string{
"--destination", "nginx",
"--dockerfile", "Dockerfile",
}

testutil.CheckErrorAndDeepEqual(t, false, err, expected, desc)
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
artifact := &latest.Artifact{
ArtifactType: latest.ArtifactType{
KanikoArtifact: test.artifact,
},
}

desc, err := builder.buildSpec(artifact, "nginx", "bucket", "object")

expected := cloudbuild.Build{
LogsBucket: "bucket",
Source: &cloudbuild.Source{
StorageSource: &cloudbuild.StorageSource{
Bucket: "bucket",
Object: "object",
},
},
Steps: []*cloudbuild.BuildStep{{
Name: "gcr.io/kaniko-project/executor",
Args: append(defaultExpectedArgs, test.expectedArgs...),
}},
Options: &cloudbuild.BuildOptions{
DiskSizeGb: 100,
MachineType: "n1-standard-1",
},
Timeout: "10m",
}

t.CheckErrorAndDeepEqual(false, err, expected, desc)
})
}
}