From 39165ac42393f8662bf0fe69a0f9ca145535903c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E6=AD=A3=E9=A3=9E?= Date: Tue, 2 Jul 2024 19:02:48 +0800 Subject: [PATCH] fix: mismatched mediaType error when push image build with warmed base image(OCI format) cache to registry --- pkg/cache/cache.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index b861866eaf..3f30506441 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -32,6 +32,7 @@ import ( "github.com/google/go-containerregistry/pkg/v1/layout" "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/tarball" + "github.com/google/go-containerregistry/pkg/v1/types" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -228,11 +229,6 @@ func mfstFromPath(p string) (*v1.Manifest, error) { } func cachedImageFromPath(p string) (v1.Image, error) { - imgTar, err := tarball.ImageFromPath(p, nil) - if err != nil { - return nil, errors.Wrap(err, "getting image from path") - } - // Manifests may be present next to the tar, named with a ".json" suffix mfstPath := p + ".json" @@ -248,9 +244,28 @@ func cachedImageFromPath(p string) (v1.Image, error) { } } + var imgTar v1.Image + var err error + switch extractMediaTypeVendor(mfst.MediaType) { + case types.DockerVendorPrefix: + imgTar, err = tarball.ImageFromPath(p, nil) + case types.OCIVendorPrefix: + imgTar, err = tarball.OCIImageFromPath(p, nil) + } + if err != nil { + return nil, errors.Wrap(err, "getting image from path") + } + return &cachedImage{ digest: filepath.Base(p), Image: imgTar, mfst: mfst, }, nil } + +func extractMediaTypeVendor(mt types.MediaType) string { + if strings.Contains(string(mt), types.OCIVendorPrefix) { + return types.OCIVendorPrefix + } + return types.DockerVendorPrefix +}