From 9a8727cf097dbc14458de35a1ff5c649b2f8990c Mon Sep 17 00:00:00 2001 From: Aldo Culquicondor Date: Tue, 19 Mar 2019 10:55:55 -0400 Subject: [PATCH] Allow to import an image for the default platform only. Add `all-platforms` option to `ctr images import`. Signed-off-by: Aldo Culquicondor --- cmd/ctr/commands/images/import.go | 6 ++++++ import.go | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/ctr/commands/images/import.go b/cmd/ctr/commands/images/import.go index 2711ad0ee..53aa91d02 100644 --- a/cmd/ctr/commands/images/import.go +++ b/cmd/ctr/commands/images/import.go @@ -64,6 +64,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb Name: "index-name", Usage: "image name to keep index as, by default index is discarded", }, + cli.BoolFlag{ + Name: "all-platforms", + Usage: "imports content for all platforms, false by default", + }, }, commands.SnapshotterFlags...), Action: func(context *cli.Context) error { @@ -89,6 +93,8 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb opts = append(opts, containerd.WithIndexName(idxName)) } + opts = append(opts, containerd.WithAllPlatforms(context.Bool("all-platforms"))) + client, ctx, cancel, err := commands.NewClient(context) if err != nil { return err diff --git a/import.go b/import.go index e00f502a0..9825f3167 100644 --- a/import.go +++ b/import.go @@ -25,14 +25,16 @@ import ( "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/images" "github.com/containerd/containerd/images/archive" + "github.com/containerd/containerd/platforms" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) type importOpts struct { - indexName string - imageRefT func(string) string - dgstRefT func(digest.Digest) string + indexName string + imageRefT func(string) string + dgstRefT func(digest.Digest) string + allPlatforms bool } // ImportOpt allows the caller to specify import specific options @@ -64,6 +66,14 @@ func WithIndexName(name string) ImportOpt { } } +// WithAllPlatforms is used to import content for all platforms. +func WithAllPlatforms(allPlatforms bool) ImportOpt { + return func(c *importOpts) error { + c.allPlatforms = allPlatforms + return nil + } +} + // Import imports an image from a Tar stream using reader. // Caller needs to specify importer. Future version may use oci.v1 as the default. // Note that unreferrenced blobs may be imported to the content store as well. @@ -98,6 +108,10 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt Target: index, }) } + var platformMatcher = platforms.All + if !iopts.allPlatforms { + platformMatcher = platforms.Default() + } var handler images.HandlerFunc = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { // Only save images at top level @@ -141,6 +155,7 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt return idx.Manifests, nil } + handler = images.FilterPlatforms(handler, platformMatcher) handler = images.SetChildrenLabels(cs, handler) if err := images.Walk(ctx, handler, index); err != nil { return nil, err