From 6c1f14c4682842a066c9eeb8a5338f5efbd30a7c Mon Sep 17 00:00:00 2001 From: Itamar Holder Date: Sun, 2 Jun 2024 12:18:39 +0300 Subject: [PATCH] unit tests: exclude critical pods from swapping Signed-off-by: Itamar Holder --- .../kuberuntime_container_linux_test.go | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/kubelet/kuberuntime/kuberuntime_container_linux_test.go b/pkg/kubelet/kuberuntime/kuberuntime_container_linux_test.go index 0530c42435b..103c4a561d9 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_container_linux_test.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_container_linux_test.go @@ -40,8 +40,10 @@ import ( utilfeature "k8s.io/apiserver/pkg/util/feature" featuregatetesting "k8s.io/component-base/featuregate/testing" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" + "k8s.io/kubernetes/pkg/apis/scheduling" "k8s.io/kubernetes/pkg/features" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" + "k8s.io/utils/ptr" ) func makeExpectedConfig(m *kubeGenericRuntimeManager, pod *v1.Pod, containerIndex int, enforceMemoryQoS bool) *runtimeapi.ContainerConfig { @@ -1031,6 +1033,7 @@ func TestGenerateLinuxContainerResourcesWithSwap(t *testing.T) { swapBehavior string addContainerWithoutRequests bool addGuaranteedContainer bool + isCriticalPod bool }{ // With cgroup v1 { @@ -1208,6 +1211,16 @@ func TestGenerateLinuxContainerResourcesWithSwap(t *testing.T) { addContainerWithoutRequests: true, addGuaranteedContainer: false, }, + + // When the pod is considered critical, disallow swap access + { + name: "Best-effort QoS, cgroups v2, LimitedSwap, critical pod", + cgroupVersion: cgroupV2, + qosClass: v1.PodQOSBurstable, + nodeSwapFeatureGateEnabled: true, + swapBehavior: types.LimitedSwap, + isCriticalPod: true, + }, } { t.Run(tc.name, func(t *testing.T) { setCgroupVersionDuringTest(tc.cgroupVersion) @@ -1244,6 +1257,11 @@ func TestGenerateLinuxContainerResourcesWithSwap(t *testing.T) { pod.Spec.Containers[0].Resources = resourceReqsC1 pod.Spec.Containers[1].Resources = resourceReqsC2 + if tc.isCriticalPod { + pod.Spec.Priority = ptr.To(scheduling.SystemCriticalPriority) + assert.True(t, types.IsCriticalPod(pod), "pod is expected to be critical") + } + resourcesC1 := m.generateLinuxContainerResources(pod, &pod.Spec.Containers[0], false) resourcesC2 := m.generateLinuxContainerResources(pod, &pod.Spec.Containers[1], false) @@ -1252,7 +1270,7 @@ func TestGenerateLinuxContainerResourcesWithSwap(t *testing.T) { return } - if !tc.nodeSwapFeatureGateEnabled || tc.cgroupVersion == cgroupV1 || (tc.swapBehavior == types.LimitedSwap && tc.qosClass != v1.PodQOSBurstable) { + if tc.isCriticalPod || !tc.nodeSwapFeatureGateEnabled || tc.cgroupVersion == cgroupV1 || (tc.swapBehavior == types.LimitedSwap && tc.qosClass != v1.PodQOSBurstable) { expectNoSwap(tc.cgroupVersion, resourcesC1, resourcesC2) return }