Skip to content

Commit 6875aa5

Browse files
committed
import: Add option to skip creating digest image
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent 8493cd1 commit 6875aa5

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

cmd/ctr/commands/images/import.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
6060
Name: "digests",
6161
Usage: "whether to create digest images (default: false)",
6262
},
63+
cli.BoolFlag{
64+
Name: "skip-digest-for-named",
65+
Usage: "skip applying --digests option to images named in the importing tar (use it in conjunction with --digests)",
66+
},
6367
cli.StringFlag{
6468
Name: "index-name",
6569
Usage: "image name to keep index as, by default index is discarded",
@@ -96,6 +100,12 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
96100
if context.Bool("digests") {
97101
opts = append(opts, containerd.WithDigestRef(archive.DigestTranslator(prefix)))
98102
}
103+
if context.Bool("skip-digest-for-named") {
104+
if !context.Bool("digests") {
105+
return fmt.Errorf("--skip-digest-for-named must be specified with --digests option")
106+
}
107+
opts = append(opts, containerd.WithSkipDigestRef(func(name string) bool { return name != "" }))
108+
}
99109

100110
if idxName := context.String("index-name"); idxName != "" {
101111
opts = append(opts, containerd.WithIndexName(idxName))

import.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type importOpts struct {
3434
indexName string
3535
imageRefT func(string) string
3636
dgstRefT func(digest.Digest) string
37+
skipDgstRef func(string) bool
3738
allPlatforms bool
3839
compress bool
3940
}
@@ -59,6 +60,17 @@ func WithDigestRef(f func(digest.Digest) string) ImportOpt {
5960
}
6061
}
6162

63+
// WithSkipDigestRef is used to specify when to skip applying
64+
// WithDigestRef. The callback receives an image reference (or an empty
65+
// string if not specified in the image). When the callback returns true,
66+
// the skip occurs.
67+
func WithSkipDigestRef(f func(string) bool) ImportOpt {
68+
return func(c *importOpts) error {
69+
c.skipDgstRef = f
70+
return nil
71+
}
72+
}
73+
6274
// WithIndexName creates a tag pointing to the imported index
6375
func WithIndexName(name string) ImportOpt {
6476
return func(c *importOpts) error {
@@ -152,6 +164,11 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
152164
Target: m,
153165
})
154166
}
167+
if iopts.skipDgstRef != nil {
168+
if iopts.skipDgstRef(name) {
169+
continue
170+
}
171+
}
155172
if iopts.dgstRefT != nil {
156173
ref := iopts.dgstRefT(m.Digest)
157174
if ref != "" {

0 commit comments

Comments
 (0)