Merge pull request #11236 from k8s-infra-cherrypick-robot/cherry-pick-11229-to-release/2.0

[release/2.0] ctr: `ctr images import --all-platforms`: fix unpack
This commit is contained in:
Phil Estes 2025-01-09 11:51:40 -05:00 committed by GitHub
commit 6f98bd9ed7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,7 +23,6 @@ import (
"os" "os"
"time" "time"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
containerd "github.com/containerd/containerd/v2/client" 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)) opts = append(opts, image.WithNamedPrefix(prefix, overwrite))
} }
var platSpec ocispec.Platform // Even with --all-platforms, only the default platform layers are unpacked,
// Only when all-platforms not specified, we will check platform value // for compatibility with --local.
// Implicitly if the platforms is empty, it means all-platforms //
// 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 !cliContext.Bool("all-platforms") {
// If platform specified, use that one, if not use default // If platform specified, use that one, if not use default
if platform := cliContext.String("platform"); platform != "" { if platform := cliContext.String("platform"); platform != "" {
platSpec, err = platforms.Parse(platform) platUnpack, err = platforms.Parse(platform)
if err != nil { if err != nil {
return err return err
} }
} else {
platSpec = platforms.DefaultSpec()
} }
opts = append(opts, image.WithPlatforms(platSpec)) opts = append(opts, image.WithPlatforms(platUnpack))
} }
if !cliContext.Bool("no-unpack") { if !cliContext.Bool("no-unpack") {
snapshotter := cliContext.String("snapshotter") snapshotter := cliContext.String("snapshotter")
// If OS field is not empty, it means platSpec was updated in the above block opts = append(opts, image.WithUnpack(platUnpack, snapshotter))
// 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))
}
} }
is := image.NewStore(cliContext.String("index-name"), opts...) is := image.NewStore(cliContext.String("index-name"), opts...)