From c4270430db0f7e27a4c03b60822c7e14d210ae46 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 8 Jan 2025 10:12:31 +0900 Subject: [PATCH] ctr: `ctr images import --all-platforms`: fix unpack Fix issue 11228 `ctr images import --all-platforms` w/o `--local` was failing due to `unable to initialize unpacker: no unpack platforms defined` error. W/ `--local`, it unpacks the layers for the strict-default platform. Now `ctr images import --all-platforms` w/o `--local` unpacks the layers for the non-strict default platform. This behavior still differs from `--local`. i.e., on an arm64 host, arm/v{5,6,7} layers are unpacked too. Signed-off-by: Akihiro Suda --- cmd/ctr/commands/images/import.go | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/cmd/ctr/commands/images/import.go b/cmd/ctr/commands/images/import.go index cb94d97a2..fc9bf71fb 100644 --- a/cmd/ctr/commands/images/import.go +++ b/cmd/ctr/commands/images/import.go @@ -23,7 +23,6 @@ import ( "os" "time" - ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/urfave/cli/v2" containerd "github.com/containerd/containerd/v2/client" @@ -141,33 +140,26 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb opts = append(opts, image.WithNamedPrefix(prefix, overwrite)) } - var platSpec ocispec.Platform - // Only when all-platforms not specified, we will check platform value - // Implicitly if the platforms is empty, it means all-platforms + // Even with --all-platforms, only the default platform layers are unpacked, + // for compatibility with --local. + // + // This is still not fully compatible with --local, which only unpacks + // the strict-default platform layers. + platUnpack := platforms.DefaultSpec() if !cliContext.Bool("all-platforms") { // If platform specified, use that one, if not use default if platform := cliContext.String("platform"); platform != "" { - platSpec, err = platforms.Parse(platform) + platUnpack, err = platforms.Parse(platform) if err != nil { return err } - } else { - platSpec = platforms.DefaultSpec() } - opts = append(opts, image.WithPlatforms(platSpec)) + opts = append(opts, image.WithPlatforms(platUnpack)) } if !cliContext.Bool("no-unpack") { snapshotter := cliContext.String("snapshotter") - // If OS field is not empty, it means platSpec was updated in the above block - // i.e all-platforms was not specified - if platSpec.OS != "" { - opts = append(opts, image.WithUnpack(platSpec, snapshotter)) - } else { - // Empty spec means all platforms - var emptySpec ocispec.Platform - opts = append(opts, image.WithUnpack(emptySpec, snapshotter)) - } + opts = append(opts, image.WithUnpack(platUnpack, snapshotter)) } is := image.NewStore(cliContext.String("index-name"), opts...)