Skip to content

Commit 6686c9e

Browse files
authored
fix: Improper parsing of image identifiers having both tag name and digest (#815)
Signed-off-by: Cheng Fang <[email protected]>
1 parent 9321f9c commit 6686c9e

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

pkg/image/image.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"strings"
55
"time"
66

7+
"github.com/distribution/distribution/v3/reference"
8+
79
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
810
"github.com/argoproj-labs/argocd-image-updater/pkg/tag"
9-
10-
"github.com/distribution/distribution/v3/reference"
1111
)
1212

1313
type ContainerImage struct {
@@ -95,12 +95,13 @@ func (img *ContainerImage) GetFullNameWithTag() string {
9595
}
9696
str += img.ImageName
9797
if img.ImageTag != nil {
98+
if img.ImageTag.TagName != "" {
99+
str += ":"
100+
str += img.ImageTag.TagName
101+
}
98102
if img.ImageTag.TagDigest != "" {
99103
str += "@"
100104
str += img.ImageTag.TagDigest
101-
} else if img.ImageTag.TagName != "" {
102-
str += ":"
103-
str += img.ImageTag.TagName
104105
}
105106
}
106107
return str
@@ -223,10 +224,22 @@ func getImageTagFromIdentifier(identifier string) (string, string, *tag.ImageTag
223224
imageString = strings.Join(comp[1:], "/")
224225
}
225226

226-
// We can either have a tag name or a digest reference
227+
// We can either have a tag name or a digest reference, or both
228+
// jannfis/test-image:0.1
229+
// gcr.io/jannfis/test-image:0.1
230+
// gcr.io/jannfis/test-image@sha256:abcde
231+
// gcr.io/jannfis/test-image:test-tag@sha256:abcde
227232
if strings.Contains(imageString, "@") {
228233
comp = strings.SplitN(imageString, "@", 2)
229-
return sourceName, comp[0], tag.NewImageTag("", time.Unix(0, 0), comp[1])
234+
colonPos := strings.LastIndex(comp[0], ":")
235+
slashPos := strings.LastIndex(comp[0], "/")
236+
if colonPos > slashPos {
237+
// first half (before @) contains image and tag name
238+
return sourceName, comp[0][:colonPos], tag.NewImageTag(comp[0][colonPos+1:], time.Unix(0, 0), comp[1])
239+
} else {
240+
// first half contains image name without tag name
241+
return sourceName, comp[0], tag.NewImageTag("", time.Unix(0, 0), comp[1])
242+
}
230243
} else {
231244
comp = strings.SplitN(imageString, ":", 2)
232245
if len(comp) != 2 {

pkg/image/image_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ func Test_ParseImageTags(t *testing.T) {
8181
t.Run("Parse valid image name with tag and digest", func(t *testing.T) {
8282
image := NewFromIdentifier("gcr.io/jannfis/test-image:test-tag@sha256:abcde")
8383
require.NotNil(t, image.ImageTag)
84-
assert.Empty(t, image.ImageTag.TagName)
84+
assert.Equal(t, "test-tag", image.ImageTag.TagName)
8585
assert.Equal(t, "sha256:abcde", image.ImageTag.TagDigest)
86-
assert.Equal(t, "latest@sha256:abcde", image.GetTagWithDigest())
86+
assert.Equal(t, "test-tag@sha256:abcde", image.GetTagWithDigest())
87+
assert.Equal(t, "gcr.io/jannfis/test-image", image.GetFullNameWithoutTag())
8788
assert.Equal(t, "gcr.io/jannfis/test-image:test-tag@sha256:abcde", image.GetFullNameWithTag())
8889
})
8990

0 commit comments

Comments
 (0)