Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add env and user to inspect in dockercompat #4007

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/nerdctl/container/container_inspect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (
"github.com/docker/go-connections/nat"
"gotest.tools/v3/assert"

"github.com/containerd/nerdctl/mod/tigron/expect"
"github.com/containerd/nerdctl/mod/tigron/test"
"github.com/containerd/nerdctl/v2/pkg/infoutil"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)

func TestContainerInspectContainsPortConfig(t *testing.T) {
Expand Down Expand Up @@ -456,6 +459,23 @@ func TestContainerInspectDevices(t *testing.T) {
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
}

func TestContainerInspectUser(t *testing.T) {
nerdtest.Setup()

testCase := &test.Case{
Description: "Container inspect contains User",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("create", "--name", data.Identifier(), "--user", "test", testutil.AlpineImage)
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("inspect", "--format", "{{.Config.User}}", data.Identifier())
},
Expected: test.Expects(0, nil, expect.Equals("test\n")),
}

testCase.Run(t)
}

type hostConfigValues struct {
Driver string
ShmSize int64
Expand Down
15 changes: 15 additions & 0 deletions pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
}
}

if ensuredImage.ImageConfig.User != "" {
internalLabels.user = ensuredImage.ImageConfig.User
}
// Override it if User is passed
if options.User != "" {
internalLabels.user = options.User
}

rootfsOpts, rootfsCOpts, err := generateRootfsOpts(args, id, ensuredImage, options)
if err != nil {
return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err
Expand Down Expand Up @@ -271,6 +279,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
if err != nil {
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
}

opts = append(opts, uOpts...)
gOpts, err := generateGroupsOpts(options.GroupAdd)
internalLabels.groupAdd = options.GroupAdd
Expand Down Expand Up @@ -665,6 +674,8 @@ type internalLabels struct {

// label for device mapping set by the --device flag
deviceMapping []dockercompat.DeviceMapping

user string
}

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

if internalLabels.user != "" {
m[labels.User] = internalLabels.user
}

return containerd.WithAdditionalContainerLabels(m), nil
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,17 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
pidMode = n.Labels[labels.PIDContainer]
}
c.HostConfig.PidMode = pidMode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should c.Config.Env be initialized as an empty list or should the else condition leave the Env key-value pair, in the case that the conditional criteria fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty list as not having the key can lead to errors downstream if they query for env from the unmarshalled json and they dont handle key not being present. And also would represent if container process has no set env variables.

if n.Spec != nil {
if spec, ok := n.Spec.(*specs.Spec); ok && spec.Process != nil {
c.Config.Env = spec.Process.Env
}
}

if n.Labels[labels.User] != "" {
c.Config.User = n.Labels[labels.User]
}

return c, nil
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/inspecttypes/dockercompat/dockercompat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ func TestContainerFromNative(t *testing.T) {
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
"nerdctl/state-dir": tempStateDir,
"nerdctl/hostname": "host1",
"nerdctl/user": "test-user",
},
},
Spec: &specs.Spec{
Process: &specs.Process{
Env: []string{"/some/path"},
},
},
Spec: &specs.Spec{},
Process: &native.Process{
Pid: 10000,
Status: containerd.Status{
Expand Down Expand Up @@ -100,8 +105,11 @@ func TestContainerFromNative(t *testing.T) {
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
"nerdctl/state-dir": tempStateDir,
"nerdctl/hostname": "host1",
"nerdctl/user": "test-user",
},
Hostname: "host1",
Env: []string{"/some/path"},
User: "test-user",
},
NetworkSettings: &NetworkSettings{
Ports: &nat.PortMap{},
Expand Down
3 changes: 3 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,7 @@ const (

// DNSSettings sets the dockercompat DNS config values
DNSSetting = Prefix + "dns"

// User is the username of the container
User = Prefix + "user"
)
Loading