From e6b3614e21c0e3c94a69a88b11f8219c0403b6fb Mon Sep 17 00:00:00 2001 From: stonezdj Date: Fri, 11 Nov 2022 22:46:21 +0800 Subject: [PATCH] Fix issue related to redhat registry proxy cache fixes #16495 Signed-off-by: stonezdj --- src/controller/proxy/controller.go | 18 +++++++++--------- src/controller/proxy/manifestcache.go | 21 ++++++++++++--------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/controller/proxy/controller.go b/src/controller/proxy/controller.go index c345e26afa69..f9392bfa6a81 100644 --- a/src/controller/proxy/controller.go +++ b/src/controller/proxy/controller.go @@ -170,31 +170,31 @@ func (c *controller) UseLocalManifest(ctx context.Context, art lib.ArtifactInfo, return a != nil && string(desc.Digest) == a.Digest, nil, nil // digest matches } - err = c.cache.Fetch(ctx, manifestListKey(art.Repository, string(desc.Digest)), &content) + err = c.cache.Fetch(ctx, manifestListKey(art.Repository, art), &content) if err != nil { if errors.Is(err, cache.ErrNotFound) { - log.Debugf("Digest is not found in manifest list cache, key=cache:%v", manifestListKey(art.Repository, string(desc.Digest))) + log.Debugf("Digest is not found in manifest list cache, key=cache:%v", manifestListKey(art.Repository, art)) } else { log.Errorf("Failed to get manifest list from cache, error: %v", err) } return a != nil && string(desc.Digest) == a.Digest, nil, nil } - err = c.cache.Fetch(ctx, manifestListContentTypeKey(art.Repository, string(desc.Digest)), &contentType) + err = c.cache.Fetch(ctx, manifestListContentTypeKey(art.Repository, art), &contentType) if err != nil { log.Debugf("failed to get the manifest list content type, not use local. error:%v", err) return false, nil, nil } - log.Debugf("Get the manifest list with key=cache:%v", manifestListKey(art.Repository, string(desc.Digest))) + log.Debugf("Get the manifest list with key=cache:%v", manifestListKey(art.Repository, art)) return true, &ManifestList{content, string(desc.Digest), contentType}, nil } -func manifestListKey(repo, dig string) string { - // actual redis key format is cache:manifestlist::sha256:xxxx - return "manifestlist:" + repo + ":" + dig +func manifestListKey(repo string, art lib.ArtifactInfo) string { + // actual redis key format is cache:manifestlist:: or cache:manifestlist::sha256:xxxx + return "manifestlist:" + repo + ":" + getReference(art) } -func manifestListContentTypeKey(rep, dig string) string { - return manifestListKey(rep, dig) + ":contenttype" +func manifestListContentTypeKey(rep string, art lib.ArtifactInfo) string { + return manifestListKey(rep, art) + ":contenttype" } func (c *controller) ProxyManifest(ctx context.Context, art lib.ArtifactInfo, remote RemoteInterface) (distribution.Manifest, error) { diff --git a/src/controller/proxy/manifestcache.go b/src/controller/proxy/manifestcache.go index 1a1c06a99096..95a47eb91786 100644 --- a/src/controller/proxy/manifestcache.go +++ b/src/controller/proxy/manifestcache.go @@ -69,15 +69,18 @@ func (m *ManifestListCache) CacheContent(ctx context.Context, remoteRepo string, log.Errorf("failed to get payload, error %v", err) return } - key := manifestListKey(art.Repository, art.Digest) - log.Debugf("cache manifest list with key=cache:%v", key) - err = m.cache.Save(ctx, manifestListContentTypeKey(art.Repository, art.Digest), contentType, manifestListCacheInterval) - if err != nil { - log.Errorf("failed to cache content type, error %v", err) - } - err = m.cache.Save(ctx, key, payload, manifestListCacheInterval) - if err != nil { - log.Errorf("failed to cache payload, error %v", err) + if len(getReference(art)) > 0 { + // some registry will not return the digest in the HEAD request, if no digest returned, cache manifest list content with tag + key := manifestListKey(art.Repository, art) + log.Debugf("cache manifest list with key=cache:%v", key) + err = m.cache.Save(ctx, manifestListContentTypeKey(art.Repository, art), contentType, manifestListCacheInterval) + if err != nil { + log.Errorf("failed to cache content type, error %v", err) + } + err = m.cache.Save(ctx, key, payload, manifestListCacheInterval) + if err != nil { + log.Errorf("failed to cache payload, error %v", err) + } } err = m.push(ctx, art.Repository, getReference(art), man) if err != nil {