Skip to content

Commit

Permalink
add label flags to ctr import
Browse files Browse the repository at this point in the history
Signed-off-by: roman-kiselenko <shindu666@gmail.com>
  • Loading branch information
roman-kiselenko committed Dec 6, 2023
1 parent 87bf39a commit 448c3b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions client/import.go
Expand Up @@ -37,6 +37,7 @@ type importOpts struct {
platformMatcher platforms.MatchComparer
compress bool
discardLayers bool
imageLabels map[string]string
}

// ImportOpt allows the caller to specify import specific options
Expand All @@ -51,6 +52,14 @@ func WithImageRefTranslator(f func(string) string) ImportOpt {
}
}

// WithImageLabels are the image labels to apply to a new image
func WithImageLabels(labels map[string]string) ImportOpt {
return func(c *importOpts) error {
c.imageLabels = labels
return nil
}
}

// WithDigestRef is used to create digest images for each
// manifest in the index.
func WithDigestRef(f func(digest.Digest) string) ImportOpt {
Expand Down Expand Up @@ -208,6 +217,7 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
}

for i := range imgs {
setImageLabels(&imgs[i], iopts.imageLabels)
img, err := is.Update(ctx, imgs[i], "target")
if err != nil {
if !errdefs.IsNotFound(err) {
Expand All @@ -225,6 +235,11 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
return imgs, nil
}

func setImageLabels(i *images.Image, labels map[string]string) {
i.Labels = map[string]string{}
i.Labels = labels
}

func imageName(annotations map[string]string, ociCleanup func(string) string) string {
name := annotations[images.AnnotationImageName]
if name != "" {
Expand Down
12 changes: 11 additions & 1 deletion cmd/ctr/commands/images/import.go
Expand Up @@ -98,7 +98,7 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
Name: "discard-unpacked-layers",
Usage: "Allow the garbage collector to clean layers up from the content store after unpacking, cannot be used with --no-unpack, false by default",
},
}, commands.SnapshotterFlags...),
}, append(commands.SnapshotterFlags, commands.LabelFlag)...),

Action: func(context *cli.Context) error {
var (
Expand All @@ -123,6 +123,11 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
overwrite = true
}

labels := context.StringSlice("label")
if len(labels) > 0 {
opts = append(opts, image.WithImageLabels(commands.LabelArgs(labels)))
}

if context.Bool("digests") {
opts = append(opts, image.WithDigestRef(prefix, overwrite, !context.Bool("skip-digest-for-named")))
} else {
Expand Down Expand Up @@ -237,6 +242,11 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
opts = append(opts, containerd.WithDiscardUnpackedLayers())
}

labels := context.StringSlice("label")
if len(labels) > 0 {
opts = append(opts, containerd.WithImageLabels(commands.LabelArgs(labels)))
}

ctx, done, err := client.WithLease(ctx)
if err != nil {
return err
Expand Down

0 comments on commit 448c3b0

Please sign in to comment.