Skip to content

Commit

Permalink
feat(digest) Parse images digest
Browse files Browse the repository at this point in the history
In some occasions the same tag can be assigned to different images.
Like for example the official Python images tagged with `3.7` or other
projects which may not have a labeling strategy which only uses a label
like `latest` or `master`. Using digests instead of labels ensures that
you don't receive unexpected upgrades

More info about tags and digests can be found
[here](https://success.docker.com/article/images-tagging-vs-digests)

Fixes fluxcd#885
  • Loading branch information
carlosjgp committed Jun 15, 2019
1 parent 2036e3b commit 28ca03d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
29 changes: 21 additions & 8 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ func (i Name) ToRef(tag string) Ref {
// * alpine:3.5
// * library/alpine:3.5
// * docker.io/weaveworks/flux:1.1.0
// * localhost:5000/arbitrary/path/to/repo:revision-sha1
// * docker.io/weaveworks/flux@sha256:e5ac3944b44556916df9b97d78c28cf8e6b6ef419614a70e87b39f84c17428dd
// * docker.io/python:3.7@sha256:284a4057aac706aaafe50c5c0090d6d311f5d8d72014f0cd69f880a54e9df6fe
type Ref struct {
Name
Tag string
Tag string
Digest string
}

// CanonicalRef is an image ref with none of the fields left to be
Expand All @@ -122,11 +124,14 @@ type CanonicalRef struct {

// String returns the Ref as a string (i.e., unparsed) without canonicalising it.
func (i Ref) String() string {
var tag string
var tag, digest string
if i.Tag != "" {
tag = ":" + i.Tag
}
return fmt.Sprintf("%s%s", i.Name.String(), tag)
if i.Digest != "" {
digest = "@" + i.Digest
}
return fmt.Sprintf("%s%s%s", i.Name.String(), tag, digest)
}

// ParseRef parses a string representation of an image id into an
Expand All @@ -142,7 +147,13 @@ func ParseRef(s string) (Ref, error) {
return id, errors.Wrapf(ErrMalformedImageID, "parsing %q", s)
}

elements := strings.Split(s, "/")
repoWithDigest := strings.Split(s, "@")

if len(repoWithDigest) == 2 {
id.Digest = repoWithDigest[1]
}

elements := strings.Split(repoWithDigest[0], "/")
switch len(elements) {
case 0: // NB strings.Split will never return []
return id, errors.Wrapf(ErrMalformedImageID, "parsing %q", s)
Expand All @@ -169,6 +180,7 @@ func ParseRef(s string) (Ref, error) {
if imageParts[0] == "" || imageParts[1] == "" {
return id, errors.Wrapf(ErrMalformedImageID, "parsing %q", s)
}

id.Image = imageParts[0]
id.Tag = imageParts[1]
default:
Expand Down Expand Up @@ -205,8 +217,9 @@ func (i Ref) CanonicalRef() CanonicalRef {
name := i.CanonicalName()
return CanonicalRef{
Ref: Ref{
Name: name.Name,
Tag: i.Tag,
Name: name.Name,
Tag: i.Tag,
Digest: i.Digest,
},
}
}
Expand Down Expand Up @@ -242,7 +255,7 @@ type Labels struct {
BuildDate time.Time `json:"org.label-schema.build-date,omitempty"`
// Created holds the Open Container Image spec 'created' label
// Ref: https://github.com/opencontainers/image-spec/blob/master/annotations.md#pre-defined-annotation-keys
Created time.Time `json:"org.opencontainers.image.created,omitempty"`
Created time.Time `json:"org.opencontainers.image.created,omitempty"`
}

// MarshalJSON returns the Labels value in JSON (as bytes). It is
Expand Down
7 changes: 5 additions & 2 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func TestParseRef(t *testing.T) {
{"quay.io/library/alpine:latest", "quay.io", "library/alpine", "quay.io/library/alpine:latest"},
{"quay.io/library/alpine:mytag", "quay.io", "library/alpine", "quay.io/library/alpine:mytag"},
{"localhost:5000/path/to/repo/alpine:mytag", "localhost:5000", "path/to/repo/alpine", "localhost:5000/path/to/repo/alpine:mytag"},
// Images with digest
{"docker.io/weaveworks/flux@sha256:e5ac3944b44556916df9b97d78c28cf8e6b6ef419614a70e87b39f84c17428dd", "index.docker.io", "weaveworks/flux", "index.docker.io/weaveworks/flux@sha256:e5ac3944b44556916df9b97d78c28cf8e6b6ef419614a70e87b39f84c17428dd"},
{"localhost:5005/python:3.7@sha256:284a4057aac706aaafe50c5c0090d6d311f5d8d72014f0cd69f880a54e9df6fe", "localhost:5005", "python", "localhost:5005/python:3.7@sha256:284a4057aac706aaafe50c5c0090d6d311f5d8d72014f0cd69f880a54e9df6fe"},
} {
i, err := ParseRef(x.test)
if err != nil {
Expand Down Expand Up @@ -238,9 +241,9 @@ func TestImage_OrderByCreationDate(t *testing.T) {
imA := mustMakeInfo("my/Image:2", testTime)
imB := mustMakeInfo("my/Image:0", time.Time{}).setLabels(Labels{Created: time0})
imC := mustMakeInfo("my/Image:3", time.Time{}).setLabels(Labels{BuildDate: time2})
imD := mustMakeInfo("my/Image:4", time.Time{}) // test nil
imD := mustMakeInfo("my/Image:4", time.Time{}) // test nil
imE := mustMakeInfo("my/Image:1", time.Time{}).setLabels(Labels{Created: testTime}) // test equal
imF := mustMakeInfo("my/Image:5", time.Time{}) // test nil equal
imF := mustMakeInfo("my/Image:5", time.Time{}) // test nil equal
imgs := []Info{imA, imB, imC, imD, imE, imF}
Sort(imgs, NewerByCreated)
checkSorted(t, imgs)
Expand Down

0 comments on commit 28ca03d

Please sign in to comment.