Skip to content
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

fix: use distribution to parse references #275

Merged
merged 4 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions pkg/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"time"

"github.com/argoproj-labs/argocd-image-updater/pkg/tag"

"github.com/distribution/distribution/v3/reference"
)

type ContainerImage struct {
Expand All @@ -22,6 +24,36 @@ type ContainerImageList []*ContainerImage

// NewFromIdentifier parses an image identifier and returns a populated ContainerImage
func NewFromIdentifier(identifier string) *ContainerImage {
imgRef := identifier
alias := ""
if strings.Contains(identifier, "=") {
n := strings.SplitN(identifier, "=", 2)
imgRef = n[1]
alias = n[0]
}
if parsed, err := reference.ParseNormalizedNamed(imgRef); err == nil {
img := ContainerImage{}
img.RegistryURL = reference.Domain(parsed)
// remove default registry for backwards-compatibility
if img.RegistryURL == "docker.io" && !strings.HasPrefix(imgRef, "docker.io") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jannfis this was my workaround. If docker.io was explicity the registry, retain it, else remove it. Is there a case I’m missing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, now I got it. Sorry, I was a little confused and missed the check for the prefix on original image ref.

OK, so if the parsed registry domain is docker.io, but the original image ref is not prefixed with docker.io, we assume default registry and set it to blank. Yes, that makes sense and would restore original behavior. Thanks for clarifying it.

img.RegistryURL = ""
}
img.ImageAlias = alias
img.ImageName = reference.Path(parsed)
if digested, ok := parsed.(reference.Digested); ok {
img.ImageTag = &tag.ImageTag{
TagDigest: string(digested.Digest()),
}
} else if tagged, ok := parsed.(reference.Tagged); ok {
img.ImageTag = &tag.ImageTag{
TagName: tagged.Tag(),
}
}
img.original = identifier
return &img
}

// if distribution couldn't parse it, fall back to the legacy parsing logic
img := ContainerImage{}
img.RegistryURL = getRegistryFromIdentifier(identifier)
img.ImageAlias, img.ImageName, img.ImageTag = getImageTagFromIdentifier(identifier)
Expand Down
18 changes: 18 additions & 0 deletions pkg/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func Test_ParseImageTags(t *testing.T) {
assert.Equal(t, "gcr.io/jannfis/test-image", image.GetFullNameWithoutTag())
})

t.Run("Parse valid image name with default registry info", func(t *testing.T) {
image := NewFromIdentifier("docker.io/jannfis/test-image:0.1")
assert.Equal(t, "docker.io", image.RegistryURL)
assert.Empty(t, image.ImageAlias)
assert.Equal(t, "jannfis/test-image", image.ImageName)
require.NotNil(t, image.ImageTag)
assert.Equal(t, "0.1", image.ImageTag.TagName)
assert.Equal(t, "docker.io/jannfis/test-image:0.1", image.GetFullNameWithTag())
assert.Equal(t, "docker.io/jannfis/test-image", image.GetFullNameWithoutTag())
})

t.Run("Parse valid image name with digest tag", func(t *testing.T) {
image := NewFromIdentifier("gcr.io/jannfis/test-image@sha256:abcde")
assert.Equal(t, "gcr.io", image.RegistryURL)
Expand Down Expand Up @@ -70,6 +81,13 @@ func Test_ParseImageTags(t *testing.T) {
assert.Equal(t, "jannfis/test-image", image.ImageName)
assert.Nil(t, image.ImageTag)
})
t.Run("#273 classic-web=registry:5000/classic-web", func(t *testing.T) {
image := NewFromIdentifier("classic-web=registry:5000/classic-web")
assert.Equal(t, "registry:5000", image.RegistryURL)
assert.Equal(t, "classic-web", image.ImageAlias)
assert.Equal(t, "classic-web", image.ImageName)
assert.Nil(t, image.ImageTag)
})
}

func Test_ImageToString(t *testing.T) {
Expand Down