Skip to content

Stop mutating cached tag in isLatestSemver - #468

Open
george-angel wants to merge 2 commits into
jetstack:mainfrom
george-angel:fix-is-available-cache-mutation
Open

george-angel wants to merge 2 commits into
jetstack:mainfrom
george-angel:fix-is-available-cache-mutation

Conversation

@george-angel

Copy link
Copy Markdown

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

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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@456 with isLatest == false
  • the cached api.ImageTag is 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 Sun-Wukhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. There is no regression test, and this PR changes only checker.go. The existing TestIsLatestSemver in 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 two isLatestSemver calls 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.

  2. A note on the shallow copy: result.Children still shares its underlying slice with the cached entry. That is safe in this diff because the child loop only reads child.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>
@george-angel

Copy link
Copy Markdown
Author

@Sun-Wukhan thanks for the detailed review. Both points are addressed in bb103c2.

  1. Regression test: TestIsLatestSemverDoesNotMutateCache (pkg/controller/checker/checker_test.go) reuses one cached *api.ImageTag across two isLatestSemver calls, covering both the parent SHA and the child SHA branch. After each call it asserts the returned tag is v1.2.4@456, that isLatest is false, and that the cached api.ImageTag is unchanged. Run against the pre-fix implementation it fails on the first call (call 1 mutated the cached image), so it would catch a regression of version_checker_is_available reports 0 for images whose tag still exists but was re-pushed (cached tag is mutated) #467.

  2. On the shallow copy: result := *latestImage now documents that the copy is there to keep the cache intact, and that Children and its elements are still shared with the cache and must stay read-only. As you say, the child loop only reads child.SHA today.

-- agent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

version_checker_is_available reports 0 for images whose tag still exists but was re-pushed (cached tag is mutated)

3 participants