Skip to content

Commit

Permalink
✨ Support parse tag with digest (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Oct 18, 2023
1 parent e8292a4 commit 29ef03e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
14 changes: 11 additions & 3 deletions pkg/utils/imagerefs/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@ func Parse(name string) (string, string, string, string, error) {
named = reference.TagNameOnly(named)
domain := reference.Domain(named)
path := reference.Path(named)

var tagOrDigest string
tagged, ok := named.(reference.Tagged)
if !ok {
return "", "", "", "", fmt.Errorf("reference is not tagged: %v, %s", named, name)
digested, ok := named.(reference.Digested)
if !ok {
return "", "", "", "", fmt.Errorf("reference is not tagged or digested: %v, %s", named, name)
}
tagOrDigest = digested.Digest().String()
} else {
tagOrDigest = tagged.Tag()
}
tag := tagged.Tag()

if !strings.Contains(path, "/") {
return "", "", "", "", fmt.Errorf("invalid reference: %s", name)
}
parts := strings.Split(path, "/")
ns := parts[0]
repo := path
return domain, ns, repo, tag, nil
return domain, ns, repo, tagOrDigest, nil
}
24 changes: 23 additions & 1 deletion pkg/utils/imagerefs/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ func TestParse(t *testing.T) {
},
{
name: "test6",
args: args{
name: "kubernetes-helm/test/tiller@sha256:4ca2a277f1dc3ddd0da33a258096de9a1cf5b9d9bd96b27ee78763ee2248c28c",
},
want: "docker.io",
want1: "kubernetes-helm",
want2: "kubernetes-helm/test/tiller",
want3: "sha256:4ca2a277f1dc3ddd0da33a258096de9a1cf5b9d9bd96b27ee78763ee2248c28c",
wantErr: false,
},
{
name: "test7",
args: args{
name: "kubernetes-helm/test/tiller:v1@sha256:4ca2a277f1dc3ddd0da33a258096de9a1cf5b9d9bd96b27ee78763ee2248c28c",
},
want: "docker.io",
want1: "kubernetes-helm",
want2: "kubernetes-helm/test/tiller",
want3: "v1",
wantErr: false,
},
{
name: "test8",
args: args{
name: "kubernetes-helm/test/tiller",
},
Expand All @@ -98,7 +120,7 @@ func TestParse(t *testing.T) {
wantErr: false,
},
{
name: "test7",
name: "test9",
args: args{
name: "busybox",
},
Expand Down

0 comments on commit 29ef03e

Please sign in to comment.