Skip to content

Commit 56e77e7

Browse files
hwanjin-jeongdgageot
authored andcommitted
Add --target parameter with kaniko on Google Cloud Build (#3462)
* Add --target parameter with kaniko on Google Cloud Build * modify: append target args when using target parameter * refactoring test of kaniko build spec
1 parent 9356290 commit 56e77e7

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed

pkg/skaffold/build/gcb/kaniko.go

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func (b *Builder) kanikoBuildSpec(artifact *latest.KanikoArtifact, tag string) (
5151
kanikoArgs = append(kanikoArgs, "--reproducible")
5252
}
5353

54+
if artifact.Target != "" {
55+
kanikoArgs = append(kanikoArgs, "--target", artifact.Target)
56+
}
57+
5458
steps := []*cloudbuild.BuildStep{
5559
{
5660
Name: b.KanikoImage,

pkg/skaffold/build/gcb/kaniko_test.go

+84-29
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,60 @@ import (
2727
)
2828

2929
func TestKanikoBuildSpec(t *testing.T) {
30-
artifact := &latest.Artifact{
31-
ArtifactType: latest.ArtifactType{
32-
KanikoArtifact: &latest.KanikoArtifact{
30+
tests := []struct {
31+
description string
32+
artifact *latest.KanikoArtifact
33+
expectedArgs []string
34+
}{
35+
{
36+
description: "simple build",
37+
artifact: &latest.KanikoArtifact{
38+
DockerfilePath: "Dockerfile",
39+
},
40+
expectedArgs: []string{},
41+
},
42+
{
43+
description: "with BuildArgs",
44+
artifact: &latest.KanikoArtifact{
3345
DockerfilePath: "Dockerfile",
3446
BuildArgs: map[string]*string{
3547
"arg1": util.StringPtr("value1"),
3648
"arg2": nil,
3749
},
38-
Cache: &latest.KanikoCache{},
50+
},
51+
expectedArgs: []string{
52+
"--build-arg", "arg1=value1",
53+
"--build-arg", "arg2",
54+
},
55+
},
56+
{
57+
description: "with cache layer",
58+
artifact: &latest.KanikoArtifact{
59+
DockerfilePath: "Dockerfile",
60+
Cache: &latest.KanikoCache{},
61+
},
62+
expectedArgs: []string{
63+
"--cache",
64+
},
65+
},
66+
{
67+
description: "with reproduceible",
68+
artifact: &latest.KanikoArtifact{
69+
DockerfilePath: "Dockerfile",
70+
Reproducible: true,
71+
},
72+
expectedArgs: []string{
73+
"--reproducible",
74+
},
75+
},
76+
{
77+
description: "with target",
78+
artifact: &latest.KanikoArtifact{
79+
DockerfilePath: "Dockerfile",
80+
Target: "builder",
81+
},
82+
expectedArgs: []string{
83+
"--target", "builder",
3984
},
4085
},
4186
}
@@ -46,32 +91,42 @@ func TestKanikoBuildSpec(t *testing.T) {
4691
MachineType: "n1-standard-1",
4792
Timeout: "10m",
4893
})
49-
desc, err := builder.buildSpec(artifact, "nginx", "bucket", "object")
5094

51-
expected := cloudbuild.Build{
52-
LogsBucket: "bucket",
53-
Source: &cloudbuild.Source{
54-
StorageSource: &cloudbuild.StorageSource{
55-
Bucket: "bucket",
56-
Object: "object",
57-
},
58-
},
59-
Steps: []*cloudbuild.BuildStep{{
60-
Name: "gcr.io/kaniko-project/executor",
61-
Args: []string{
62-
"--destination", "nginx",
63-
"--dockerfile", "Dockerfile",
64-
"--build-arg", "arg1=value1",
65-
"--build-arg", "arg2",
66-
"--cache",
67-
},
68-
}},
69-
Options: &cloudbuild.BuildOptions{
70-
DiskSizeGb: 100,
71-
MachineType: "n1-standard-1",
72-
},
73-
Timeout: "10m",
95+
defaultExpectedArgs := []string{
96+
"--destination", "nginx",
97+
"--dockerfile", "Dockerfile",
7498
}
7599

76-
testutil.CheckErrorAndDeepEqual(t, false, err, expected, desc)
100+
for _, test := range tests {
101+
testutil.Run(t, test.description, func(t *testutil.T) {
102+
artifact := &latest.Artifact{
103+
ArtifactType: latest.ArtifactType{
104+
KanikoArtifact: test.artifact,
105+
},
106+
}
107+
108+
desc, err := builder.buildSpec(artifact, "nginx", "bucket", "object")
109+
110+
expected := cloudbuild.Build{
111+
LogsBucket: "bucket",
112+
Source: &cloudbuild.Source{
113+
StorageSource: &cloudbuild.StorageSource{
114+
Bucket: "bucket",
115+
Object: "object",
116+
},
117+
},
118+
Steps: []*cloudbuild.BuildStep{{
119+
Name: "gcr.io/kaniko-project/executor",
120+
Args: append(defaultExpectedArgs, test.expectedArgs...),
121+
}},
122+
Options: &cloudbuild.BuildOptions{
123+
DiskSizeGb: 100,
124+
MachineType: "n1-standard-1",
125+
},
126+
Timeout: "10m",
127+
}
128+
129+
t.CheckErrorAndDeepEqual(false, err, expected, desc)
130+
})
131+
}
77132
}

0 commit comments

Comments
 (0)