|
6 | 6 |
|
7 | 7 | v1 "k8s.io/api/core/v1"
|
8 | 8 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
| 9 | + "sigs.k8s.io/descheduler/test" |
9 | 10 | )
|
10 | 11 |
|
11 | 12 | func TestUniqueSortTolerations(t *testing.T) {
|
@@ -1041,3 +1042,71 @@ func TestPodNodeAffinityWeight(t *testing.T) {
|
1041 | 1042 | })
|
1042 | 1043 | }
|
1043 | 1044 | }
|
| 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