Merge pull request #7946 from mqasimsarfraz/fix-swap-tests
cri: Fix TestUpdateOCILinuxResource for host w/o swap controller
This commit is contained in:
commit
61617211bf
@ -411,7 +411,8 @@ var (
|
|||||||
swapControllerAvailabilityOnce sync.Once
|
swapControllerAvailabilityOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
func swapControllerAvailable() bool {
|
// SwapControllerAvailable returns true if the swap controller is available
|
||||||
|
func SwapControllerAvailable() bool {
|
||||||
swapControllerAvailabilityOnce.Do(func() {
|
swapControllerAvailabilityOnce.Do(func() {
|
||||||
const warn = "Failed to detect the availability of the swap controller, assuming not available"
|
const warn = "Failed to detect the availability of the swap controller, assuming not available"
|
||||||
p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
|
p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
|
||||||
@ -481,7 +482,7 @@ func WithResources(resources *runtime.LinuxContainerResources, tolerateMissingHu
|
|||||||
if limit != 0 {
|
if limit != 0 {
|
||||||
s.Linux.Resources.Memory.Limit = &limit
|
s.Linux.Resources.Memory.Limit = &limit
|
||||||
// swap/memory limit should be equal to prevent container from swapping by default
|
// swap/memory limit should be equal to prevent container from swapping by default
|
||||||
if swapLimit == 0 && swapControllerAvailable() {
|
if swapLimit == 0 && SwapControllerAvailable() {
|
||||||
s.Linux.Resources.Memory.Swap = &limit
|
s.Linux.Resources.Memory.Swap = &limit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,18 @@ import (
|
|||||||
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
||||||
|
|
||||||
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
||||||
|
criopts "github.com/containerd/containerd/pkg/cri/opts"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpdateOCILinuxResource(t *testing.T) {
|
func TestUpdateOCILinuxResource(t *testing.T) {
|
||||||
oomscoreadj := new(int)
|
oomscoreadj := new(int)
|
||||||
*oomscoreadj = -500
|
*oomscoreadj = -500
|
||||||
|
expectedSwap := func(swap int64) *int64 {
|
||||||
|
if criopts.SwapControllerAvailable() {
|
||||||
|
return &swap
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for desc, test := range map[string]struct {
|
for desc, test := range map[string]struct {
|
||||||
spec *runtimespec.Spec
|
spec *runtimespec.Spec
|
||||||
request *runtime.UpdateContainerResourcesRequest
|
request *runtime.UpdateContainerResourcesRequest
|
||||||
@ -72,7 +79,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
@ -118,7 +125,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
@ -159,7 +166,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
@ -208,7 +215,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
|
@ -26,11 +26,18 @@ import (
|
|||||||
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
||||||
|
|
||||||
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
||||||
|
criopts "github.com/containerd/containerd/pkg/cri/opts"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpdateOCILinuxResource(t *testing.T) {
|
func TestUpdateOCILinuxResource(t *testing.T) {
|
||||||
oomscoreadj := new(int)
|
oomscoreadj := new(int)
|
||||||
*oomscoreadj = -500
|
*oomscoreadj = -500
|
||||||
|
expectedSwap := func(swap int64) *int64 {
|
||||||
|
if criopts.SwapControllerAvailable() {
|
||||||
|
return &swap
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for desc, test := range map[string]struct {
|
for desc, test := range map[string]struct {
|
||||||
spec *runtimespec.Spec
|
spec *runtimespec.Spec
|
||||||
request *runtime.UpdateContainerResourcesRequest
|
request *runtime.UpdateContainerResourcesRequest
|
||||||
@ -72,7 +79,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
@ -118,7 +125,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
@ -159,7 +166,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
@ -208,7 +215,7 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
Resources: &runtimespec.LinuxResources{
|
Resources: &runtimespec.LinuxResources{
|
||||||
Memory: &runtimespec.LinuxMemory{
|
Memory: &runtimespec.LinuxMemory{
|
||||||
Limit: proto.Int64(54321),
|
Limit: proto.Int64(54321),
|
||||||
Swap: proto.Int64(54321),
|
Swap: expectedSwap(54321),
|
||||||
},
|
},
|
||||||
CPU: &runtimespec.LinuxCPU{
|
CPU: &runtimespec.LinuxCPU{
|
||||||
Shares: proto.Uint64(4444),
|
Shares: proto.Uint64(4444),
|
||||||
|
Loading…
Reference in New Issue
Block a user