[cherry-pick] Cache image list with digest key (#19837)

Cache image list with digest key

  fixes #19429

Signed-off-by: stonezdj <daojunz@vmware.com>
Co-authored-by: stonezdj <daojunz@vmware.com>
This commit is contained in:
stonezdj(Daojun Zhang) 2024-01-15 13:45:30 +08:00 committed by GitHub
parent 8afce30617
commit 84d7f17b6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 6 deletions

View File

@ -183,7 +183,10 @@ func (c *controller) UseLocalManifest(ctx context.Context, art lib.ArtifactInfo,
if c.cache == nil { if c.cache == nil {
return a != nil && string(desc.Digest) == a.Digest, nil, nil // digest matches return a != nil && string(desc.Digest) == a.Digest, nil, nil // digest matches
} }
// Pass digest to the cache key, digest is more stable than tag, because tag could be updated
if len(art.Digest) == 0 {
art.Digest = string(desc.Digest)
}
err = c.cache.Fetch(ctx, manifestListKey(art.Repository, art), &content) err = c.cache.Fetch(ctx, manifestListKey(art.Repository, art), &content)
if err != nil { if err != nil {
if errors.Is(err, cache.ErrNotFound) { if errors.Is(err, cache.ErrNotFound) {
@ -318,8 +321,8 @@ func getRemoteRepo(art lib.ArtifactInfo) string {
} }
func getReference(art lib.ArtifactInfo) string { func getReference(art lib.ArtifactInfo) string {
if len(art.Tag) > 0 { if len(art.Digest) > 0 {
return art.Tag return art.Digest
} }
return art.Digest return art.Tag
} }

View File

@ -209,7 +209,7 @@ func TestGetRef(t *testing.T) {
{ {
name: `normal`, name: `normal`,
in: lib.ArtifactInfo{Repository: "hello-world", Tag: "latest", Digest: "sha256:aabbcc"}, in: lib.ArtifactInfo{Repository: "hello-world", Tag: "latest", Digest: "sha256:aabbcc"},
want: "latest", want: "sha256:aabbcc",
}, },
{ {
name: `digest_only`, name: `digest_only`,

View File

@ -73,7 +73,10 @@ func (m *ManifestListCache) CacheContent(ctx context.Context, remoteRepo string,
log.Errorf("failed to get reference, reference is empty, skip to cache manifest list") log.Errorf("failed to get reference, reference is empty, skip to cache manifest list")
return return
} }
// some registry will not return the digest in the HEAD request, if no digest returned, cache manifest list content with tag // cache key should contain digest if digest exist
if len(art.Digest) == 0 {
art.Digest = string(digest.FromBytes(payload))
}
key := manifestListKey(art.Repository, art) key := manifestListKey(art.Repository, art)
log.Debugf("cache manifest list with key=cache:%v", key) log.Debugf("cache manifest list with key=cache:%v", key)
if err := m.cache.Save(ctx, manifestListContentTypeKey(art.Repository, art), contentType, manifestListCacheInterval); err != nil { if err := m.cache.Save(ctx, manifestListContentTypeKey(art.Repository, art), contentType, manifestListCacheInterval); err != nil {