Merge pull request #6814 from QiWang19/oci-dir

Fix saving in oci format
This commit is contained in:
OpenShift Merge Robot
2020-07-09 16:38:39 +02:00
committed by GitHub
2 changed files with 48 additions and 22 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/containers/image/v5/image" "github.com/containers/image/v5/image"
"github.com/containers/image/v5/manifest" "github.com/containers/image/v5/manifest"
ociarchive "github.com/containers/image/v5/oci/archive" ociarchive "github.com/containers/image/v5/oci/archive"
"github.com/containers/image/v5/oci/layout"
is "github.com/containers/image/v5/storage" is "github.com/containers/image/v5/storage"
"github.com/containers/image/v5/tarball" "github.com/containers/image/v5/tarball"
"github.com/containers/image/v5/transports" "github.com/containers/image/v5/transports"
@ -1483,9 +1484,10 @@ func (i *Image) Save(ctx context.Context, source, format, output string, moreTag
return errors.Wrapf(err, "error getting OCI archive ImageReference for (%q, %q)", output, destImageName) return errors.Wrapf(err, "error getting OCI archive ImageReference for (%q, %q)", output, destImageName)
} }
case "oci-dir": case "oci-dir":
destRef, err = directory.NewReference(output) destImageName := imageNameForSaveDestination(i, source)
destRef, err = layout.NewReference(output, destImageName) // destImageName may be ""
if err != nil { if err != nil {
return errors.Wrapf(err, "error getting directory ImageReference for %q", output) return errors.Wrapf(err, "error getting the OCI directory ImageReference for (%q, %q)", output, destImageName)
} }
manifestType = imgspecv1.MediaTypeImageManifest manifestType = imgspecv1.MediaTypeImageManifest
case "docker-dir": case "docker-dir":

View File

@ -8,9 +8,15 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
"github.com/containers/buildah/imagebuildah" "github.com/containers/buildah/imagebuildah"
"github.com/containers/image/v5/directory"
"github.com/containers/image/v5/docker/reference" "github.com/containers/image/v5/docker/reference"
ociarchive "github.com/containers/image/v5/oci/archive"
"github.com/containers/image/v5/oci/layout"
"github.com/containers/image/v5/types"
"github.com/containers/libpod/v2/libpod/define" "github.com/containers/libpod/v2/libpod/define"
"github.com/containers/libpod/v2/libpod/image" "github.com/containers/libpod/v2/libpod/image"
"github.com/containers/libpod/v2/pkg/util" "github.com/containers/libpod/v2/pkg/util"
@ -18,9 +24,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/containers/image/v5/directory"
dockerarchive "github.com/containers/image/v5/docker/archive" dockerarchive "github.com/containers/image/v5/docker/archive"
ociarchive "github.com/containers/image/v5/oci/archive"
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
) )
@ -256,28 +260,48 @@ func DownloadFromFile(reader *os.File) (string, error) {
// LoadImage loads a container image into local storage // LoadImage loads a container image into local storage
func (r *Runtime) LoadImage(ctx context.Context, name, inputFile string, writer io.Writer, signaturePolicy string) (string, error) { func (r *Runtime) LoadImage(ctx context.Context, name, inputFile string, writer io.Writer, signaturePolicy string) (string, error) {
var newImages []*image.Image var (
src, err := dockerarchive.ParseReference(inputFile) // FIXME? We should add dockerarchive.NewReference() newImages []*image.Image
if err == nil { err error
newImages, err = r.ImageRuntime().LoadFromArchiveReference(ctx, src, signaturePolicy, writer) src types.ImageReference
} )
if err != nil {
// generate full src name with specified image:tag for _, referenceFn := range []func() (types.ImageReference, error){
src, err := ociarchive.NewReference(inputFile, name) // imageName may be "" func() (types.ImageReference, error) {
if err == nil { return dockerarchive.ParseReference(inputFile) // FIXME? We should add dockerarchive.NewReference()
newImages, err = r.ImageRuntime().LoadFromArchiveReference(ctx, src, signaturePolicy, writer) },
} func() (types.ImageReference, error) {
if err != nil { return ociarchive.NewReference(inputFile, name) // name may be ""
src, err := directory.NewReference(inputFile) },
if err == nil { func() (types.ImageReference, error) {
newImages, err = r.ImageRuntime().LoadFromArchiveReference(ctx, src, signaturePolicy, writer) // prepend "localhost/" to support local image saved with this semantics
if !strings.Contains(name, "/") {
return ociarchive.NewReference(inputFile, fmt.Sprintf("%s/%s", image.DefaultLocalRegistry, name))
} }
if err != nil { return nil, nil
return "", errors.Wrapf(err, "error pulling %q", name) },
func() (types.ImageReference, error) {
return directory.NewReference(inputFile)
},
func() (types.ImageReference, error) {
return layout.NewReference(inputFile, name)
},
func() (types.ImageReference, error) {
// prepend "localhost/" to support local image saved with this semantics
if !strings.Contains(name, "/") {
return layout.NewReference(inputFile, fmt.Sprintf("%s/%s", image.DefaultLocalRegistry, name))
}
return nil, nil
},
} {
src, err = referenceFn()
if err == nil && src != nil {
if newImages, err = r.ImageRuntime().LoadFromArchiveReference(ctx, src, signaturePolicy, writer); err == nil {
return getImageNames(newImages), nil
} }
} }
} }
return getImageNames(newImages), nil return "", errors.Wrapf(err, "error pulling %q", name)
} }
func getImageNames(images []*image.Image) string { func getImageNames(images []*image.Image) string {