Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pkg/controller/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

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


latestImageV := semver.Parse(result.Tag)

var isLatest bool

Expand All @@ -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.
Expand Down
63 changes: 63 additions & 0 deletions pkg/controller/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading