Skip to content

Commit

Permalink
Cache image list with digest key
Browse files Browse the repository at this point in the history
  fixes goharbor#19429

Signed-off-by: stonezdj <daojunz@vmware.com>
  • Loading branch information
stonezdj authored and stonezdj committed Jan 5, 2024
1 parent 88a4cab commit 18d2548
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/controller/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ func (c *controller) UseLocalManifest(ctx context.Context, art lib.ArtifactInfo,
if c.cache == nil {
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)
if err != nil {
if errors.Is(err, cache.ErrNotFound) {
Expand Down Expand Up @@ -318,8 +321,8 @@ func getRemoteRepo(art lib.ArtifactInfo) string {
}

func getReference(art lib.ArtifactInfo) string {
if len(art.Tag) > 0 {
return art.Tag
if len(art.Digest) > 0 {
return art.Digest
}
return art.Digest
return art.Tag
}
5 changes: 4 additions & 1 deletion src/controller/proxy/manifestcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func (m *ManifestListCache) CacheContent(ctx context.Context, _ string, man dist
log.Errorf("failed to get reference, reference is empty, skip to cache manifest list")
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)
log.Debugf("cache manifest list with key=cache:%v", key)
if err := m.cache.Save(ctx, manifestListContentTypeKey(art.Repository, art), contentType, manifestListCacheInterval); err != nil {
Expand Down

0 comments on commit 18d2548

Please sign in to comment.