Repository navigation
Stop mutating cached tag in isLatestSemver - #468
george-angel wants to merge 2 commits into
Conversation
isLatestSemver appended "@<sha>" 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 jetstack#467
There was a problem hiding this comment.
🟡 Changes recommended
The existing tests still pass with the original cache-mutating implementation, leaving repeated-call consistency and source-tag immutability unverified in both parent-SHA and child-SHA branches.
1 open issue
What changed in this PR
Fixes #467 by preventing isLatestSemver from modifying cached image tags, preserving availability checks and consistent results across reconciles when an upstream tag is re-pushed.
Changes:
- Copies the cached image before appending a digest in either SHA branch.
- Returns the copy, leaving the shared cache unchanged.
| File | Description |
|---|---|
pkg/controller/checker/checker.go |
Uses a local image copy for semver checks and digest-qualified results. |
Review effort: Balanced
| } | ||
|
|
||
| latestImageV := semver.Parse(latestImage.Tag) | ||
| result := *latestImage |
There was a problem hiding this comment.
@Copilot added in bb103c2: TestIsLatestSemverDoesNotMutateCache in checker_test.go, with a parent-SHA case and a child-SHA case. Each case drives a single FakeSearch whose With closure returns the same *api.ImageTag, so both calls see the same cached entry, and asserts:
- both calls return
v1.2.4@456withisLatest == false - the cached
api.ImageTagis unchanged after each call
Against the pre-fix implementation (HEAD~1) the test fails on the first call with call 1 mutated the cached image, so it pins the regression rather than just documenting current behaviour.
-- agent
Sun-Wukhan
left a comment
There was a problem hiding this comment.
The fix itself looks correct to me. Working on result := *latestImage (checker.go, around line 169) and returning &result keeps the "@" suffix writes (both the parent SHA branch and the child SHA branch) off the shared cache entry, semver.Parse still runs against the unsuffixed tag, and MatchesSHA semantics are unchanged. Two points:
-
There is no regression test, and this PR changes only checker.go. The existing
TestIsLatestSemverin checker_test.go builds a fresh search response per case and calls the function once, so it passes against the old mutating implementation too and would not catch a regression of #467. A test that would pin this: reuse one cached ImageTag across twoisLatestSemvercalls with a re-pushed upstream digest and assert the cached tag is unchanged after the first call (and that the second call still reports the image as available), covering both the parent SHA branch and the child SHA branch, since both wrote to the cached tag before. -
A note on the shallow copy:
result.Childrenstill shares its underlying slice with the cached entry. That is safe in this diff because the child loop only readschild.SHA, but it is the one remaining path by which a future edit inside this function could write through to the cache again. A one-line comment on the copy saying Children is shared and must stay read-only would make the invariant explicit.
With a regression test along the lines of point 1, I would be happy to see this land.
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 <george-angel@users.noreply.github.com>
|
@Sun-Wukhan thanks for the detailed review. Both points are addressed in
-- agent |

isLatestSemver appended "@" to the tag returned by search.LatestImage, which is a pointer into the shared version cache. When an upstream tag was re-pushed to a new digest, the cached tag stopped matching its own name, so the tag-name fallback in CurrentImage failed and version_checker_is_available reported 0 for an image that still exists. The corrupted entry was also reused on later reconciles, flipping IsLatest to true and, after a cache refetch, regressing LatestVersion.
Working on a copy keeps the cache intact.
Fixes #467