Skip to content

Commit 5b53915

Browse files
Test that pod matches terms, has label selector
1 parent fabcbc6 commit 5b53915

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

pkg/descheduler/pod/pods_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,11 @@ func TestGroupByNodeName(t *testing.T) {
229229
},
230230
}
231231
for _, test := range tests {
232-
resultMap := GroupByNodeName(test.pods)
233-
if !reflect.DeepEqual(resultMap, test.expMap) {
234-
t.Errorf("Expected %v node map, got %v", test.expMap, resultMap)
235-
}
232+
t.Run(test.name, func(t *testing.T) {
233+
resultMap := GroupByNodeName(test.pods)
234+
if !reflect.DeepEqual(resultMap, test.expMap) {
235+
t.Errorf("Expected %v node map, got %v", test.expMap, resultMap)
236+
}
237+
})
236238
}
237239
}

pkg/utils/predicates.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,15 @@ func CheckPodsWithAntiAffinityExist(pod *v1.Pod, pods map[string][]*v1.Pod, node
314314
affinity := pod.Spec.Affinity
315315
if affinity != nil && affinity.PodAntiAffinity != nil {
316316
for _, term := range getPodAntiAffinityTerms(affinity.PodAntiAffinity) {
317-
namespaces := GetNamespacesFromPodAffinityTerm(pod, &term)
317+
namespaces := getNamespacesFromPodAffinityTerm(pod, &term)
318318
selector, err := metav1.LabelSelectorAsSelector(term.LabelSelector)
319319
if err != nil {
320320
klog.ErrorS(err, "Unable to convert LabelSelector into Selector")
321321
return false
322322
}
323323
for namespace := range namespaces {
324324
for _, existingPod := range pods[namespace] {
325-
if existingPod.Name != pod.Name && PodMatchesTermsNamespaceAndSelector(existingPod, namespaces, selector) {
325+
if existingPod.Name != pod.Name && podMatchesTermsNamespaceAndSelector(existingPod, namespaces, selector) {
326326
node, ok := nodeMap[pod.Spec.NodeName]
327327
if !ok {
328328
continue
@@ -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+
}

pkg/utils/priority.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const SystemCriticalPriority = 2 * int32(1000000000)
1717
// GetNamespacesFromPodAffinityTerm returns a set of names
1818
// according to the namespaces indicated in podAffinityTerm.
1919
// If namespaces is empty it considers the given pod's namespace.
20-
func GetNamespacesFromPodAffinityTerm(pod *v1.Pod, podAffinityTerm *v1.PodAffinityTerm) sets.Set[string] {
20+
func getNamespacesFromPodAffinityTerm(pod *v1.Pod, podAffinityTerm *v1.PodAffinityTerm) sets.Set[string] {
2121
names := sets.New[string]()
2222
if len(podAffinityTerm.Namespaces) == 0 {
2323
names.Insert(pod.Namespace)
@@ -27,9 +27,9 @@ func GetNamespacesFromPodAffinityTerm(pod *v1.Pod, podAffinityTerm *v1.PodAffini
2727
return names
2828
}
2929

30-
// PodMatchesTermsNamespaceAndSelector returns true if the given <pod>
30+
// podMatchesTermsNamespaceAndSelector returns true if the given <pod>
3131
// matches the namespace and selector defined by <affinityPod>`s <term>.
32-
func PodMatchesTermsNamespaceAndSelector(pod *v1.Pod, namespaces sets.Set[string], selector labels.Selector) bool {
32+
func podMatchesTermsNamespaceAndSelector(pod *v1.Pod, namespaces sets.Set[string], selector labels.Selector) bool {
3333
if !namespaces.Has(pod.Namespace) {
3434
return false
3535
}

0 commit comments

Comments
 (0)