diff --git a/pkg/controller/checker/checker.go b/pkg/controller/checker/checker.go index 01d43c31..63002f79 100644 --- a/pkg/controller/checker/checker.go +++ b/pkg/controller/checker/checker.go @@ -166,7 +166,12 @@ func (c *Checker) isLatestSemver(ctx context.Context, imageURL, currentSHA strin return nil, false, err } - latestImageV := semver.Parse(latestImage.Tag) + // Copy before adding a digest, so the cached entry behind latestImage is + // never modified. Children and its elements are still shared with the + // cache and must stay read-only in this function. + result := *latestImage + + latestImageV := semver.Parse(result.Tag) var isLatest bool @@ -179,23 +184,23 @@ func (c *Checker) isLatestSemver(ctx context.Context, imageURL, currentSHA strin // but the SHA has been updated upstream, // mark not latest if currentImage.Equal(latestImageV) && - !latestImage.MatchesSHA(currentSHA) { + !result.MatchesSHA(currentSHA) { isLatest = false - if latestImage.SHA != "" { + if result.SHA != "" { // Add the SHA as a prefix to identify that it has been updated! - latestImage.Tag = fmt.Sprintf("%s@%s", latestImage.Tag, latestImage.SHA) + result.Tag = fmt.Sprintf("%s@%s", result.Tag, result.SHA) } else { - for _, child := range latestImage.Children { + for _, child := range result.Children { // Take first child's SHA for latest image tag if child.SHA != currentSHA { - latestImage.Tag = fmt.Sprintf("%s@%s", latestImage.Tag, child.SHA) + result.Tag = fmt.Sprintf("%s@%s", result.Tag, child.SHA) break } } } } - return latestImage, isLatest, nil + return &result, isLatest, nil } // isLatestSHA will return the the result of whether the given image is the latest, according to image SHA. diff --git a/pkg/controller/checker/checker_test.go b/pkg/controller/checker/checker_test.go index 3e3e1734..229f713c 100644 --- a/pkg/controller/checker/checker_test.go +++ b/pkg/controller/checker/checker_test.go @@ -546,6 +546,69 @@ func TestIsLatestSemver(t *testing.T) { } } +// TestIsLatestSemverDoesNotMutateCache ensures isLatestSemver works on a copy +// of the cached image tag, so repeated lookups keep matching a tag that was +// re-pushed upstream. +func TestIsLatestSemverDoesNotMutateCache(t *testing.T) { + tests := map[string]struct { + imageURL, currentSHA string + currentImage *semver.SemVer + cached *api.ImageTag + expTag string + }{ + "if the parent SHA differs from upstream": { + imageURL: "docker.io", + currentSHA: "123", + currentImage: semver.Parse("v1.2.4"), + cached: &api.ImageTag{ + Tag: "v1.2.4", + SHA: "456", + }, + expTag: "v1.2.4@456", + }, + "if the child SHA differs from upstream": { + imageURL: "docker.io", + currentSHA: "123", + currentImage: semver.Parse("v1.2.4"), + cached: &api.ImageTag{ + Tag: "v1.2.4", + Children: []*api.ImageTag{ + {SHA: "456"}, + {SHA: "789"}, + }, + }, + expTag: "v1.2.4@456", + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + expCached := *test.cached + if test.cached.Children != nil { + expCached.Children = make([]*api.ImageTag, len(test.cached.Children)) + for i, child := range test.cached.Children { + childCopy := *child + expCached.Children[i] = &childCopy + } + } + + checker := New(search.New().With(test.cached, nil)) + + for i := 0; i < 2; i++ { + latestImage, isLatest, err := checker.isLatestSemver(context.TODO(), test.imageURL, test.currentSHA, test.currentImage, nil) + require.NoError(t, err) + + assert.Equal(t, test.expTag, latestImage.Tag, + "call %d returned unexpected tag", i+1) + assert.False(t, isLatest, + "call %d should not report the image as latest", i+1) + assert.Equal(t, expCached, *test.cached, + "call %d mutated the cached image", i+1) + } + }) + } +} + func TestIsLatestSHA(t *testing.T) { tests := map[string]struct { imageURL, currentSHA string