Skip to content

Commit

Permalink
ctr: Sync with upstream ctr and add --platform option to import
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Berger <stefab@linux.ibm.com>
  • Loading branch information
stefanberger authored and lumjjb committed Sep 19, 2022
1 parent 3c7c6b1 commit b8f807f
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions cmd/ctr/commands/images/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/platforms"
"github.com/containerd/imgcrypt"
"github.com/containerd/imgcrypt/cmd/ctr/commands/flags"
"github.com/containerd/imgcrypt/images/encryption"
Expand Down Expand Up @@ -72,6 +73,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
Name: "all-platforms",
Usage: "imports content for all platforms, false by default",
},
cli.StringFlag{
Name: "platform",
Usage: "imports content for specific platform",
},
cli.BoolFlag{
Name: "no-unpack",
Usage: "skip unpacking the images, false by default",
Expand All @@ -84,8 +89,9 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb

Action: func(context *cli.Context) error {
var (
in = context.Args().First()
opts []containerd.ImportOpt
in = context.Args().First()
opts []containerd.ImportOpt
platformMacher platforms.MatchComparer
)

prefix := context.String("base-name")
Expand All @@ -109,6 +115,15 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
opts = append(opts, containerd.WithImportCompression())
}

if platform := context.String("platform"); platform != "" {
platSpec, err := platforms.Parse(platform)
if err != nil {
return err
}
platformMacher = platforms.Only(platSpec)
opts = append(opts, containerd.WithImportPlatform(platformMacher))
}

opts = append(opts, containerd.WithAllPlatforms(context.Bool("all-platforms")))

client, ctx, cancel, err := commands.NewClient(context)
Expand Down Expand Up @@ -148,8 +163,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
log.G(ctx).Debugf("unpacking %d images", len(imgs))

for _, img := range imgs {
// TODO: Allow configuration of the platform
image := containerd.NewImage(client, img)
if platformMacher == nil { // if platform not specified use default.
platformMacher = platforms.Default()
}
image := containerd.NewImageWithPlatform(client, img, platformMacher)

// TODO: Show unpack status
fmt.Printf("unpacking %s (%s)...", img.Name, img.Target.Digest)
Expand Down

0 comments on commit b8f807f

Please sign in to comment.