Skip to content

Commit

Permalink
chore: add pidMode to inspect response
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Raja Yogidas <[email protected]>
  • Loading branch information
coderbirju committed Jan 22, 2025
1 parent a71684a commit 0b7af18
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
36 changes: 36 additions & 0 deletions cmd/nerdctl/container/container_inspect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,39 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
assert.Equal(t, 0, len(inspect.HostConfig.DNSSearch))
assert.Equal(t, 0, len(inspect.HostConfig.DNSOptions))
}

func TestContainerInspectHostConfigPID(t *testing.T) {
testContainer1 := testutil.Identifier(t)
testContainer2 := testutil.Identifier(t)

base := testutil.NewBase(t)
defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run()

// Run the first container
base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK()

// Run a container with PID namespace options
base.Cmd("run", "-d", "--name", testContainer2,
"--pid", fmt.Sprintf("container:%s", testContainer1),
testutil.AlpineImage, "sleep", "infinity").AssertOK()

inspect := base.InspectContainer(testContainer2)

assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode)

}

func TestContainerInspectHostConfigPIDDefaults(t *testing.T) {
testContainer := testutil.Identifier(t)

base := testutil.NewBase(t)
defer base.Cmd("rm", "-f", testContainer).Run()

// Run a container without specifying PID options
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()

inspect := base.InspectContainer(testContainer)

// Check that PID mode is empty (private) by default
assert.Equal(t, "", inspect.HostConfig.PidMode)
}
6 changes: 6 additions & 0 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type HostConfig struct {
Sysctls map[string]string // List of Namespaced sysctls used for the container
Runtime string // Runtime to use with this container
Devices []string // List of devices to map inside the container
PidMode string // PID namespace to use for the container
}

// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
Expand Down Expand Up @@ -480,6 +481,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) {

c.HostConfig.Devices = hostConfigLabel.DeviceMapping

var pidMode string
if n.Labels[labels.PIDContainer] != "" {
pidMode = n.Labels[labels.PIDContainer]
}
c.HostConfig.PidMode = pidMode
return c, nil
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/inspecttypes/dockercompat/dockercompat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func TestContainerFromNative(t *testing.T) {
Opts: map[string]string{},
},
UTSMode: "host",
Devices: []string{},
},
Mounts: []MountPoint{
{
Expand Down Expand Up @@ -168,7 +167,6 @@ func TestContainerFromNative(t *testing.T) {
Opts: map[string]string{},
},
UTSMode: "host",
Devices: []string{},
},
Mounts: []MountPoint{
{
Expand Down Expand Up @@ -250,7 +248,6 @@ func TestContainerFromNative(t *testing.T) {
Opts: map[string]string{},
},
UTSMode: "host",
Devices: []string{},
},
Mounts: []MountPoint{
{
Expand Down

0 comments on commit 0b7af18

Please sign in to comment.