Skip to content

Commit

Permalink
fix: mismatched mediaType error when push image build with warmed bas…
Browse files Browse the repository at this point in the history
…e image(OCI format) cache to registry
  • Loading branch information
luxurine committed Jul 2, 2024
1 parent d6aab15 commit 081bbc7
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"

Expand All @@ -248,9 +244,31 @@ 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)
if err != nil {
return nil, errors.Wrap(err, "getting image from path")
}
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
}

0 comments on commit 081bbc7

Please sign in to comment.