diff --git a/cmd/ctr/commands/images/import.go b/cmd/ctr/commands/images/import.go index e7a494579..ab1fbb2cd 100644 --- a/cmd/ctr/commands/images/import.go +++ b/cmd/ctr/commands/images/import.go @@ -60,6 +60,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb Name: "digests", Usage: "whether to create digest images (default: false)", }, + cli.BoolFlag{ + Name: "skip-digest-for-named", + Usage: "skip applying --digests option to images named in the importing tar (use it in conjunction with --digests)", + }, cli.StringFlag{ Name: "index-name", 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 if context.Bool("digests") { opts = append(opts, containerd.WithDigestRef(archive.DigestTranslator(prefix))) } + if context.Bool("skip-digest-for-named") { + if !context.Bool("digests") { + return fmt.Errorf("--skip-digest-for-named must be specified with --digests option") + } + opts = append(opts, containerd.WithSkipDigestRef(func(name string) bool { return name != "" })) + } if idxName := context.String("index-name"); idxName != "" { opts = append(opts, containerd.WithIndexName(idxName)) diff --git a/import.go b/import.go index 6080161f8..2ba717448 100644 --- a/import.go +++ b/import.go @@ -34,6 +34,7 @@ type importOpts struct { indexName string imageRefT func(string) string dgstRefT func(digest.Digest) string + skipDgstRef func(string) bool allPlatforms bool compress bool } @@ -59,6 +60,17 @@ func WithDigestRef(f func(digest.Digest) string) ImportOpt { } } +// WithSkipDigestRef is used to specify when to skip applying +// WithDigestRef. The callback receives an image reference (or an empty +// string if not specified in the image). When the callback returns true, +// the skip occurs. +func WithSkipDigestRef(f func(string) bool) ImportOpt { + return func(c *importOpts) error { + c.skipDgstRef = f + return nil + } +} + // WithIndexName creates a tag pointing to the imported index func WithIndexName(name string) ImportOpt { return func(c *importOpts) error { @@ -152,6 +164,11 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt Target: m, }) } + if iopts.skipDgstRef != nil { + if iopts.skipDgstRef(name) { + continue + } + } if iopts.dgstRefT != nil { ref := iopts.dgstRefT(m.Digest) if ref != "" {