Skip to content

Commit 9c8c450

Browse files
committed
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]>
1 parent 54ec191 commit 9c8c450

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

pkg/cri/opts/spec_linux.go

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

414-
func swapControllerAvailable() bool {
414+
// SwapControllerAvailable returns true if the swap controller is available
415+
func SwapControllerAvailable() bool {
415416
swapControllerAvailabilityOnce.Do(func() {
416417
const warn = "Failed to detect the availability of the swap controller, assuming not available"
417418
p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
@@ -481,7 +482,7 @@ func WithResources(resources *runtime.LinuxContainerResources, tolerateMissingHu
481482
if limit != 0 {
482483
s.Linux.Resources.Memory.Limit = &limit
483484
// swap/memory limit should be equal to prevent container from swapping by default
484-
if swapLimit == 0 && swapControllerAvailable() {
485+
if swapLimit == 0 && SwapControllerAvailable() {
485486
s.Linux.Resources.Memory.Swap = &limit
486487
}
487488
}

pkg/cri/sbserver/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),

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)