Skip to content

Commit 91a68ed

Browse files
mqasimsarfrazthaJeztah
authored andcommitted
cri: Fix TestUpdateOCILinuxResource for host w/o swap controller
Tested on Ubuntu 20.04 w/o swap controller: ``` $ stat -fc %T /sys/fs/cgroup/ tmpfs $ la -la /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes ls: cannot access '/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes': No such file or directory $ go test -v ./pkg/cri/sbserver/ -run TestUpdateOCILinuxResource === RUN TestUpdateOCILinuxResource === RUN TestUpdateOCILinuxResource/should_be_able_to_patch_the_unified_map === RUN TestUpdateOCILinuxResource/should_be_able_to_update_each_resource === RUN TestUpdateOCILinuxResource/should_skip_empty_fields === RUN TestUpdateOCILinuxResource/should_be_able_to_fill_empty_fields --- PASS: TestUpdateOCILinuxResource (0.00s) --- PASS: TestUpdateOCILinuxResource/should_be_able_to_patch_the_unified_map (0.00s) --- PASS: TestUpdateOCILinuxResource/should_be_able_to_update_each_resource (0.00s) --- PASS: TestUpdateOCILinuxResource/should_skip_empty_fields (0.00s) --- PASS: TestUpdateOCILinuxResource/should_be_able_to_fill_empty_fields (0.00s) PASS ok github.com/containerd/containerd/pkg/cri/sbserver (cached) $ go test -v ./pkg/cri/server/ -run TestUpdateOCILinuxResource === RUN TestUpdateOCILinuxResource === RUN TestUpdateOCILinuxResource/should_be_able_to_update_each_resource === RUN TestUpdateOCILinuxResource/should_skip_empty_fields === RUN TestUpdateOCILinuxResource/should_be_able_to_fill_empty_fields === RUN TestUpdateOCILinuxResource/should_be_able_to_patch_the_unified_map --- PASS: TestUpdateOCILinuxResource (0.00s) --- PASS: TestUpdateOCILinuxResource/should_be_able_to_update_each_resource (0.00s) --- PASS: TestUpdateOCILinuxResource/should_skip_empty_fields (0.00s) --- PASS: TestUpdateOCILinuxResource/should_be_able_to_fill_empty_fields (0.00s) --- PASS: TestUpdateOCILinuxResource/should_be_able_to_patch_the_unified_map (0.00s) PASS ok github.com/containerd/containerd/pkg/cri/server (cached) ``` Signed-off-by: Qasim Sarfraz <[email protected]> (cherry picked from commit 9c8c450) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 5594f70 commit 91a68ed

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

pkg/cri/opts/spec_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ var (
409409
swapControllerAvailabilityOnce sync.Once
410410
)
411411

412-
func swapControllerAvailable() bool {
412+
// SwapControllerAvailable returns true if the swap controller is available
413+
func SwapControllerAvailable() bool {
413414
swapControllerAvailabilityOnce.Do(func() {
414415
const warn = "Failed to detect the availability of the swap controller, assuming not available"
415416
p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
@@ -479,7 +480,7 @@ func WithResources(resources *runtime.LinuxContainerResources, tolerateMissingHu
479480
if limit != 0 {
480481
s.Linux.Resources.Memory.Limit = &limit
481482
// swap/memory limit should be equal to prevent container from swapping by default
482-
if swapLimit == 0 && swapControllerAvailable() {
483+
if swapLimit == 0 && SwapControllerAvailable() {
483484
s.Linux.Resources.Memory.Swap = &limit
484485
}
485486
}

pkg/cri/server/container_update_resources_linux_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ import (
2626
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2727

2828
criconfig "github.com/containerd/containerd/pkg/cri/config"
29+
criopts "github.com/containerd/containerd/pkg/cri/opts"
2930
)
3031

3132
func TestUpdateOCILinuxResource(t *testing.T) {
3233
oomscoreadj := new(int)
3334
*oomscoreadj = -500
35+
expectedSwap := func(swap int64) *int64 {
36+
if criopts.SwapControllerAvailable() {
37+
return &swap
38+
}
39+
return nil
40+
}
3441
for desc, test := range map[string]struct {
3542
spec *runtimespec.Spec
3643
request *runtime.UpdateContainerResourcesRequest
@@ -72,7 +79,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
7279
Resources: &runtimespec.LinuxResources{
7380
Memory: &runtimespec.LinuxMemory{
7481
Limit: proto.Int64(54321),
75-
Swap: proto.Int64(54321),
82+
Swap: expectedSwap(54321),
7683
},
7784
CPU: &runtimespec.LinuxCPU{
7885
Shares: proto.Uint64(4444),
@@ -118,7 +125,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
118125
Resources: &runtimespec.LinuxResources{
119126
Memory: &runtimespec.LinuxMemory{
120127
Limit: proto.Int64(54321),
121-
Swap: proto.Int64(54321),
128+
Swap: expectedSwap(54321),
122129
},
123130
CPU: &runtimespec.LinuxCPU{
124131
Shares: proto.Uint64(4444),
@@ -159,7 +166,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
159166
Resources: &runtimespec.LinuxResources{
160167
Memory: &runtimespec.LinuxMemory{
161168
Limit: proto.Int64(54321),
162-
Swap: proto.Int64(54321),
169+
Swap: expectedSwap(54321),
163170
},
164171
CPU: &runtimespec.LinuxCPU{
165172
Shares: proto.Uint64(4444),
@@ -208,7 +215,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
208215
Resources: &runtimespec.LinuxResources{
209216
Memory: &runtimespec.LinuxMemory{
210217
Limit: proto.Int64(54321),
211-
Swap: proto.Int64(54321),
218+
Swap: expectedSwap(54321),
212219
},
213220
CPU: &runtimespec.LinuxCPU{
214221
Shares: proto.Uint64(4444),

0 commit comments

Comments
 (0)