diff options
author | Qi Wang <qiwan@redhat.com> | 2020-07-07 10:35:33 -0400 |
---|---|---|
committer | Qi Wang <qiwan@redhat.com> | 2020-07-09 08:03:54 -0400 |
commit | a1d6114177874125550890c0e4b30a98fc6d304e (patch) | |
tree | 1ffbbf205bfd1a537ce36dc5f891091cc4938228 /libpod/runtime_img.go | |
parent | edf5fe8b17f39ce98805c279b9795bfd4123abe2 (diff) | |
download | podman-a1d6114177874125550890c0e4b30a98fc6d304e.tar.gz podman-a1d6114177874125550890c0e4b30a98fc6d304e.tar.bz2 podman-a1d6114177874125550890c0e4b30a98fc6d304e.zip |
Fix saving in oci format
- fix saving&loading oci format. Close #6544
- support loading using image name without "localhost/" prefix when reading from ociarchive/dir saved from this semantics
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
Signed-off-by: Qi Wang <qiwan@redhat.com>
Diffstat (limited to 'libpod/runtime_img.go')
-rw-r--r-- | libpod/runtime_img.go | 64 |
1 files changed, 44 insertions, 20 deletions
diff --git a/libpod/runtime_img.go b/libpod/runtime_img.go index eab05f34d..370c9d610 100644 --- a/libpod/runtime_img.go +++ b/libpod/runtime_img.go @@ -8,9 +8,15 @@ import ( "net/http" "net/url" "os" + "strings" "github.com/containers/buildah/imagebuildah" + "github.com/containers/image/v5/directory" "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/image" "github.com/containers/libpod/v2/pkg/util" @@ -18,9 +24,7 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" - "github.com/containers/image/v5/directory" 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" ) @@ -256,28 +260,48 @@ func DownloadFromFile(reader *os.File) (string, error) { // 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) { - var newImages []*image.Image - src, err := dockerarchive.ParseReference(inputFile) // FIXME? We should add dockerarchive.NewReference() - if err == nil { - newImages, err = r.ImageRuntime().LoadFromArchiveReference(ctx, src, signaturePolicy, writer) - } - if err != nil { - // generate full src name with specified image:tag - src, err := ociarchive.NewReference(inputFile, name) // imageName may be "" - if err == nil { - newImages, err = r.ImageRuntime().LoadFromArchiveReference(ctx, src, signaturePolicy, writer) - } - if err != nil { - src, err := directory.NewReference(inputFile) - if err == nil { - newImages, err = r.ImageRuntime().LoadFromArchiveReference(ctx, src, signaturePolicy, writer) + var ( + newImages []*image.Image + err error + src types.ImageReference + ) + + for _, referenceFn := range []func() (types.ImageReference, error){ + func() (types.ImageReference, error) { + return dockerarchive.ParseReference(inputFile) // FIXME? We should add dockerarchive.NewReference() + }, + func() (types.ImageReference, error) { + return ociarchive.NewReference(inputFile, name) // name may be "" + }, + func() (types.ImageReference, error) { + // 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)) + } + return nil, nil + }, + 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)) } - if err != nil { - return "", errors.Wrapf(err, "error pulling %q", 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 { |