Skip to content

Commit 563925c

Browse files
add user to inspect
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent b0448ee commit 563925c

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ import (
2626
"github.com/docker/go-connections/nat"
2727
"gotest.tools/v3/assert"
2828

29+
"github.com/containerd/nerdctl/mod/tigron/expect"
30+
"github.com/containerd/nerdctl/mod/tigron/test"
2931
"github.com/containerd/nerdctl/v2/pkg/infoutil"
3032
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3133
"github.com/containerd/nerdctl/v2/pkg/labels"
3234
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3335
"github.com/containerd/nerdctl/v2/pkg/testutil"
36+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3437
)
3538

3639
func TestContainerInspectContainsPortConfig(t *testing.T) {
@@ -456,6 +459,23 @@ func TestContainerInspectDevices(t *testing.T) {
456459
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
457460
}
458461

462+
func TestContainerInspectUser(t *testing.T) {
463+
nerdtest.Setup()
464+
465+
testCase := &test.Case{
466+
Description: "Container inspect contains User",
467+
Setup: func(data test.Data, helpers test.Helpers) {
468+
helpers.Ensure("create", "--name", data.Identifier(), "--user", "test", testutil.AlpineImage)
469+
},
470+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
471+
return helpers.Command("inspect", "--format", "{{.Config.User}}", data.Identifier())
472+
},
473+
Expected: test.Expects(0, nil, expect.Equals("test\n")),
474+
}
475+
476+
testCase.Run(t)
477+
}
478+
459479
type hostConfigValues struct {
460480
Driver string
461481
ShmSize int64

pkg/cmd/container/create.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
179179
}
180180
}
181181

182+
if ensuredImage.ImageConfig.User != "" {
183+
internalLabels.user = ensuredImage.ImageConfig.User
184+
}
185+
// Override it if User is passed
186+
if options.User != "" {
187+
internalLabels.user = options.User
188+
}
189+
182190
rootfsOpts, rootfsCOpts, err := generateRootfsOpts(args, id, ensuredImage, options)
183191
if err != nil {
184192
return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err
@@ -271,6 +279,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
271279
if err != nil {
272280
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
273281
}
282+
274283
opts = append(opts, uOpts...)
275284
gOpts, err := generateGroupsOpts(options.GroupAdd)
276285
internalLabels.groupAdd = options.GroupAdd
@@ -665,6 +674,8 @@ type internalLabels struct {
665674

666675
// label for device mapping set by the --device flag
667676
deviceMapping []dockercompat.DeviceMapping
677+
678+
user string
668679
}
669680

670681
// WithInternalLabels sets the internal labels for a container.
@@ -790,6 +801,10 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
790801
}
791802
m[labels.DNSSetting] = string(dnsSettingsJSON)
792803

804+
if internalLabels.user != "" {
805+
m[labels.User] = internalLabels.user
806+
}
807+
793808
return containerd.WithAdditionalContainerLabels(m), nil
794809
}
795810

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
546546
c.Config.Env = spec.Process.Env
547547
}
548548
}
549+
550+
if n.Labels[labels.User] != "" {
551+
c.Config.User = n.Labels[labels.User]
552+
}
553+
549554
return c, nil
550555
}
551556

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestContainerFromNative(t *testing.T) {
5555
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
5656
"nerdctl/state-dir": tempStateDir,
5757
"nerdctl/hostname": "host1",
58+
"nerdctl/user": "test-user",
5859
},
5960
},
6061
Spec: &specs.Spec{
@@ -104,9 +105,11 @@ func TestContainerFromNative(t *testing.T) {
104105
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
105106
"nerdctl/state-dir": tempStateDir,
106107
"nerdctl/hostname": "host1",
108+
"nerdctl/user": "test-user",
107109
},
108110
Hostname: "host1",
109111
Env: []string{"/some/path"},
112+
User: "test-user",
110113
},
111114
NetworkSettings: &NetworkSettings{
112115
Ports: &nat.PortMap{},

pkg/labels/labels.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ const (
115115

116116
// DNSSettings sets the dockercompat DNS config values
117117
DNSSetting = Prefix + "dns"
118+
119+
// User is the username of the container
120+
User = Prefix + "user"
118121
)

0 commit comments

Comments
 (0)