Skip to content

Commit fafdcd7

Browse files
authored
Ensure tag stripping logic can optionally accept digests (#5919)
1 parent e94297b commit fafdcd7

File tree

7 files changed

+23
-10
lines changed

7 files changed

+23
-10
lines changed

pkg/skaffold/initializer/build/builders_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func TestStripImageTags(t *testing.T) {
375375
fakeWarner := &warnings.Collect{}
376376
t.Override(&warnings.Printf, fakeWarner.Warnf)
377377

378-
images := tag.StripTags(test.taggedImages)
378+
images := tag.StripTags(test.taggedImages, true)
379379

380380
t.CheckDeepEqual(test.expectedImages, images)
381381
t.CheckDeepEqual(test.expectedWarnings, fakeWarner.Warnings)

pkg/skaffold/initializer/build/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
func matchBuildersToImages(builders []InitBuilder, images []string) ([]ArtifactInfo, []InitBuilder, []string) {
27-
images = tag.StripTags(images)
27+
images = tag.StripTags(images, true)
2828

2929
var artifactInfos []ArtifactInfo
3030
var unresolvedImages = make(sortedSet)

pkg/skaffold/kubernetes/colorpicker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NewColorPicker(imageNames []string) ColorPicker {
5656
imageColors := make(map[string]output.Color)
5757

5858
for i, imageName := range imageNames {
59-
imageColors[tag.StripTag(imageName)] = colorCodes[i%len(colorCodes)]
59+
imageColors[tag.StripTag(imageName, false)] = colorCodes[i%len(colorCodes)]
6060
}
6161

6262
return &colorPicker{
@@ -69,7 +69,7 @@ func NewColorPicker(imageNames []string) ColorPicker {
6969
// write with no formatting.
7070
func (p *colorPicker) Pick(pod *v1.Pod) output.Color {
7171
for _, container := range pod.Spec.Containers {
72-
if c, present := p.imageColors[tag.StripTag(container.Image)]; present {
72+
if c, present := p.imageColors[tag.StripTag(container.Image, false)]; present {
7373
return c
7474
}
7575
}

pkg/skaffold/kubernetes/colorpicker_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func TestColorPicker(t *testing.T) {
6969
},
7070
expectedColor: colorCodes[1],
7171
},
72+
{
73+
description: "accept image with digest",
74+
pod: &v1.Pod{
75+
Spec: v1.PodSpec{
76+
Containers: []v1.Container{
77+
{Image: "second:tag@sha256:d3f4dd1541ee34b96850efc46955bada1a415b0594dc9948607c0197d2d16749"},
78+
},
79+
},
80+
},
81+
expectedColor: colorCodes[1],
82+
},
7283
}
7384

7485
picker := NewColorPicker([]string{"image:ignored", "second"})

pkg/skaffold/kubernetes/logger/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (a *LogAggregator) prefix(pod *v1.Pod, container v1.ContainerStatus) string
186186
var c latestV1.Pipeline
187187
var present bool
188188
for _, container := range pod.Spec.Containers {
189-
if c, present = a.config.PipelineForImage(tag.StripTag(container.Image)); present {
189+
if c, present = a.config.PipelineForImage(tag.StripTag(container.Image, false)); present {
190190
break
191191
}
192192
}

pkg/skaffold/tag/util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@ import (
2121
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/warnings"
2222
)
2323

24-
func StripTags(taggedImages []string) []string {
24+
func StripTags(taggedImages []string, ignoreDigest bool) []string {
2525
// Remove tags from image names
2626
var images []string
2727
for _, image := range taggedImages {
28-
tag := StripTag(image)
28+
tag := StripTag(image, ignoreDigest)
2929
if tag != "" {
3030
images = append(images, tag)
3131
}
3232
}
3333
return images
3434
}
3535

36-
func StripTag(image string) string {
36+
func StripTag(image string, ignoreDigest bool) string {
3737
parsed, err := docker.ParseReference(image)
3838
if err != nil {
3939
// It's possible that it's a templatized name that can't be parsed as is.
4040
warnings.Printf("Couldn't parse image [%s]: %s", image, err.Error())
4141
return ""
4242
}
43-
if parsed.Digest != "" {
43+
if ignoreDigest && parsed.Digest != "" {
4444
warnings.Printf("Ignoring image referenced by digest: [%s]", image)
4545
return ""
4646
}

pkg/skaffold/tag/util_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestStripTags(t *testing.T) {
2727
name string
2828
images []string
2929
expectedImages []string
30+
ignoreDigest bool
3031
}{
3132
{
3233
name: "latest",
@@ -47,14 +48,15 @@ func TestStripTags(t *testing.T) {
4748
name: "ignore digest",
4849
images: []string{"foo:sha256@deadbeef"},
4950
expectedImages: nil,
51+
ignoreDigest: true,
5052
},
5153
}
5254

5355
for _, test := range tests {
5456
testutil.Run(t, test.name, func(t *testutil.T) {
5557
t.Parallel()
5658

57-
i := StripTags(test.images)
59+
i := StripTags(test.images, test.ignoreDigest)
5860
t.CheckDeepEqual(test.expectedImages, i)
5961
})
6062
}

0 commit comments

Comments
 (0)