From a274dbe82258d40e8fd09dc2c8f4331717e1045d Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Thu, 23 May 2019 11:57:53 -0700 Subject: [PATCH] Fix run with specified platform Adds the platform flag to the run command and resolves the image based on that platform. Signed-off-by: Derek McGowan --- cmd/ctr/commands/run/run.go | 4 ++++ cmd/ctr/commands/run/run_unix.go | 14 +++++++++++++- container_opts.go | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/ctr/commands/run/run.go b/cmd/ctr/commands/run/run.go index 41f11a1c2..43e0cfeea 100644 --- a/cmd/ctr/commands/run/run.go +++ b/cmd/ctr/commands/run/run.go @@ -115,6 +115,10 @@ var Command = cli.Command{ Name: "cgroup", Usage: "cgroup path (To disable use of cgroup, set to \"\" explicitly)", }, + cli.StringFlag{ + Name: "platform", + Usage: "run image for specific platform", + }, }, append(platformRunFlags, append(commands.SnapshotterFlags, commands.ContainerFlags...)...)...), Action: func(context *cli.Context) error { var ( diff --git a/cmd/ctr/commands/run/run_unix.go b/cmd/ctr/commands/run/run_unix.go index 8ecf3d03a..c538b945c 100644 --- a/cmd/ctr/commands/run/run_unix.go +++ b/cmd/ctr/commands/run/run_unix.go @@ -27,6 +27,7 @@ import ( "github.com/containerd/containerd/cmd/ctr/commands" "github.com/containerd/containerd/contrib/nvidia" "github.com/containerd/containerd/oci" + "github.com/containerd/containerd/platforms" "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" "github.com/urfave/cli" @@ -73,10 +74,21 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli opts = append(opts, oci.WithRootFSPath(rootfs)) } else { snapshotter := context.String("snapshotter") - image, err := client.GetImage(ctx, ref) + var image containerd.Image + i, err := client.ImageService().Get(ctx, ref) if err != nil { return nil, err } + if ps := context.String("platform"); ps != "" { + platform, err := platforms.Parse(ps) + if err != nil { + return nil, err + } + image = containerd.NewImageWithPlatform(client, i, platforms.Only(platform)) + } else { + image = containerd.NewImage(client, i) + } + unpacked, err := image.IsUnpacked(ctx, snapshotter) if err != nil { return nil, err diff --git a/container_opts.go b/container_opts.go index 25b8f9730..a65f429ae 100644 --- a/container_opts.go +++ b/container_opts.go @@ -132,7 +132,7 @@ func WithSnapshot(id string) NewContainerOpts { // root filesystem in read-write mode func WithNewSnapshot(id string, i Image, opts ...snapshots.Opt) NewContainerOpts { return func(ctx context.Context, client *Client, c *containers.Container) error { - diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Default()) + diffIDs, err := i.RootFS(ctx) if err != nil { return err }