Skip to content

Commit 3b9fa16

Browse files
committed
chore: add ReadonlyRootfs,UTSMode,ShmSize to inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 4c6ed57 commit 3b9fa16

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ func TestContainerInspectHostConfig(t *testing.T) {
250250
"--ipc", "host",
251251
"--memory", "512m",
252252
"--oom-kill-disable",
253+
"--read-only",
254+
"--uts", "host",
255+
"--shm-size", "256m",
253256
testutil.AlpineImage, "sleep", "infinity").AssertOK()
254257

255258
inspect := base.InspectContainer(testContainer)
@@ -268,6 +271,9 @@ func TestContainerInspectHostConfig(t *testing.T) {
268271
assert.Equal(t, int64(536870912), inspect.HostConfig.Memory)
269272
assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap)
270273
assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable)
274+
assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs)
275+
assert.Equal(t, "host", inspect.HostConfig.UTSMode)
276+
assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize)
271277
}
272278

273279
func TestContainerInspectHostConfigDefaults(t *testing.T) {
@@ -293,6 +299,9 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
293299
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
294300
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
295301
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
302+
assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs)
303+
assert.Equal(t, "", inspect.HostConfig.UTSMode)
304+
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
296305
}
297306

298307
func TestContainerInspectHostConfigDNS(t *testing.T) {

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"time"
3737

3838
"github.com/docker/go-connections/nat"
39+
"github.com/docker/go-units"
3940
"github.com/opencontainers/runtime-spec/specs-go"
4041

4142
containerd "github.com/containerd/containerd/v2/client"
@@ -161,6 +162,10 @@ type HostConfig struct {
161162
DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for
162163
DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for
163164
OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000)
165+
ReadonlyRootfs bool // Is the container root filesystem in read-only
166+
UTSMode string // UTS namespace to use for the container
167+
ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0.
168+
164169
}
165170

166171
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
@@ -450,6 +455,17 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
450455
oomScoreAdj, _ := getOomScoreAdjFromNative(n.Spec.(*specs.Spec))
451456
c.HostConfig.OomScoreAdj = oomScoreAdj
452457

458+
c.HostConfig.ReadonlyRootfs = false
459+
if n.Spec.(*specs.Spec).Root != nil && n.Spec.(*specs.Spec).Root.Readonly {
460+
c.HostConfig.ReadonlyRootfs = n.Spec.(*specs.Spec).Root.Readonly
461+
}
462+
463+
utsMode, _ := getUtsModeFromNative(n.Spec.(*specs.Spec))
464+
c.HostConfig.UTSMode = utsMode
465+
466+
shmSize, _ := getShmSizeFromNative(n.Spec.(*specs.Spec))
467+
c.HostConfig.ShmSize = shmSize
468+
453469
c.State = cs
454470
c.Config = &Config{
455471
Labels: n.Labels,
@@ -740,6 +756,39 @@ func getOomScoreAdjFromNative(sp *specs.Spec) (int, error) {
740756
return res, nil
741757
}
742758

759+
func getUtsModeFromNative(sp *specs.Spec) (string, error) {
760+
if sp.Linux != nil && len(sp.Linux.Namespaces) > 0 {
761+
for _, ns := range sp.Linux.Namespaces {
762+
if ns.Type == "uts" {
763+
return "", nil
764+
}
765+
}
766+
}
767+
return "host", nil
768+
}
769+
770+
func getShmSizeFromNative(sp *specs.Spec) (int64, error) {
771+
var res int64
772+
773+
if sp.Mounts != nil && len(sp.Mounts) > 0 {
774+
for _, mount := range sp.Mounts {
775+
if mount.Destination == "/dev/shm" {
776+
for _, option := range mount.Options {
777+
if strings.HasPrefix(option, "size=") {
778+
sizeStr := strings.TrimPrefix(option, "size=")
779+
size, err := units.RAMInBytes(sizeStr)
780+
if err != nil {
781+
return 0, fmt.Errorf("failed to parse shm size: %v", err)
782+
}
783+
res = size
784+
}
785+
}
786+
}
787+
}
788+
}
789+
return res, nil
790+
}
791+
743792
type IPAMConfig struct {
744793
Subnet string `json:"Subnet,omitempty"`
745794
Gateway string `json:"Gateway,omitempty"`

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func TestContainerFromNative(t *testing.T) {
8484
Opts: map[string]string{},
8585
},
8686
},
87+
UTSMode: "host",
8788
},
8889
Mounts: []MountPoint{
8990
{
@@ -169,6 +170,7 @@ func TestContainerFromNative(t *testing.T) {
169170
Opts: map[string]string{},
170171
},
171172
},
173+
UTSMode: "host",
172174
},
173175
Mounts: []MountPoint{
174176
{
@@ -251,6 +253,7 @@ func TestContainerFromNative(t *testing.T) {
251253
Opts: map[string]string{},
252254
},
253255
},
256+
UTSMode: "host",
254257
},
255258
Mounts: []MountPoint{
256259
{

0 commit comments

Comments
 (0)