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

feat(url): tag and digest can appear together #132

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
55 changes: 36 additions & 19 deletions pkg/utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,36 @@ type RepoURL struct {
func GenerateRepoURLs(url string, externalTagsOrDigest func(registry, repository string,
) (tagsOrDigest []string, err error)) ([]*RepoURL, error) {
var result []*RepoURL
ref, err := reference.ParseNormalizedNamed(url)

var tagsOrDigest []string
var urlWithoutTagOrDigest string

if canonicalRef, ok := ref.(reference.Canonical); ok {
// url has digest
tagsOrDigest = append(tagsOrDigest, canonicalRef.Digest().String())
urlWithoutTagOrDigest = canonicalRef.Name()
} else if taggedRef, ok := ref.(reference.NamedTagged); ok {
// url has one normal tag
tagsOrDigest = append(tagsOrDigest, taggedRef.Tag())
urlWithoutTagOrDigest = taggedRef.Name()
} else if err == nil {
// url has no specified digest or tag
registry, repo := getRegistryAndRepositoryFromURLWithoutTagOrDigest(url)
allTags, err := externalTagsOrDigest(registry, repo)
if err != nil {
return nil, fmt.Errorf("failed to get external tags: %v", err)
ref, err := reference.ParseNormalizedNamed(url)
if err == nil {
tagOrDigest := make([]string, 0, 2)
if taggedRef, ok := ref.(reference.NamedTagged); ok {
// url has one normal tag
tagOrDigest = append(tagOrDigest, taggedRef.Tag())
urlWithoutTagOrDigest = taggedRef.Name()
}
if canonicalRef, ok := ref.(reference.Canonical); ok {
// url has digest
tagOrDigest = append(tagOrDigest, canonicalRef.Digest().String())
urlWithoutTagOrDigest = canonicalRef.Name()
}
if len(tagOrDigest) > 0 {
tagsOrDigest = append(tagsOrDigest, strings.Join(tagOrDigest, "@"))
} else {
// url has no specified digest or tag
registry, repo := getRegistryAndRepositoryFromURLWithoutTagOrDigest(url)
allTags, err := externalTagsOrDigest(registry, repo)
if err != nil {
return nil, fmt.Errorf("failed to get external tags: %v", err)
}

urlWithoutTagOrDigest = url
tagsOrDigest = append(tagsOrDigest, allTags...)
urlWithoutTagOrDigest = url
tagsOrDigest = append(tagsOrDigest, allTags...)
}
} else {
// url might have special tags
if strings.Contains(url, ":/") {
Expand Down Expand Up @@ -97,8 +104,18 @@ func GenerateRepoURLs(url string, externalTagsOrDigest func(registry, repository
return nil, fmt.Errorf("failed to parse first tag with url %v: %v", slice[0], err)
}

urlWithoutTagOrDigest = ref.(reference.NamedTagged).Name()
tagsOrDigest = append(tagsOrDigest, ref.(reference.NamedTagged).Tag())
tagOrDigest := make([]string, 0, 2)
if taggedRef, ok := ref.(reference.NamedTagged); ok {
// url has one normal tag
tagOrDigest = append(tagOrDigest, taggedRef.Tag())
urlWithoutTagOrDigest = taggedRef.Name()
}
if canonicalRef, ok := ref.(reference.Canonical); ok {
// url has digest
tagOrDigest = append(tagOrDigest, canonicalRef.Digest().String())
urlWithoutTagOrDigest = canonicalRef.Name()
}
tagsOrDigest = append(tagsOrDigest, strings.Join(tagOrDigest, "@"))
tagsOrDigest = append(tagsOrDigest, slice[1:]...)
}
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestURL(t *testing.T) {
"registry.cn-beijing.aliyuncs.com/hhyasdf/hybridnet@sha256:df2ef9e979fc063645dcbed51374233c6bcf4ab49308c0478702565e96b9bc9e",
"nginx",
"test-regex/test:/b+/",
"registry.cn-beijing.aliyuncs.com/hhyasdf/hybridnet:v1.2@sha256:df2ef9e979fc063645dcbed51374233c6bcf4ab49308c0478702565e96b9bc9e,v1.3@sha256:df2ef9e979fc063645dcbed51372233c6bcf4ab49308c0478702565e96b9bc9e",
"registry.cn-beijing.aliyuncs.com/hhyasdf/hybridnet:v1.2@sha256:df2ef9e979fc063645dcbed51374233c6bcf4ab49308c0478702565e96b9bc9e,v1.3",
}

var repoURLs []*RepoURL
Expand Down Expand Up @@ -59,4 +61,8 @@ func TestURL(t *testing.T) {
repoURLs[7].GetRepo())
assert.Equal(t, DockerHubURL, repoURLs[8].GetRegistry())
assert.Equal(t, "bbb", repoURLs[9].GetTagOrDigest())
assert.Equal(t, "v1.2@sha256:df2ef9e979fc063645dcbed51374233c6bcf4ab49308c0478702565e96b9bc9e", repoURLs[10].GetTagOrDigest())
assert.Equal(t, "v1.3@sha256:df2ef9e979fc063645dcbed51372233c6bcf4ab49308c0478702565e96b9bc9e", repoURLs[11].GetTagOrDigest())
assert.Equal(t, "v1.2@sha256:df2ef9e979fc063645dcbed51374233c6bcf4ab49308c0478702565e96b9bc9e", repoURLs[12].GetTagOrDigest())
assert.Equal(t, "v1.3", repoURLs[13].GetTagOrDigest())
}