From ff2a04e8b2faa02cdf9d932615b45642ddfbfdbd Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Tue, 11 Jan 2022 13:43:57 -0500 Subject: [PATCH] server: don't set memory swap when it's not enabled Signed-off-by: Peter Hunt --- server/container_create_linux.go | 6 +++++- test/cgroups.bats | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/server/container_create_linux.go b/server/container_create_linux.go index b84d9fb6d10..c315d3ced24 100644 --- a/server/container_create_linux.go +++ b/server/container_create_linux.go @@ -392,7 +392,11 @@ func (s *Server) createSandboxContainer(ctx context.Context, ctr ctrIface.Contai } memoryLimit = resources.MemorySwapLimitInBytes } - specgen.SetLinuxResourcesMemorySwap(memoryLimit) + // If node doesn't have memory swap, then skip setting + // otherwise the container creation fails. + if node.CgroupHasMemorySwap() { + specgen.SetLinuxResourcesMemorySwap(memoryLimit) + } } specgen.SetProcessOOMScoreAdj(int(resources.OomScoreAdj)) diff --git a/test/cgroups.bats b/test/cgroups.bats index 21b1c31292b..99ae3469043 100644 --- a/test/cgroups.bats +++ b/test/cgroups.bats @@ -72,6 +72,23 @@ function teardown() { ! crictl run "$newconfig" "$TESTDATA"/sandbox_config.json } +@test "ctr swap only configured if enabled" { + set_swap_fields_given_cgroup_version + if test -r "$CGROUP_MEM_SWAP_FILE"; then + skip "swap cgroup enabled" + fi + start_crio + # memsw should be greater than or equal to memory limit + # 210763776 = 1024*1024*200 + jq ' .linux.resources.memory_swap_limit_in_bytes = 210763776 + | .linux.resources.memory_limit_in_bytes = 209715200' \ + "$TESTDATA"/container_sleep.json > "$newconfig" + + ctr_id=$(crictl run "$newconfig" "$TESTDATA"/sandbox_config.json) + # verify CRI-O did not specify memory swap value + jq -e .linux.resources.memory.swap "$(runtime list | grep "$ctr_id" | awk '{ print $4 }')/config.json" +} + @test "cgroupv2 unified support" { if ! is_cgroup_v2; then skip "node must be configured with cgroupv2 for this test"