Skip to content

Commit 18e0571

Browse files
Test that pod matches terms, has label selector
1 parent 7ced323 commit 18e0571

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

pkg/utils/predicates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func CheckPodsWithAntiAffinityExist(pod *v1.Pod, pods map[string][]*v1.Pod, node
332332
continue
333333
}
334334
if hasSameLabelValue(node, nodeHavingExistingPod, term.TopologyKey) {
335-
klog.V(1).InfoS("Found Pods violating PodAntiAffinity", "pod to evicted", klog.KObj(pod))
335+
klog.V(1).InfoS("Found Pods matching PodAntiAffinity", "pod with anti-affinity", klog.KObj(pod))
336336
return true
337337
}
338338
}

pkg/utils/predicates_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
v1 "k8s.io/api/core/v1"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"sigs.k8s.io/descheduler/test"
910
)
1011

1112
func TestUniqueSortTolerations(t *testing.T) {
@@ -1041,3 +1042,71 @@ func TestPodNodeAffinityWeight(t *testing.T) {
10411042
})
10421043
}
10431044
}
1045+
1046+
func TestCheckPodsWithAntiAffinityExist(t *testing.T) {
1047+
tests := []struct {
1048+
name string
1049+
pod *v1.Pod
1050+
podsInNamespace map[string][]*v1.Pod
1051+
nodeMap map[string]*v1.Node
1052+
affinity *v1.PodAntiAffinity
1053+
expMatch bool
1054+
}{
1055+
{
1056+
name: "found pod matching pod anti-affinity",
1057+
pod: test.PodWithPodAntiAffinity(test.BuildTestPod("p1", 1000, 1000, "node", nil), "foo", "bar"),
1058+
podsInNamespace: map[string][]*v1.Pod{
1059+
"default": {
1060+
test.PodWithPodAntiAffinity(test.BuildTestPod("p2", 1000, 1000, "node", nil), "foo", "bar"),
1061+
},
1062+
},
1063+
nodeMap: map[string]*v1.Node{
1064+
"node": test.BuildTestNode("node", 64000, 128*1000*1000*1000, 2, func(node *v1.Node) {
1065+
node.ObjectMeta.Labels = map[string]string{
1066+
"region": "main-region",
1067+
}
1068+
}),
1069+
},
1070+
expMatch: true,
1071+
},
1072+
{
1073+
name: "no match with invalid label selector",
1074+
pod: &v1.Pod{
1075+
Spec: v1.PodSpec{
1076+
Affinity: &v1.Affinity{
1077+
PodAntiAffinity: &v1.PodAntiAffinity{
1078+
RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
1079+
{
1080+
LabelSelector: &metav1.LabelSelector{
1081+
MatchLabels: map[string]string{
1082+
"wrong": "selector",
1083+
},
1084+
},
1085+
},
1086+
},
1087+
},
1088+
},
1089+
},
1090+
},
1091+
podsInNamespace: map[string][]*v1.Pod{},
1092+
nodeMap: map[string]*v1.Node{},
1093+
expMatch: false,
1094+
},
1095+
{
1096+
name: "no match if pod does not match terms namespace",
1097+
pod: test.PodWithPodAntiAffinity(test.BuildTestPod("p1", 1000, 1000, "node", func(pod *v1.Pod) {
1098+
pod.Namespace = "other"
1099+
}), "foo", "bar"),
1100+
podsInNamespace: map[string][]*v1.Pod{},
1101+
nodeMap: map[string]*v1.Node{},
1102+
expMatch: false,
1103+
},
1104+
}
1105+
for _, test := range tests {
1106+
t.Run(test.name, func(t *testing.T) {
1107+
if match := CheckPodsWithAntiAffinityExist(test.pod, test.podsInNamespace, test.nodeMap); match != test.expMatch {
1108+
t.Errorf("exp %v got %v", test.expMatch, match)
1109+
}
1110+
})
1111+
}
1112+
}

0 commit comments

Comments
 (0)