From 9b37d976e65bb04cc32988c67b98a3836d3417d6 Mon Sep 17 00:00:00 2001 From: George Angel Date: Wed, 18 Nov 2026 14:24:31 +1000 Subject: [PATCH 1/2] Stop mutating cached tag in isLatestSemver isLatestSemver appended "@" to the *api.ImageTag returned by search.LatestImage, which points into the shared version cache. Once a tag had been re-pushed upstream, the cached tag no longer matched its name, so the tag-name fallback in CurrentImage failed and version_checker_is_available reported 0 for an image that still exists. The same corrupted entry was reused on later reconciles, flipping IsLatest to true and, after a cache refetch, regressing LatestVersion. Work on a copy so the cache stays intact. Fixes #467 --- pkg/controller/checker/checker.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/controller/checker/checker.go b/pkg/controller/checker/checker.go index 01d43c31..c1de697d 100644 --- a/pkg/controller/checker/checker.go +++ b/pkg/controller/checker/checker.go @@ -166,7 +166,9 @@ func (c *Checker) isLatestSemver(ctx context.Context, imageURL, currentSHA strin return nil, false, err } - latestImageV := semver.Parse(latestImage.Tag) + result := *latestImage + + latestImageV := semver.Parse(result.Tag) var isLatest bool @@ -179,23 +181,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. From bb103c20d97b570b0bb153fc225e14bd25e3fef8 Mon Sep 17 00:00:00 2001 From: George Angel Date: Tue, 1 Dec 2026 11:17:24 +1000 Subject: [PATCH 2/2] Add regression test for cached tag immutability TestIsLatestSemverDoesNotMutateCache calls isLatestSemver twice with the same cached image tag, once for the parent SHA branch and once for the child SHA branch, and asserts the cached entry is unchanged while both calls return the same digest-qualified tag with IsLatest false. Against the mutating implementation the first call corrupts the cached tag. Also document on the copy that Children and its elements are still shared with the cache and must stay read-only. Signed-off-by: George Angel --- pkg/controller/checker/checker.go | 3 ++ pkg/controller/checker/checker_test.go | 63 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/pkg/controller/checker/checker.go b/pkg/controller/checker/checker.go index c1de697d..63002f79 100644 --- a/pkg/controller/checker/checker.go +++ b/pkg/controller/checker/checker.go @@ -166,6 +166,9 @@ func (c *Checker) isLatestSemver(ctx context.Context, imageURL, currentSHA strin return nil, false, err } + // 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) 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