Skip to content

Commit

Permalink
load: support buildkit archives
Browse files Browse the repository at this point in the history
Archives generated with buildkit have some kind of "hybrid" layout which
is the same for OCI and Docker archives.  OCI ones ship with a
manifest.json but set the image's reference in the index.json but in a
custom annotation and not the one the OCI image spec wants.  Archives
in the Docker format set the reference in `RepoTags` of the
manifest.json.

To support these archives, simply look for the custom containerd
annotation *and* change the order back to give OCI archives precedence.

Fixes: containers/podman/issues/12560
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
  • Loading branch information
vrothberg committed Dec 10, 2021
1 parent 515a280 commit e04bbd4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
22 changes: 11 additions & 11 deletions libimage/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ func (r *Runtime) Load(ctx context.Context, path string, options *LoadOptions) (
var loadErrors []error

for _, f := range []func() ([]string, string, error){
// DOCKER-ARCHIVE - must be first (see containers/podman/issues/10809)
func() ([]string, string, error) {
logrus.Debugf("-> Attempting to load %q as a Docker archive", path)
ref, err := dockerArchiveTransport.ParseReference(path)
if err != nil {
return nil, dockerArchiveTransport.Transport.Name(), err
}
images, err := r.loadMultiImageDockerArchive(ctx, ref, &options.CopyOptions)
return images, dockerArchiveTransport.Transport.Name(), err
},

// OCI
func() ([]string, string, error) {
logrus.Debugf("-> Attempting to load %q as an OCI directory", path)
Expand All @@ -68,6 +57,17 @@ func (r *Runtime) Load(ctx context.Context, path string, options *LoadOptions) (
return images, ociArchiveTransport.Transport.Name(), err
},

// DOCKER-ARCHIVE - must be first (see containers/podman/issues/10809)
func() ([]string, string, error) {
logrus.Debugf("-> Attempting to load %q as a Docker archive", path)
ref, err := dockerArchiveTransport.ParseReference(path)
if err != nil {
return nil, dockerArchiveTransport.Transport.Name(), err
}
images, err := r.loadMultiImageDockerArchive(ctx, ref, &options.CopyOptions)
return images, dockerArchiveTransport.Transport.Name(), err
},

// DIR
func() ([]string, string, error) {
logrus.Debugf("-> Attempting to load %q as a Docker dir", path)
Expand Down
2 changes: 2 additions & 0 deletions libimage/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ func TestLoad(t *testing.T) {
{"testdata/docker-two-names.tar.xz", false, 1, []string{"localhost/pretty-empty:latest", "example.com/empty:latest"}},
{"testdata/docker-two-images.tar.xz", false, 2, []string{"example.com/empty:latest", "example.com/empty/but:different"}},
{"testdata/docker-unnamed.tar.xz", false, 1, []string{"sha256:ec9293436c2e66da44edb9efb8d41f6b13baf62283ebe846468bc992d76d7951"}},
{"testdata/buildkit-docker.tar", false, 1, []string{"github.com/buildkit/archive:docker"}},

// OCI ARCHIVE
{"testdata/oci-name-only.tar.gz", false, 1, []string{"localhost/pretty-empty:latest"}},
{"testdata/oci-non-docker-name.tar.gz", true, 0, nil},
{"testdata/oci-registry-name.tar.gz", false, 1, []string{"example.com/empty:latest"}},
{"testdata/oci-unnamed.tar.gz", false, 1, []string{"sha256:5c8aca8137ac47e84c69ae93ce650ce967917cc001ba7aad5494073fac75b8b6"}},
{"testdata/buildkit-oci.tar", false, 1, []string{"github.com/buildkit/archive:oci"}},
} {
loadedImages, err := runtime.Load(ctx, test.input, loadOptions)
if test.expectError {
Expand Down
24 changes: 20 additions & 4 deletions libimage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/containers/image/v5/types"
"github.com/containers/storage"
digest "github.com/opencontainers/go-digest"
ociSpec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -169,6 +170,20 @@ func (r *Runtime) Pull(ctx context.Context, name string, pullPolicy config.PullP
return localImages, pullError
}

// nameFromAnnotations returns a reference string to be used as an image name,
// or an empty string. The annotations map may be nil.
func nameFromAnnotations(annotations map[string]string) string {
if annotations == nil {
return ""
}
// buildkit/containerd are using a custom annotation see
// containers/podman/issues/12560.
if annotations["io.containerd.image.name"] != "" {
return annotations["io.containerd.image.name"]
}
return annotations[ociSpec.AnnotationRefName]
}

// copyFromDefault is the default copier for a number of transports. Other
// transports require some specific dancing, sometimes Yoga.
func (r *Runtime) copyFromDefault(ctx context.Context, ref types.ImageReference, options *CopyOptions) ([]string, error) {
Expand Down Expand Up @@ -201,15 +216,16 @@ func (r *Runtime) copyFromDefault(ctx context.Context, ref types.ImageReference,
if err != nil {
return nil, err
}
// if index.json has no reference name, compute the image ID instead
if manifestDescriptor.Annotations == nil || manifestDescriptor.Annotations["org.opencontainers.image.ref.name"] == "" {
storageName = nameFromAnnotations(manifestDescriptor.Annotations)
switch len(storageName) {
case 0:
// If there's no reference name in the annotations, compute an ID.
storageName, err = getImageID(ctx, ref, nil)
if err != nil {
return nil, err
}
imageName = "sha256:" + storageName[1:]
} else {
storageName = manifestDescriptor.Annotations["org.opencontainers.image.ref.name"]
default:
named, err := NormalizeName(storageName)
if err != nil {
return nil, err
Expand Down
Binary file added libimage/testdata/buildkit-docker.tar
Binary file not shown.
Binary file added libimage/testdata/buildkit-oci.tar
Binary file not shown.

0 comments on commit e04bbd4

Please sign in to comment.