Skip to content

Commit a202f68

Browse files
committed
Fix issue where default-repo wasn't being added to artifact tags (#5341)
1 parent 4b3c30f commit a202f68

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed

cmd/skaffold/app/cmd/deploy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func doDeploy(ctx context.Context, out io.Writer) error {
5959
for _, cfg := range configs {
6060
artifacts = append(artifacts, cfg.Build.Artifacts...)
6161
}
62-
buildArtifacts, err := getBuildArtifactsAndSetTags(r, artifacts)
62+
buildArtifacts, err := getBuildArtifactsAndSetTags(artifacts, r.ApplyDefaultRepo)
6363
if err != nil {
6464
tips.PrintUseRunVsDeploy(out)
6565
return err

cmd/skaffold/app/cmd/test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func doTest(ctx context.Context, out io.Writer) error {
4444
for _, c := range configs {
4545
artifacts = append(artifacts, c.Build.Artifacts...)
4646
}
47-
buildArtifacts, err := getBuildArtifactsAndSetTags(r, artifacts)
47+
buildArtifacts, err := getBuildArtifactsAndSetTags(artifacts, r.ApplyDefaultRepo)
4848
if err != nil {
4949
tips.PrintForTest(out)
5050
return err

cmd/skaffold/app/cmd/util.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,32 @@ import (
2121

2222
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
2323
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
24-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
2524
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
2625
)
2726

28-
func getBuildArtifactsAndSetTags(r runner.Runner, artifacts []*latest.Artifact) ([]build.Artifact, error) {
27+
// DefaultRepoFn takes an image tag and returns either a new tag with the default repo prefixed, or the original tag if
28+
// no default repo is specified.
29+
type DefaultRepoFn func(string) (string, error)
30+
31+
func getBuildArtifactsAndSetTags(artifacts []*latest.Artifact, defaulterFn DefaultRepoFn) ([]build.Artifact, error) {
2932
buildArtifacts, err := mergeBuildArtifacts(fromBuildOutputFile.BuildArtifacts(), preBuiltImages.Artifacts(), artifacts)
3033
if err != nil {
3134
return nil, err
3235
}
3336

34-
for _, artifact := range buildArtifacts {
35-
tag, err := r.ApplyDefaultRepo(artifact.Tag)
37+
return applyDefaultRepoToArtifacts(buildArtifacts, defaulterFn)
38+
}
39+
40+
func applyDefaultRepoToArtifacts(artifacts []build.Artifact, defaulterFn DefaultRepoFn) ([]build.Artifact, error) {
41+
for i := range artifacts {
42+
updatedTag, err := defaulterFn(artifacts[i].Tag)
3643
if err != nil {
3744
return nil, err
3845
}
39-
artifact.Tag = tag
46+
artifacts[i].Tag = updatedTag
4047
}
4148

42-
return buildArtifacts, nil
49+
return artifacts, nil
4350
}
4451

4552
func mergeBuildArtifacts(fromFile, fromCLI []build.Artifact, artifacts []*latest.Artifact) ([]build.Artifact, error) {

cmd/skaffold/app/cmd/util_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"testing"
2123

2224
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
@@ -119,3 +121,82 @@ func TestGetArtifacts(t *testing.T) {
119121
})
120122
}
121123
}
124+
125+
func Test_getBuildArtifactsAndSetTags(t *testing.T) {
126+
tests := []struct {
127+
description string
128+
artifacts []build.Artifact
129+
expected []build.Artifact
130+
defaultRepo string
131+
shouldErr bool
132+
}{
133+
{
134+
description: "no artifact without default-repo",
135+
artifacts: nil,
136+
expected: []build.Artifact(nil),
137+
},
138+
{
139+
description: "single artifact without default-repo",
140+
artifacts: []build.Artifact{{ImageName: "image", Tag: "image:tag"}},
141+
expected: []build.Artifact{{ImageName: "image", Tag: "image:tag"}},
142+
},
143+
{
144+
description: "multiple artifacts without default-repo",
145+
artifacts: []build.Artifact{
146+
{ImageName: "image1", Tag: "image1:tag"},
147+
{ImageName: "image1", Tag: "image1:tag"},
148+
},
149+
expected: []build.Artifact{
150+
{ImageName: "image1", Tag: "image1:tag"},
151+
{ImageName: "image1", Tag: "image1:tag"},
152+
},
153+
},
154+
{
155+
description: "single artifact with default-repo",
156+
artifacts: []build.Artifact{{ImageName: "image", Tag: "image:tag"}},
157+
expected: []build.Artifact{{ImageName: "image", Tag: "example.com/test-repo/image:tag"}},
158+
defaultRepo: "example.com/test-repo",
159+
},
160+
{
161+
description: "multiple artifacts with default-repo",
162+
artifacts: []build.Artifact{
163+
{ImageName: "image1", Tag: "image1:tag"},
164+
{ImageName: "image1", Tag: "image1:tag"},
165+
},
166+
expected: []build.Artifact{
167+
{ImageName: "image1", Tag: "example.com/test-repo/image1:tag"},
168+
{ImageName: "image1", Tag: "example.com/test-repo/image1:tag"},
169+
},
170+
defaultRepo: "example.com/test-repo",
171+
},
172+
{
173+
description: "multiple artifacts with erring default-repo",
174+
artifacts: []build.Artifact{
175+
{ImageName: "image1", Tag: "image1:tag"},
176+
{ImageName: "image1", Tag: "image1:tag"},
177+
},
178+
expected: []build.Artifact(nil),
179+
defaultRepo: "example.com/test-repo",
180+
shouldErr: true,
181+
},
182+
}
183+
for _, test := range tests {
184+
testutil.Run(t, test.description, func(t *testutil.T) {
185+
artifacts, err := applyDefaultRepoToArtifacts(test.artifacts, func(s string) (string, error) {
186+
if test.shouldErr {
187+
// this seems counter-intuitive that we explicitly return an error when shouldErr is true,
188+
// however this function is a callback, the test is ensuring the error from the callback is handled
189+
// correctly
190+
return "", errors.New("error")
191+
}
192+
193+
if test.defaultRepo == "" {
194+
return s, nil
195+
}
196+
197+
return fmt.Sprintf("%s/%s", test.defaultRepo, s), nil
198+
})
199+
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, artifacts)
200+
})
201+
}
202+
}

0 commit comments

Comments
 (0)