Skip to content

Commit 49a810c

Browse files
committed
chore: add Memory and CgroupNsMode
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent e214bb3 commit 49a810c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ func TestContainerInspectHostConfig(t *testing.T) {
248248
"--add-host", "host1:10.0.0.1",
249249
"--add-host", "host2:10.0.0.2",
250250
"--ipc", "host",
251+
"--memory", "512m",
252+
"--oom-kill-disable",
251253
testutil.AlpineImage, "sleep", "infinity").AssertOK()
252254

253255
inspect := base.InspectContainer(testContainer)
@@ -263,6 +265,9 @@ func TestContainerInspectHostConfig(t *testing.T) {
263265
assert.Equal(t, "host", inspect.HostConfig.IpcMode)
264266
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type)
265267
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver)
268+
assert.Equal(t, int64(536870912), inspect.HostConfig.Memory)
269+
assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap)
270+
assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable)
266271
}
267272

268273
func TestContainerInspectHostConfigDefaults(t *testing.T) {
@@ -285,4 +290,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
285290
assert.Equal(t, "", inspect.HostConfig.IpcMode)
286291
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type)
287292
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver)
293+
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
294+
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
295+
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
288296
}

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ type HostConfig struct {
153153
ContainerIDFile string // File (path) where the containerId is written
154154
GroupAdd []string // GroupAdd specifies additional groups to join
155155
IpcMode string // IPC namespace to use for the container
156+
CgroupnsMode string // Cgroup namespace mode to use for the container
157+
Memory int64 // Memory limit (in bytes)
158+
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap
159+
OomKillDisable bool // specifies whether to disable OOM Killer
156160
}
157161

158162
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
@@ -409,6 +413,20 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
409413
c.HostConfig.CPUQuota = cpuSetting.cpuQuota
410414
c.HostConfig.CPUShares = cpuSetting.cpuShares
411415

416+
cgroupNamespace, err := getCgroupnsFromNative(n.Spec.(*specs.Spec))
417+
if err != nil {
418+
return nil, fmt.Errorf("failed to Decode cgroupNamespace: %v", err)
419+
}
420+
c.HostConfig.CgroupnsMode = cgroupNamespace
421+
422+
memorySettings, err := getMemorySettingsFromNative(n.Spec.(*specs.Spec))
423+
if err != nil {
424+
return nil, fmt.Errorf("failed to Decode memory Settings: %v", err)
425+
}
426+
427+
c.HostConfig.OomKillDisable = memorySettings.DisableOOMKiller
428+
c.HostConfig.Memory = memorySettings.Limit
429+
c.HostConfig.MemorySwap = memorySettings.Swap
412430
c.State = cs
413431
c.Config = &Config{
414432
Labels: n.Labels,
@@ -598,6 +616,18 @@ func cpuSettingsFromNative(sp *specs.Spec) (*CPUSettings, error) {
598616
return res, nil
599617
}
600618

619+
func getCgroupnsFromNative(sp *specs.Spec) (string, error) {
620+
res := ""
621+
if sp.Linux != nil && len(sp.Linux.Namespaces) != 0 {
622+
for _, ns := range sp.Linux.Namespaces {
623+
if ns.Type == "cgroup" {
624+
res = "private"
625+
}
626+
}
627+
}
628+
return res, nil
629+
}
630+
601631
func groupAddFromNative(sp *specs.Spec) ([]string, error) {
602632
res := []string{}
603633
if sp.Process != nil && sp.Process.User.AdditionalGids != nil {
@@ -637,6 +667,24 @@ func parseExtraHosts(extraHostsJSON string) []string {
637667
return extraHosts
638668
}
639669

670+
func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) {
671+
res := &MemorySetting{}
672+
if sp.Linux != nil && sp.Linux.Resources != nil && sp.Linux.Resources.Memory != nil {
673+
if sp.Linux.Resources.Memory.DisableOOMKiller != nil {
674+
res.DisableOOMKiller = *sp.Linux.Resources.Memory.DisableOOMKiller
675+
}
676+
677+
if sp.Linux.Resources.Memory.Limit != nil {
678+
res.Limit = *sp.Linux.Resources.Memory.Limit
679+
}
680+
681+
if sp.Linux.Resources.Memory.Swap != nil {
682+
res.Swap = *sp.Linux.Resources.Memory.Swap
683+
}
684+
}
685+
return res, nil
686+
}
687+
640688
type IPAMConfig struct {
641689
Subnet string `json:"Subnet,omitempty"`
642690
Gateway string `json:"Gateway,omitempty"`
@@ -667,6 +715,12 @@ type structuredCNI struct {
667715
} `json:"plugins"`
668716
}
669717

718+
type MemorySetting struct {
719+
Limit int64 `json:"limit"`
720+
Swap int64 `json:"swap"`
721+
DisableOOMKiller bool `json:"disableOOMKiller"`
722+
}
723+
670724
func NetworkFromNative(n *native.Network) (*Network, error) {
671725
var res Network
672726

0 commit comments

Comments
 (0)