Skip to content

Commit 64ad424

Browse files
committed
chore: add pidMode to inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent a71684a commit 64ad424

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,39 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
362362
assert.Equal(t, 0, len(inspect.HostConfig.DNSSearch))
363363
assert.Equal(t, 0, len(inspect.HostConfig.DNSOptions))
364364
}
365+
366+
func TestContainerInspectHostConfigPID(t *testing.T) {
367+
testContainer1 := testutil.Identifier(t)
368+
testContainer2 := testutil.Identifier(t)
369+
370+
base := testutil.NewBase(t)
371+
defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run()
372+
373+
// Run the first container
374+
base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK()
375+
376+
// Run a container with PID namespace options
377+
base.Cmd("run", "-d", "--name", testContainer2,
378+
"--pid", fmt.Sprintf("container:%s", testContainer1),
379+
testutil.AlpineImage, "sleep", "infinity").AssertOK()
380+
381+
inspect := base.InspectContainer(testContainer2)
382+
383+
assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode)
384+
385+
}
386+
387+
func TestContainerInspectHostConfigPIDDefaults(t *testing.T) {
388+
testContainer := testutil.Identifier(t)
389+
390+
base := testutil.NewBase(t)
391+
defer base.Cmd("rm", "-f", testContainer).Run()
392+
393+
// Run a container without specifying PID options
394+
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
395+
396+
inspect := base.InspectContainer(testContainer)
397+
398+
// Check that PID mode is empty (private) by default
399+
assert.Equal(t, "", inspect.HostConfig.PidMode)
400+
}

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ type HostConfig struct {
163163
Sysctls map[string]string // List of Namespaced sysctls used for the container
164164
Runtime string // Runtime to use with this container
165165
Devices []string // List of devices to map inside the container
166+
PidMode string // PID namespace to use for the container
167+
Tmpfs []MountPoint `json:",omitempty"` // List of tmpfs (mounts) used for the container
166168
}
167169

168170
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
@@ -292,6 +294,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
292294
// XXX is this always right? what if the container OS is NOT the same as the host OS?
293295
Platform: runtime.GOOS, // for Docker compatibility, this Platform string does NOT contain arch like "/amd64"
294296
}
297+
c.HostConfig = new(HostConfig)
295298
if n.Labels[restart.StatusLabel] == string(containerd.Running) {
296299
c.RestartCount, _ = strconv.Atoi(n.Labels[restart.CountLabel])
297300
}
@@ -332,15 +335,20 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
332335
}
333336
}
334337

338+
var tmpfsMounts []MountPoint
339+
335340
if nerdctlMounts := n.Labels[labels.Mounts]; nerdctlMounts != "" {
336341
mounts, err := parseMounts(nerdctlMounts)
337342
if err != nil {
338343
return nil, err
339344
}
340345
c.Mounts = mounts
346+
if len(mounts) > 0 {
347+
tmpfsMounts = filterTmpfsMounts(mounts)
348+
}
341349
}
350+
c.HostConfig.Tmpfs = tmpfsMounts
342351

343-
c.HostConfig = new(HostConfig)
344352
if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" {
345353
c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts)
346354
}
@@ -366,7 +374,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
366374
}
367375

368376
// var hostConfigLabel HostConfigLabel
369-
hostConfigLabel, err := getHostConfigLabelFromNative(n.Labels)
377+
hostConfigLabel, _ := getHostConfigLabelFromNative(n.Labels)
370378

371379
c.HostConfig.BlkioWeight = hostConfigLabel.BlkioWeight
372380
c.HostConfig.ContainerIDFile = hostConfigLabel.CidFile
@@ -480,6 +488,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
480488

481489
c.HostConfig.Devices = hostConfigLabel.DeviceMapping
482490

491+
var pidMode string
492+
if n.Labels[labels.PIDContainer] != "" {
493+
pidMode = n.Labels[labels.PIDContainer]
494+
}
495+
c.HostConfig.PidMode = pidMode
483496
return c, nil
484497
}
485498

@@ -550,6 +563,18 @@ func mountsFromNative(spMounts []specs.Mount) []MountPoint {
550563
return mountpoints
551564
}
552565

566+
// filterTmpfsMounts filters the tmpfs mounts
567+
func filterTmpfsMounts(spMounts []MountPoint) []MountPoint {
568+
mountpoints := make([]MountPoint, 0, len(spMounts))
569+
for _, m := range spMounts {
570+
if m.Type == "tmpfs" {
571+
mountpoints = append(mountpoints, m)
572+
}
573+
}
574+
575+
return mountpoints
576+
}
577+
553578
func statusFromNative(x containerd.Status, labels map[string]string) string {
554579
switch s := x.Status; s {
555580
case containerd.Stopped:
@@ -799,15 +824,6 @@ func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) {
799824
return res, nil
800825
}
801826

802-
func parseDeviceMapping(deviceMappingJSON string) ([]string, error) {
803-
var devices []string
804-
err := json.Unmarshal([]byte(deviceMappingJSON), &devices)
805-
if err != nil {
806-
return nil, fmt.Errorf("failed to parse device mapping: %v", err)
807-
}
808-
return devices, nil
809-
}
810-
811827
type IPAMConfig struct {
812828
Subnet string `json:"Subnet,omitempty"`
813829
Gateway string `json:"Gateway,omitempty"`

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestContainerFromNative(t *testing.T) {
8383
Opts: map[string]string{},
8484
},
8585
UTSMode: "host",
86-
Devices: []string{},
86+
Tmpfs: []MountPoint{},
8787
},
8888
Mounts: []MountPoint{
8989
{
@@ -168,7 +168,6 @@ func TestContainerFromNative(t *testing.T) {
168168
Opts: map[string]string{},
169169
},
170170
UTSMode: "host",
171-
Devices: []string{},
172171
},
173172
Mounts: []MountPoint{
174173
{
@@ -250,7 +249,6 @@ func TestContainerFromNative(t *testing.T) {
250249
Opts: map[string]string{},
251250
},
252251
UTSMode: "host",
253-
Devices: []string{},
254252
},
255253
Mounts: []MountPoint{
256254
{

0 commit comments

Comments
 (0)