Skip to content

Commit 4a0a980

Browse files
committed
support image label
Signed-off-by: Kay Yan <[email protected]>
1 parent b5758f5 commit 4a0a980

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

cmd/nerdctl/container_create_linux_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ func TestCreate(t *testing.T) {
3838
base.Cmd("logs", tID).AssertOutContains("foo")
3939
}
4040

41+
func TestCreateWithLabel(t *testing.T) {
42+
t.Parallel()
43+
base := testutil.NewBase(t)
44+
tID := testutil.Identifier(t)
45+
46+
base.Cmd("create", "--name", tID, "--label", "foo=bar", testutil.NginxAlpineImage, "echo", "foo").AssertOK()
47+
defer base.Cmd("rm", "-f", tID).Run()
48+
inspect := base.InspectContainer(tID)
49+
assert.Equal(base.T, "bar", inspect.Config.Labels["foo"])
50+
assert.Equal(base.T, "NGINX Docker Maintainers <[email protected]>", inspect.Config.Labels["maintainer"])
51+
}
52+
4153
func TestCreateWithMACAddress(t *testing.T) {
4254
base := testutil.NewBase(t)
4355
tID := testutil.Identifier(t)

pkg/cmd/container/create.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
234234
}
235235
cOpts = append(cOpts, rtCOpts...)
236236

237-
lCOpts, err := withContainerLabels(options.Label, options.LabelFile)
237+
lCOpts, err := withContainerLabels(options.Label, options.LabelFile, ensuredImage)
238238
if err != nil {
239239
return nil, nil, err
240240
}
@@ -504,7 +504,14 @@ func withNerdctlOCIHook(cmd string, args []string) (oci.SpecOpts, error) {
504504
}, nil
505505
}
506506

507-
func withContainerLabels(label, labelFile []string) ([]containerd.NewContainerOpts, error) {
507+
func withContainerLabels(label, labelFile []string, ensuredImage *imgutil.EnsuredImage) ([]containerd.NewContainerOpts, error) {
508+
var opts []containerd.NewContainerOpts
509+
510+
if ensuredImage != nil {
511+
imageLabelOpts := containerd.WithAdditionalContainerLabels(ensuredImage.ImageConfig.Labels)
512+
opts = append(opts, imageLabelOpts)
513+
}
514+
508515
labelMap, err := readKVStringsMapfFromLabel(label, labelFile)
509516
if err != nil {
510517
return nil, err
@@ -517,7 +524,9 @@ func withContainerLabels(label, labelFile []string) ([]containerd.NewContainerOp
517524
}
518525
}
519526
o := containerd.WithAdditionalContainerLabels(labelMap)
520-
return []containerd.NewContainerOpts{o}, nil
527+
opts = append(opts, o)
528+
529+
return opts, nil
521530
}
522531

523532
func readKVStringsMapfFromLabel(label, labelFile []string) (map[string]string, error) {

0 commit comments

Comments
 (0)