Skip to content

Commit 715b54e

Browse files
committed
Fix: set Swappiness when no mem limits
This hard codes -1 on inspect for Docker's API to return `null` and be compatible Fixes: #23824 Signed-off-by: Nicola Sella <nsella@redhat.com>
1 parent 0c638b4 commit 715b54e

6 files changed

Lines changed: 19 additions & 5 deletions

File tree

docs/source/markdown/podman-container-inspect.1.md.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ $ podman container inspect foobar
296296
"KernelMemory": 0,
297297
"MemoryReservation": 0,
298298
"MemorySwap": 0,
299-
"MemorySwappiness": 0,
299+
"MemorySwappiness": -1,
300300
"OomKillDisable": false,
301301
"PidsLimit": 2048,
302302
"Ulimits": [],

libpod/container_inspect_freebsd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC
1111
// Not sure what to put here. FreeBSD jails use pids from the
1212
// global pool but can only see their own pids.
1313
hostConfig.PidMode = "host"
14+
hostConfig.MemorySwappiness = -1
1415

1516
// UTS namespace mode
1617
hostConfig.UTSMode = c.NamespaceMode(spec.UTSNamespace, ctrSpec)

libpod/container_inspect_linux.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC
5050
hostConfig.CpusetCpus = ctrSpec.Linux.Resources.CPU.Cpus
5151
hostConfig.CpusetMems = ctrSpec.Linux.Resources.CPU.Mems
5252
}
53+
hostConfig.MemorySwappiness = -1
5354
if ctrSpec.Linux.Resources.Memory != nil {
5455
if ctrSpec.Linux.Resources.Memory.Limit != nil {
5556
hostConfig.Memory = *ctrSpec.Linux.Resources.Memory.Limit
@@ -62,9 +63,6 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC
6263
}
6364
if ctrSpec.Linux.Resources.Memory.Swappiness != nil {
6465
hostConfig.MemorySwappiness = int64(*ctrSpec.Linux.Resources.Memory.Swappiness)
65-
} else {
66-
// Swappiness has a default of -1
67-
hostConfig.MemorySwappiness = -1
6866
}
6967
if ctrSpec.Linux.Resources.Memory.DisableOOMKiller != nil {
7068
hostConfig.OomKillDisable = *ctrSpec.Linux.Resources.Memory.DisableOOMKiller

pkg/api/handlers/compat/containers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,11 @@ func LibpodToContainerJSON(l *libpod.Container, sz bool) (*handlers.LegacyImageI
543543
hc.CgroupnsMode = container.CgroupnsModeHost
544544
}
545545

546+
// Docker reports null instead of -1 for unset Swappiness
547+
if hc.MemorySwappiness != nil && *hc.MemorySwappiness == -1 {
548+
hc.MemorySwappiness = nil
549+
}
550+
546551
// k8s-file == json-file
547552
if hc.LogConfig.Type == define.KubernetesLogging {
548553
hc.LogConfig.Type = define.JSONLogging

test/apiv2/20-containers.at

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,16 @@ t GET containers/$cid/json 200 \
870870
t DELETE containers/$cid 204
871871
podman rmi test1
872872

873+
# test MemorySwappiness
874+
podman run -d --rm --name swappiness_with_mem_limit --memory 200M $IMAGE top
875+
podman run -d --rm --name swappiness_without_mem_limit $IMAGE top
876+
877+
t GET containers/swappiness_with_mem_limit/json 200 \
878+
.HostConfig.MemorySwappiness=null
879+
t GET containers/swappiness_without_mem_limit/json 200 \
880+
.HostConfig.MemorySwappiness=null
881+
882+
podman stop -t0 swappiness_with_mem_limit swappiness_without_mem_limit
873883

874884
# test if API support -1 for ulimits https://github.com/containers/podman/issues/24886
875885

test/e2e/container_clone_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ var _ = Describe("Podman container clone", func() {
140140
cloneInspect.WaitWithDefaultTimeout()
141141
Expect(cloneInspect).To(ExitCleanly())
142142
cloneData = cloneInspect.InspectContainerToJSON()
143-
Expect(cloneData[0].HostConfig).To(HaveField("MemorySwappiness", int64(0)))
143+
Expect(cloneData[0].HostConfig).To(HaveField("MemorySwappiness", int64(-1)))
144144
})
145145

146146
It("podman container clone in a pod", func() {

0 commit comments

Comments
 (0)