Skip to content

Commit 781d027

Browse files
authored
Merge pull request containerd#9476 from roman-kiselenko/label-images-on-import
add label flags to ctr import
2 parents aa66354 + a3c62e6 commit 781d027

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

client/import.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type importOpts struct {
3838
compress bool
3939
discardLayers bool
4040
skipMissing bool
41+
imageLabels map[string]string
4142
}
4243

4344
// ImportOpt allows the caller to specify import specific options
@@ -52,6 +53,14 @@ func WithImageRefTranslator(f func(string) string) ImportOpt {
5253
}
5354
}
5455

56+
// WithImageLabels are the image labels to apply to a new image
57+
func WithImageLabels(labels map[string]string) ImportOpt {
58+
return func(c *importOpts) error {
59+
c.imageLabels = labels
60+
return nil
61+
}
62+
}
63+
5564
// WithDigestRef is used to create digest images for each
5665
// manifest in the index.
5766
func WithDigestRef(f func(digest.Digest) string) ImportOpt {
@@ -223,7 +232,12 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
223232
}
224233

225234
for i := range imgs {
226-
img, err := is.Update(ctx, imgs[i], "target")
235+
fieldsPath := []string{"target"}
236+
if iopts.imageLabels != nil {
237+
fieldsPath = append(fieldsPath, "labels")
238+
imgs[i].Labels = iopts.imageLabels
239+
}
240+
img, err := is.Update(ctx, imgs[i], fieldsPath...)
227241
if err != nil {
228242
if !errdefs.IsNotFound(err) {
229243
return nil, err

cmd/ctr/commands/images/import.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
9898
Name: "discard-unpacked-layers",
9999
Usage: "Allow the garbage collector to clean layers up from the content store after unpacking, cannot be used with --no-unpack, false by default",
100100
},
101-
}, commands.SnapshotterFlags...),
101+
}, append(commands.SnapshotterFlags, commands.LabelFlag)...),
102102

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

126+
labels := context.StringSlice("label")
127+
if len(labels) > 0 {
128+
opts = append(opts, image.WithImageLabels(commands.LabelArgs(labels)))
129+
}
130+
126131
if context.Bool("digests") {
127132
opts = append(opts, image.WithDigestRef(prefix, overwrite, !context.Bool("skip-digest-for-named")))
128133
} else {
@@ -237,6 +242,11 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
237242
opts = append(opts, containerd.WithDiscardUnpackedLayers())
238243
}
239244

245+
labels := context.StringSlice("label")
246+
if len(labels) > 0 {
247+
opts = append(opts, containerd.WithImageLabels(commands.LabelArgs(labels)))
248+
}
249+
240250
ctx, done, err := client.WithLease(ctx)
241251
if err != nil {
242252
return err

integration/client/import_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ func TestImport(t *testing.T) {
173173

174174
m1, d3, expManifest := createManifest(c1, [][]byte{b1})
175175

176+
importLabels := map[string]string{"foo": "bar"}
177+
176178
c2, _ := createConfig(runtime.GOOS, runtime.GOARCH, "test2")
177179
m2, d5, _ := createManifest(c2, [][]byte{{1, 2, 3, 4, 5}})
178180

@@ -315,6 +317,29 @@ func TestImport(t *testing.T) {
315317
checkManifest(ctx, t, client.ContentStore(), imgs[0].Target, expManifest)
316318
},
317319
},
320+
{
321+
Name: "OCI-Labels",
322+
Writer: tartest.TarAll(
323+
tc.Dir(ocispec.ImageBlobsDir, 0o755),
324+
tc.Dir(ocispec.ImageBlobsDir+"/sha256", 0o755),
325+
tc.File(ocispec.ImageBlobsDir+"/sha256/"+d1.Encoded(), b1, 0o644),
326+
tc.File(ocispec.ImageBlobsDir+"/sha256/"+d2.Encoded(), c1, 0o644),
327+
tc.File(ocispec.ImageBlobsDir+"/sha256/"+d3.Encoded(), m1, 0o644),
328+
tc.File(ocispec.ImageIndexFile, createIndex(m1, "latest", "docker.io/lib/img:ok"), 0o644),
329+
tc.File(ocispec.ImageLayoutFile, []byte(`{"imageLayoutVersion":"`+ocispec.ImageLayoutVersion+`"}`), 0o644),
330+
),
331+
Check: func(ctx context.Context, t *testing.T, _ *Client, imgs []images.Image) {
332+
for i := range imgs {
333+
if !reflect.DeepEqual(imgs[i].Labels, importLabels) {
334+
t.Fatalf("DeepEqual on labels failed img.Labels: %+v expected: %+v", imgs[i].Labels, importLabels)
335+
}
336+
}
337+
},
338+
Opts: []ImportOpt{
339+
WithImageLabels(importLabels),
340+
WithImageRefTranslator(archive.AddRefPrefix("localhost:5000/myimage")),
341+
},
342+
},
318343
{
319344
Name: "OCIPrefixName",
320345
Writer: tartest.TarAll(

0 commit comments

Comments
 (0)