Skip to content

Commit da16d49

Browse files
committed
feat: support import image for specific platform
Signed-off-by: jonyhy <[email protected]>
1 parent d132691 commit da16d49

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

cmd/ctr/commands/images/import.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/containerd/containerd/cmd/ctr/commands"
2727
"github.com/containerd/containerd/images/archive"
2828
"github.com/containerd/containerd/log"
29+
"github.com/containerd/containerd/platforms"
2930
"github.com/urfave/cli"
3031
)
3132

@@ -72,6 +73,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
7273
Name: "all-platforms",
7374
Usage: "imports content for all platforms, false by default",
7475
},
76+
cli.BoolFlag{
77+
Name: "platform",
78+
Usage: "imports content for specific platform",
79+
},
7580
cli.BoolFlag{
7681
Name: "no-unpack",
7782
Usage: "skip unpacking the images, false by default",
@@ -84,8 +89,9 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
8489

8590
Action: func(context *cli.Context) error {
8691
var (
87-
in = context.Args().First()
88-
opts []containerd.ImportOpt
92+
in = context.Args().First()
93+
opts []containerd.ImportOpt
94+
platformMacher platforms.MatchComparer
8995
)
9096

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

124+
if platform := context.String("platform"); platform != "" {
125+
platSpec, err := platforms.Parse(platform)
126+
if err != nil {
127+
return err
128+
}
129+
platformMacher = platforms.Only(platSpec)
130+
opts = append(opts, containerd.WithImportPlatform(platformMacher))
131+
}
132+
118133
opts = append(opts, containerd.WithAllPlatforms(context.Bool("all-platforms")))
119134

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

147162
for _, img := range imgs {
148-
// TODO: Allow configuration of the platform
149-
image := containerd.NewImage(client, img)
163+
if platformMacher == nil { // if platform not specified use default.
164+
platformMacher = platforms.Default()
165+
}
166+
image := containerd.NewImageWithPlatform(client, img, platformMacher)
150167

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

import.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ import (
3131
)
3232

3333
type importOpts struct {
34-
indexName string
35-
imageRefT func(string) string
36-
dgstRefT func(digest.Digest) string
37-
skipDgstRef func(string) bool
38-
allPlatforms bool
39-
compress bool
34+
indexName string
35+
imageRefT func(string) string
36+
dgstRefT func(digest.Digest) string
37+
skipDgstRef func(string) bool
38+
allPlatforms bool
39+
platformMatcher platforms.MatchComparer
40+
compress bool
4041
}
4142

4243
// ImportOpt allows the caller to specify import specific options
@@ -87,6 +88,14 @@ func WithAllPlatforms(allPlatforms bool) ImportOpt {
8788
}
8889
}
8990

91+
// WithImportPlatform is used to import content for specific platform.
92+
func WithImportPlatform(platformMacher platforms.MatchComparer) ImportOpt {
93+
return func(c *importOpts) error {
94+
c.platformMatcher = platformMacher
95+
return nil
96+
}
97+
}
98+
9099
// WithImportCompression compresses uncompressed layers on import.
91100
// This is used for import formats which do not include the manifest.
92101
func WithImportCompression() ImportOpt {
@@ -135,9 +144,11 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
135144
Target: index,
136145
})
137146
}
138-
var platformMatcher = platforms.All
139-
if !iopts.allPlatforms {
140-
platformMatcher = c.platform
147+
var platformMatcher = c.platform
148+
if iopts.allPlatforms {
149+
platformMatcher = platforms.All
150+
} else if iopts.platformMatcher != nil {
151+
platformMatcher = iopts.platformMatcher
141152
}
142153

143154
var handler images.HandlerFunc = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {

0 commit comments

Comments
 (0)