Skip to content

Commit 9ab4c8c

Browse files
authored
Merge pull request #3108 from alculquicondor/fix/import
Allow to import an image for the default platform only.
2 parents 0cbbd0f + 9a8727c commit 9ab4c8c

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

cmd/ctr/commands/images/import.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
6464
Name: "index-name",
6565
Usage: "image name to keep index as, by default index is discarded",
6666
},
67+
cli.BoolFlag{
68+
Name: "all-platforms",
69+
Usage: "imports content for all platforms, false by default",
70+
},
6771
}, commands.SnapshotterFlags...),
6872

6973
Action: func(context *cli.Context) error {
@@ -89,6 +93,8 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
8993
opts = append(opts, containerd.WithIndexName(idxName))
9094
}
9195

96+
opts = append(opts, containerd.WithAllPlatforms(context.Bool("all-platforms")))
97+
9298
client, ctx, cancel, err := commands.NewClient(context)
9399
if err != nil {
94100
return err

import.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ import (
2525
"github.com/containerd/containerd/errdefs"
2626
"github.com/containerd/containerd/images"
2727
"github.com/containerd/containerd/images/archive"
28+
"github.com/containerd/containerd/platforms"
2829
digest "github.com/opencontainers/go-digest"
2930
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3031
)
3132

3233
type importOpts struct {
33-
indexName string
34-
imageRefT func(string) string
35-
dgstRefT func(digest.Digest) string
34+
indexName string
35+
imageRefT func(string) string
36+
dgstRefT func(digest.Digest) string
37+
allPlatforms bool
3638
}
3739

3840
// ImportOpt allows the caller to specify import specific options
@@ -64,6 +66,14 @@ func WithIndexName(name string) ImportOpt {
6466
}
6567
}
6668

69+
// WithAllPlatforms is used to import content for all platforms.
70+
func WithAllPlatforms(allPlatforms bool) ImportOpt {
71+
return func(c *importOpts) error {
72+
c.allPlatforms = allPlatforms
73+
return nil
74+
}
75+
}
76+
6777
// Import imports an image from a Tar stream using reader.
6878
// Caller needs to specify importer. Future version may use oci.v1 as the default.
6979
// 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
98108
Target: index,
99109
})
100110
}
111+
var platformMatcher = platforms.All
112+
if !iopts.allPlatforms {
113+
platformMatcher = platforms.Default()
114+
}
101115

102116
var handler images.HandlerFunc = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
103117
// Only save images at top level
@@ -141,6 +155,7 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
141155
return idx.Manifests, nil
142156
}
143157

158+
handler = images.FilterPlatforms(handler, platformMatcher)
144159
handler = images.SetChildrenLabels(cs, handler)
145160
if err := images.Walk(ctx, handler, index); err != nil {
146161
return nil, err

0 commit comments

Comments
 (0)