From 102bd6a91d208a3324bc10aa786874d1d0ecef68 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 14:27:52 +0100 Subject: [PATCH 01/12] refactor(TestRemovePodsViolatingNodeAffinity): deduplicate node creation with buildTestNode helper --- .../node_affinity_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index fe6029fd5..b94b41586 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -32,15 +32,19 @@ import ( "sigs.k8s.io/descheduler/test" ) +func buildTestNode(name string, apply func(*v1.Node)) *v1.Node { + return test.BuildTestNode(name, 2000, 3000, 10, apply) +} + func TestRemovePodsViolatingNodeAffinity(t *testing.T) { nodeLabelKey := "kubernetes.io/desiredNode" nodeLabelValue := "yes" - nodeWithLabels := test.BuildTestNode("nodeWithLabels", 2000, 3000, 10, nil) + nodeWithLabels := buildTestNode("nodeWithLabels", nil) nodeWithLabels.Labels[nodeLabelKey] = nodeLabelValue - nodeWithoutLabels := test.BuildTestNode("nodeWithoutLabels", 2000, 3000, 10, nil) + nodeWithoutLabels := buildTestNode("nodeWithoutLabels", nil) - unschedulableNodeWithLabels := test.BuildTestNode("unschedulableNodeWithLabels", 2000, 3000, 10, nil) + unschedulableNodeWithLabels := buildTestNode("unschedulableNodeWithLabels", nil) unschedulableNodeWithLabels.Labels[nodeLabelKey] = nodeLabelValue unschedulableNodeWithLabels.Spec.Unschedulable = true From 8fe74c7a0ce36af5004fe5678b3fd4aef354981e Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 14:29:09 +0100 Subject: [PATCH 02/12] refactor(TestRemovePodsViolatingNodeAffinity): apply unit test convention for nodeWithLabels --- .../removepodsviolatingnodeaffinity/node_affinity_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index b94b41586..ae5d1330f 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -39,8 +39,9 @@ func buildTestNode(name string, apply func(*v1.Node)) *v1.Node { func TestRemovePodsViolatingNodeAffinity(t *testing.T) { nodeLabelKey := "kubernetes.io/desiredNode" nodeLabelValue := "yes" - nodeWithLabels := buildTestNode("nodeWithLabels", nil) - nodeWithLabels.Labels[nodeLabelKey] = nodeLabelValue + nodeWithLabels := buildTestNode("nodeWithLabels", func(node *v1.Node) { + node.Labels[nodeLabelKey] = nodeLabelValue + }) nodeWithoutLabels := buildTestNode("nodeWithoutLabels", nil) From 691a1da43ba6c495a9ade64538a7f7a4a2faa500 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 14:30:27 +0100 Subject: [PATCH 03/12] refactor(TestRemovePodsViolatingNodeAffinity): apply unit test convention for unschedulableNodeWithLabels --- .../removepodsviolatingnodeaffinity/node_affinity_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index ae5d1330f..2da00cee2 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -45,9 +45,10 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { nodeWithoutLabels := buildTestNode("nodeWithoutLabels", nil) - unschedulableNodeWithLabels := buildTestNode("unschedulableNodeWithLabels", nil) - unschedulableNodeWithLabels.Labels[nodeLabelKey] = nodeLabelValue - unschedulableNodeWithLabels.Spec.Unschedulable = true + unschedulableNodeWithLabels := buildTestNode("unschedulableNodeWithLabels", func(node *v1.Node) { + node.Labels[nodeLabelKey] = nodeLabelValue + node.Spec.Unschedulable = true + }) addPodsToNode := func(node *v1.Node, deletionTimestamp *metav1.Time, affinityType string) []*v1.Pod { podWithNodeAffinity := test.BuildTestPod("podWithNodeAffinity", 100, 0, node.Name, nil) From 2cda1bd89d579b5e348d5663921f3bceb43e1e81 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 14:32:41 +0100 Subject: [PATCH 04/12] refactor(TestRemovePodsViolatingNodeAffinity): deduplicate pod creation with buildTestPod helper --- .../node_affinity_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index 2da00cee2..a420c9073 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -36,6 +36,10 @@ func buildTestNode(name string, apply func(*v1.Node)) *v1.Node { return test.BuildTestNode(name, 2000, 3000, 10, apply) } +func buildTestPod(name string, nodeName string, apply func(*v1.Pod)) *v1.Pod { + return test.BuildTestPod(name, 100, 0, nodeName, apply) +} + func TestRemovePodsViolatingNodeAffinity(t *testing.T) { nodeLabelKey := "kubernetes.io/desiredNode" nodeLabelValue := "yes" @@ -51,7 +55,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }) addPodsToNode := func(node *v1.Node, deletionTimestamp *metav1.Time, affinityType string) []*v1.Pod { - podWithNodeAffinity := test.BuildTestPod("podWithNodeAffinity", 100, 0, node.Name, nil) + podWithNodeAffinity := buildTestPod("podWithNodeAffinity", node.Name, nil) podWithNodeAffinity.Spec.Affinity = &v1.Affinity{ NodeAffinity: &v1.NodeAffinity{}, } @@ -95,8 +99,8 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { t.Fatalf("Invalid affinity type %s", affinityType) } - pod1 := test.BuildTestPod("pod1", 100, 0, node.Name, nil) - pod2 := test.BuildTestPod("pod2", 100, 0, node.Name, nil) + pod1 := buildTestPod("pod1", node.Name, nil) + pod2 := buildTestPod("pod2", node.Name, nil) podWithNodeAffinity.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() pod1.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() From a2ffbc12611d399a93efcc8dc0ec6133112b9ab8 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 14:34:05 +0100 Subject: [PATCH 05/12] refactor(TestRemovePodsViolatingNodeAffinity): apply unit test convention for podWithNodeAffinity --- .../node_affinity_test.go | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index a420c9073..44add06ff 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -55,58 +55,60 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }) addPodsToNode := func(node *v1.Node, deletionTimestamp *metav1.Time, affinityType string) []*v1.Pod { - podWithNodeAffinity := buildTestPod("podWithNodeAffinity", node.Name, nil) - podWithNodeAffinity.Spec.Affinity = &v1.Affinity{ - NodeAffinity: &v1.NodeAffinity{}, - } + podWithNodeAffinity := buildTestPod("podWithNodeAffinity", node.Name, func(pod *v1.Pod) { + pod.Spec.Affinity = &v1.Affinity{ + NodeAffinity: &v1.NodeAffinity{}, + } - switch affinityType { - case "requiredDuringSchedulingIgnoredDuringExecution": - podWithNodeAffinity.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &v1.NodeSelector{ - NodeSelectorTerms: []v1.NodeSelectorTerm{ + switch affinityType { + case "requiredDuringSchedulingIgnoredDuringExecution": + pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &v1.NodeSelector{ + NodeSelectorTerms: []v1.NodeSelectorTerm{ + { + MatchExpressions: []v1.NodeSelectorRequirement{ + { + Key: nodeLabelKey, + Operator: "In", + Values: []string{ + nodeLabelValue, + }, + }, + }, + }, + }, + } + case "preferredDuringSchedulingIgnoredDuringExecution": + pod.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution = []v1.PreferredSchedulingTerm{ { - MatchExpressions: []v1.NodeSelectorRequirement{ - { - Key: nodeLabelKey, - Operator: "In", - Values: []string{ - nodeLabelValue, + Weight: 10, + Preference: v1.NodeSelectorTerm{ + MatchExpressions: []v1.NodeSelectorRequirement{ + { + Key: nodeLabelKey, + Operator: "In", + Values: []string{ + nodeLabelValue, + }, }, }, }, }, - }, + } + case "requiredDuringSchedulingRequiredDuringExecution": + default: + t.Fatalf("Invalid affinity type %s", affinityType) } - case "preferredDuringSchedulingIgnoredDuringExecution": - podWithNodeAffinity.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution = []v1.PreferredSchedulingTerm{ - { - Weight: 10, - Preference: v1.NodeSelectorTerm{ - MatchExpressions: []v1.NodeSelectorRequirement{ - { - Key: nodeLabelKey, - Operator: "In", - Values: []string{ - nodeLabelValue, - }, - }, - }, - }, - }, - } - case "requiredDuringSchedulingRequiredDuringExecution": - default: - t.Fatalf("Invalid affinity type %s", affinityType) - } + + pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + pod.DeletionTimestamp = deletionTimestamp + }) pod1 := buildTestPod("pod1", node.Name, nil) pod2 := buildTestPod("pod2", node.Name, nil) - podWithNodeAffinity.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() pod1.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() pod2.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - podWithNodeAffinity.DeletionTimestamp = deletionTimestamp pod1.DeletionTimestamp = deletionTimestamp pod2.DeletionTimestamp = deletionTimestamp From 183a138d82bab6603cedc129f869ecb19db5546e Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 15:28:03 +0100 Subject: [PATCH 06/12] refactor(TestRemovePodsViolatingNodeAffinity): add constants for node names --- .../node_affinity_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index 44add06ff..f37dd987d 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -32,6 +32,12 @@ import ( "sigs.k8s.io/descheduler/test" ) +const ( + nodeWithLabelsName = "nodeWithLabels" + nodeWithoutLabelsName = "nodeWithoutLabels" + unschedulableNodeWithLabelsName = "unschedulableNodeWithLabels" +) + func buildTestNode(name string, apply func(*v1.Node)) *v1.Node { return test.BuildTestNode(name, 2000, 3000, 10, apply) } @@ -43,13 +49,13 @@ func buildTestPod(name string, nodeName string, apply func(*v1.Pod)) *v1.Pod { func TestRemovePodsViolatingNodeAffinity(t *testing.T) { nodeLabelKey := "kubernetes.io/desiredNode" nodeLabelValue := "yes" - nodeWithLabels := buildTestNode("nodeWithLabels", func(node *v1.Node) { + nodeWithLabels := buildTestNode(nodeWithLabelsName, func(node *v1.Node) { node.Labels[nodeLabelKey] = nodeLabelValue }) - nodeWithoutLabels := buildTestNode("nodeWithoutLabels", nil) + nodeWithoutLabels := buildTestNode(nodeWithoutLabelsName, nil) - unschedulableNodeWithLabels := buildTestNode("unschedulableNodeWithLabels", func(node *v1.Node) { + unschedulableNodeWithLabels := buildTestNode(unschedulableNodeWithLabelsName, func(node *v1.Node) { node.Labels[nodeLabelKey] = nodeLabelValue node.Spec.Unschedulable = true }) From 42d255fd957e768dd430c681534677513b3e4ad6 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 15:31:18 +0100 Subject: [PATCH 07/12] refactor(TestRemovePodsViolatingNodeAffinity): update addPodsToNode to accept nodeName --- .../node_affinity_test.go | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index f37dd987d..1fde52470 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -60,8 +60,8 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { node.Spec.Unschedulable = true }) - addPodsToNode := func(node *v1.Node, deletionTimestamp *metav1.Time, affinityType string) []*v1.Pod { - podWithNodeAffinity := buildTestPod("podWithNodeAffinity", node.Name, func(pod *v1.Pod) { + addPodsToNode := func(nodeName string, deletionTimestamp *metav1.Time, affinityType string) []*v1.Pod { + podWithNodeAffinity := buildTestPod("podWithNodeAffinity", nodeName, func(pod *v1.Pod) { pod.Spec.Affinity = &v1.Affinity{ NodeAffinity: &v1.NodeAffinity{}, } @@ -109,8 +109,8 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { pod.DeletionTimestamp = deletionTimestamp }) - pod1 := buildTestPod("pod1", node.Name, nil) - pod2 := buildTestPod("pod2", node.Name, nil) + pod1 := buildTestPod("pod1", nodeName, nil) + pod2 := buildTestPod("pod2", nodeName, nil) pod1.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() pod2.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() @@ -144,7 +144,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { NodeAffinityType: []string{"requiredDuringSchedulingRequiredDuringExecution"}, }, expectedEvictedPodCount: 0, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingRequiredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingRequiredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, }, { @@ -153,7 +153,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, expectedEvictedPodCount: 0, - pods: addPodsToNode(nodeWithLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithLabels}, }, { @@ -162,7 +162,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, expectedEvictedPodCount: 0, - pods: addPodsToNode(nodeWithLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithLabels}, }, { @@ -171,7 +171,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, }, { @@ -180,7 +180,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, }, { @@ -189,7 +189,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxPodsToEvictPerNode: &uint1, }, @@ -199,7 +199,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxPodsToEvictPerNode: &uint1, }, @@ -209,7 +209,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxPodsToEvictPerNode: &uint0, }, @@ -219,7 +219,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxPodsToEvictPerNode: &uint0, }, @@ -229,7 +229,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxPodsToEvictPerNode: &uint1, }, @@ -239,7 +239,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxPodsToEvictPerNode: &uint1, }, @@ -249,7 +249,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxNoOfPodsToEvictPerNamespace: &uint1, }, @@ -259,7 +259,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxNoOfPodsToEvictPerNamespace: &uint1, maxNoOfPodsToEvictTotal: &uint0, @@ -270,7 +270,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxNoOfPodsToEvictPerNamespace: &uint1, }, @@ -280,7 +280,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxNoOfPodsToEvictPerNamespace: &uint0, }, @@ -290,7 +290,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxNoOfPodsToEvictPerNamespace: &uint0, }, @@ -300,7 +300,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxNoOfPodsToEvictPerNamespace: &uint1, }, @@ -310,7 +310,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, maxNoOfPodsToEvictPerNamespace: &uint1, }, @@ -320,7 +320,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, unschedulableNodeWithLabels}, nodefit: true, }, @@ -330,7 +330,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithoutLabels, unschedulableNodeWithLabels}, nodefit: true, }, @@ -340,7 +340,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithLabels, unschedulableNodeWithLabels}, maxPodsToEvictPerNode: &uint1, nodefit: true, @@ -351,7 +351,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabels, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{nodeWithLabels, unschedulableNodeWithLabels}, maxPodsToEvictPerNode: &uint1, nodefit: true, From 9f7629136f459c4640b983bd659b17a3278e8531 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 15:37:04 +0100 Subject: [PATCH 08/12] refactor(TestRemovePodsViolatingNodeAffinity): inline nodeWithLabels --- .../node_affinity_test.go | 146 ++++++++++++------ 1 file changed, 102 insertions(+), 44 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index 1fde52470..d74704c1d 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -36,27 +36,27 @@ const ( nodeWithLabelsName = "nodeWithLabels" nodeWithoutLabelsName = "nodeWithoutLabels" unschedulableNodeWithLabelsName = "unschedulableNodeWithLabels" + nodeLabelKey = "kubernetes.io/desiredNode" + nodeLabelValue = "yes" ) func buildTestNode(name string, apply func(*v1.Node)) *v1.Node { return test.BuildTestNode(name, 2000, 3000, 10, apply) } +func setNodeDesiredNodeLabel(node *v1.Node) { + node.Labels[nodeLabelKey] = nodeLabelValue +} + func buildTestPod(name string, nodeName string, apply func(*v1.Pod)) *v1.Pod { return test.BuildTestPod(name, 100, 0, nodeName, apply) } func TestRemovePodsViolatingNodeAffinity(t *testing.T) { - nodeLabelKey := "kubernetes.io/desiredNode" - nodeLabelValue := "yes" - nodeWithLabels := buildTestNode(nodeWithLabelsName, func(node *v1.Node) { - node.Labels[nodeLabelKey] = nodeLabelValue - }) - nodeWithoutLabels := buildTestNode(nodeWithoutLabelsName, nil) unschedulableNodeWithLabels := buildTestNode(unschedulableNodeWithLabelsName, func(node *v1.Node) { - node.Labels[nodeLabelKey] = nodeLabelValue + setNodeDesiredNodeLabel(node) node.Spec.Unschedulable = true }) @@ -145,7 +145,10 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, expectedEvictedPodCount: 0, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingRequiredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, }, { description: "Pod is correctly scheduled on node, no eviction expected [required affinity]", @@ -154,7 +157,9 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, expectedEvictedPodCount: 0, pods: addPodsToNode(nodeWithLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithLabels}, + nodes: []*v1.Node{ + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, }, { description: "Pod is correctly scheduled on node, no eviction expected [preferred affinity]", @@ -163,7 +168,9 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, expectedEvictedPodCount: 0, pods: addPodsToNode(nodeWithLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithLabels}, + nodes: []*v1.Node{ + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, }, { description: "Pod is scheduled on node without matching labels, another schedulable node available, should be evicted", @@ -171,8 +178,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, }, { description: "Pod is scheduled on node without matching labels, another schedulable node available with better fit, should be evicted", @@ -180,8 +190,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, }, { description: "Pod is scheduled on node without matching labels, another schedulable node available, maxPodsToEvictPerNode set to 1, should be evicted [required affinity]", @@ -189,8 +202,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxPodsToEvictPerNode: &uint1, }, { @@ -199,8 +215,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxPodsToEvictPerNode: &uint1, }, { @@ -209,8 +228,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxPodsToEvictPerNode: &uint0, }, { @@ -219,8 +241,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxPodsToEvictPerNode: &uint0, }, { @@ -229,8 +254,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxPodsToEvictPerNode: &uint1, }, { @@ -239,8 +267,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxPodsToEvictPerNode: &uint1, }, { @@ -249,8 +280,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxNoOfPodsToEvictPerNamespace: &uint1, }, { @@ -259,8 +293,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxNoOfPodsToEvictPerNamespace: &uint1, maxNoOfPodsToEvictTotal: &uint0, }, @@ -270,8 +307,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxNoOfPodsToEvictPerNamespace: &uint1, }, { @@ -280,8 +320,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxNoOfPodsToEvictPerNamespace: &uint0, }, { @@ -290,8 +333,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxNoOfPodsToEvictPerNamespace: &uint0, }, { @@ -300,8 +346,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxNoOfPodsToEvictPerNamespace: &uint1, }, { @@ -310,8 +359,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, nodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + nodeWithoutLabels, + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + }, maxNoOfPodsToEvictPerNamespace: &uint1, }, { @@ -340,8 +392,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithLabels, unschedulableNodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + unschedulableNodeWithLabels, + }, maxPodsToEvictPerNode: &uint1, nodefit: true, }, @@ -351,8 +406,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithLabels, unschedulableNodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), + unschedulableNodeWithLabels, + }, maxPodsToEvictPerNode: &uint1, nodefit: true, }, From 7b9d5d2539e804f859277d78c22a78039dc5be30 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 15:50:25 +0100 Subject: [PATCH 09/12] refactor(TestRemovePodsViolatingNodeAffinity): inline nodeWithoutLabels --- .../node_affinity_test.go | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index d74704c1d..10ea1a3ec 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -53,8 +53,6 @@ func buildTestPod(name string, nodeName string, apply func(*v1.Pod)) *v1.Pod { } func TestRemovePodsViolatingNodeAffinity(t *testing.T) { - nodeWithoutLabels := buildTestNode(nodeWithoutLabelsName, nil) - unschedulableNodeWithLabels := buildTestNode(unschedulableNodeWithLabelsName, func(node *v1.Node) { setNodeDesiredNodeLabel(node) node.Spec.Unschedulable = true @@ -146,7 +144,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { expectedEvictedPodCount: 0, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingRequiredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, }, @@ -180,7 +178,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, }, @@ -192,7 +190,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, }, @@ -204,7 +202,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxPodsToEvictPerNode: &uint1, @@ -217,7 +215,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxPodsToEvictPerNode: &uint1, @@ -230,7 +228,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxPodsToEvictPerNode: &uint0, @@ -243,7 +241,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxPodsToEvictPerNode: &uint0, @@ -256,7 +254,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxPodsToEvictPerNode: &uint1, @@ -269,7 +267,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxPodsToEvictPerNode: &uint1, @@ -282,7 +280,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxNoOfPodsToEvictPerNamespace: &uint1, @@ -295,7 +293,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxNoOfPodsToEvictPerNamespace: &uint1, @@ -309,7 +307,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxNoOfPodsToEvictPerNamespace: &uint1, @@ -322,7 +320,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxNoOfPodsToEvictPerNamespace: &uint0, @@ -335,7 +333,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxNoOfPodsToEvictPerNamespace: &uint0, @@ -348,7 +346,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxNoOfPodsToEvictPerNamespace: &uint1, @@ -361,7 +359,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { }, pods: addPodsToNode(nodeWithoutLabelsName, &metav1.Time{}, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ - nodeWithoutLabels, + buildTestNode(nodeWithoutLabelsName, nil), buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), }, maxNoOfPodsToEvictPerNamespace: &uint1, @@ -372,8 +370,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, unschedulableNodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + buildTestNode(nodeWithoutLabelsName, nil), + unschedulableNodeWithLabels, + }, nodefit: true, }, { @@ -382,8 +383,11 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { args: RemovePodsViolatingNodeAffinityArgs{ NodeAffinityType: []string{"preferredDuringSchedulingIgnoredDuringExecution"}, }, - pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), - nodes: []*v1.Node{nodeWithoutLabels, unschedulableNodeWithLabels}, + pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), + nodes: []*v1.Node{ + buildTestNode(nodeWithoutLabelsName, nil), + unschedulableNodeWithLabels, + }, nodefit: true, }, { From aec4416099d83b49ec5d76d991ba1350b280ba17 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 15:59:01 +0100 Subject: [PATCH 10/12] refactor(TestRemovePodsViolatingNodeAffinity): add buildUnschedulableNodeWithLabels function --- .../node_affinity_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index 10ea1a3ec..51e352738 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -52,12 +52,14 @@ func buildTestPod(name string, nodeName string, apply func(*v1.Pod)) *v1.Pod { return test.BuildTestPod(name, 100, 0, nodeName, apply) } -func TestRemovePodsViolatingNodeAffinity(t *testing.T) { - unschedulableNodeWithLabels := buildTestNode(unschedulableNodeWithLabelsName, func(node *v1.Node) { +func buildUnschedulableNodeWithLabels() *v1.Node { + return buildTestNode(unschedulableNodeWithLabelsName, func(node *v1.Node) { setNodeDesiredNodeLabel(node) node.Spec.Unschedulable = true }) +} +func TestRemovePodsViolatingNodeAffinity(t *testing.T) { addPodsToNode := func(nodeName string, deletionTimestamp *metav1.Time, affinityType string) []*v1.Pod { podWithNodeAffinity := buildTestPod("podWithNodeAffinity", nodeName, func(pod *v1.Pod) { pod.Spec.Affinity = &v1.Affinity{ @@ -373,7 +375,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ buildTestNode(nodeWithoutLabelsName, nil), - unschedulableNodeWithLabels, + buildUnschedulableNodeWithLabels(), }, nodefit: true, }, @@ -386,7 +388,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ buildTestNode(nodeWithoutLabelsName, nil), - unschedulableNodeWithLabels, + buildUnschedulableNodeWithLabels(), }, nodefit: true, }, @@ -399,7 +401,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { pods: addPodsToNode(nodeWithoutLabelsName, nil, "requiredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), - unschedulableNodeWithLabels, + buildUnschedulableNodeWithLabels(), }, maxPodsToEvictPerNode: &uint1, nodefit: true, @@ -413,7 +415,7 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { pods: addPodsToNode(nodeWithoutLabelsName, nil, "preferredDuringSchedulingIgnoredDuringExecution"), nodes: []*v1.Node{ buildTestNode(nodeWithLabelsName, setNodeDesiredNodeLabel), - unschedulableNodeWithLabels, + buildUnschedulableNodeWithLabels(), }, maxPodsToEvictPerNode: &uint1, nodefit: true, From cad120881f733aa1fe6c457cfe49d8fb87832da6 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 16:02:37 +0100 Subject: [PATCH 11/12] refactor(TestRemovePodsViolatingNodeAffinity): apply pod single creation convention --- .../node_affinity_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index 51e352738..62de8c892 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -109,14 +109,14 @@ func TestRemovePodsViolatingNodeAffinity(t *testing.T) { pod.DeletionTimestamp = deletionTimestamp }) - pod1 := buildTestPod("pod1", nodeName, nil) - pod2 := buildTestPod("pod2", nodeName, nil) - - pod1.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod2.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - - pod1.DeletionTimestamp = deletionTimestamp - pod2.DeletionTimestamp = deletionTimestamp + pod1 := buildTestPod("pod1", nodeName, func(pod *v1.Pod) { + pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + pod.DeletionTimestamp = deletionTimestamp + }) + pod2 := buildTestPod("pod2", nodeName, func(pod *v1.Pod) { + pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + pod.DeletionTimestamp = deletionTimestamp + }) return []*v1.Pod{ podWithNodeAffinity, From 07616c3fc0bab8cc9e86856b5cd20782d55f6db6 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 15 Dec 2025 16:14:50 +0100 Subject: [PATCH 12/12] refactor(TestRemovePodsHavingTooManyRestarts): make fmt --- .../removepodsviolatingnodeaffinity/node_affinity_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go index 62de8c892..ffc0f6db7 100644 --- a/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodeaffinity/node_affinity_test.go @@ -48,7 +48,7 @@ func setNodeDesiredNodeLabel(node *v1.Node) { node.Labels[nodeLabelKey] = nodeLabelValue } -func buildTestPod(name string, nodeName string, apply func(*v1.Pod)) *v1.Pod { +func buildTestPod(name, nodeName string, apply func(*v1.Pod)) *v1.Pod { return test.BuildTestPod(name, 100, 0, nodeName, apply) }