@@ -28,7 +28,6 @@ import (
28
28
"sigs.k8s.io/descheduler/pkg/utils"
29
29
30
30
v1 "k8s.io/api/core/v1"
31
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
31
"k8s.io/klog/v2"
33
32
)
34
33
@@ -86,8 +85,8 @@ func (d *RemovePodsViolatingInterPodAntiAffinity) Deschedule(ctx context.Context
86
85
}
87
86
88
87
podsInANamespace := podutil .GroupByNamespace (pods )
89
- podsOnANode := groupByNodeName (pods )
90
- nodeMap := createNodeMap (nodes )
88
+ podsOnANode := podutil . GroupByNodeName (pods )
89
+ nodeMap := utils . CreateNodeMap (nodes )
91
90
92
91
loop:
93
92
for _ , node := range nodes {
@@ -97,15 +96,17 @@ loop:
97
96
podutil .SortPodsBasedOnPriorityLowToHigh (pods )
98
97
totalPods := len (pods )
99
98
for i := 0 ; i < totalPods ; i ++ {
100
- if checkPodsWithAntiAffinityExist (pods [i ], podsInANamespace , nodeMap ) && d .handle .Evictor ().Filter (pods [i ]) && d .handle .Evictor ().PreEvictionFilter (pods [i ]) {
101
- if d .handle .Evictor ().Evict (ctx , pods [i ], evictions.EvictOptions {StrategyName : PluginName }) {
102
- // Since the current pod is evicted all other pods which have anti-affinity with this
103
- // pod need not be evicted.
104
- // Update allPods.
105
- podsInANamespace = removePodFromNamespaceMap (pods [i ], podsInANamespace )
106
- pods = append (pods [:i ], pods [i + 1 :]... )
107
- i --
108
- totalPods --
99
+ if utils .CheckPodsWithAntiAffinityExist (pods [i ], podsInANamespace , nodeMap ) {
100
+ if d .handle .Evictor ().Filter (pods [i ]) && d .handle .Evictor ().PreEvictionFilter (pods [i ]) {
101
+ if d .handle .Evictor ().Evict (ctx , pods [i ], evictions.EvictOptions {StrategyName : PluginName }) {
102
+ // Since the current pod is evicted all other pods which have anti-affinity with this
103
+ // pod need not be evicted.
104
+ // Update allPods.
105
+ podsInANamespace = removePodFromNamespaceMap (pods [i ], podsInANamespace )
106
+ pods = append (pods [:i ], pods [i + 1 :]... )
107
+ i --
108
+ totalPods --
109
+ }
109
110
}
110
111
}
111
112
if d .handle .Evictor ().NodeLimitExceeded (node ) {
@@ -130,87 +131,3 @@ func removePodFromNamespaceMap(podToRemove *v1.Pod, podMap map[string][]*v1.Pod)
130
131
}
131
132
return podMap
132
133
}
133
-
134
- func groupByNodeName (pods []* v1.Pod ) map [string ][]* v1.Pod {
135
- m := make (map [string ][]* v1.Pod )
136
- for i := 0 ; i < len (pods ); i ++ {
137
- pod := pods [i ]
138
- m [pod .Spec .NodeName ] = append (m [pod .Spec .NodeName ], pod )
139
- }
140
- return m
141
- }
142
-
143
- func createNodeMap (nodes []* v1.Node ) map [string ]* v1.Node {
144
- m := make (map [string ]* v1.Node , len (nodes ))
145
- for _ , node := range nodes {
146
- m [node .GetName ()] = node
147
- }
148
- return m
149
- }
150
-
151
- // checkPodsWithAntiAffinityExist checks if there are other pods on the node that the current pod cannot tolerate.
152
- func checkPodsWithAntiAffinityExist (pod * v1.Pod , pods map [string ][]* v1.Pod , nodeMap map [string ]* v1.Node ) bool {
153
- affinity := pod .Spec .Affinity
154
- if affinity != nil && affinity .PodAntiAffinity != nil {
155
- for _ , term := range getPodAntiAffinityTerms (affinity .PodAntiAffinity ) {
156
- namespaces := utils .GetNamespacesFromPodAffinityTerm (pod , & term )
157
- selector , err := metav1 .LabelSelectorAsSelector (term .LabelSelector )
158
- if err != nil {
159
- klog .ErrorS (err , "Unable to convert LabelSelector into Selector" )
160
- return false
161
- }
162
- for namespace := range namespaces {
163
- for _ , existingPod := range pods [namespace ] {
164
- if existingPod .Name != pod .Name && utils .PodMatchesTermsNamespaceAndSelector (existingPod , namespaces , selector ) {
165
- node , ok := nodeMap [pod .Spec .NodeName ]
166
- if ! ok {
167
- continue
168
- }
169
- nodeHavingExistingPod , ok := nodeMap [existingPod .Spec .NodeName ]
170
- if ! ok {
171
- continue
172
- }
173
- if hasSameLabelValue (node , nodeHavingExistingPod , term .TopologyKey ) {
174
- klog .V (1 ).InfoS ("Found Pods violating PodAntiAffinity" , "pod to evicted" , klog .KObj (pod ))
175
- return true
176
- }
177
- }
178
- }
179
- }
180
- }
181
- }
182
- return false
183
- }
184
-
185
- func hasSameLabelValue (node1 , node2 * v1.Node , key string ) bool {
186
- if node1 .Name == node2 .Name {
187
- return true
188
- }
189
- node1Labels := node1 .Labels
190
- if node1Labels == nil {
191
- return false
192
- }
193
- node2Labels := node2 .Labels
194
- if node2Labels == nil {
195
- return false
196
- }
197
- value1 , ok := node1Labels [key ]
198
- if ! ok {
199
- return false
200
- }
201
- value2 , ok := node2Labels [key ]
202
- if ! ok {
203
- return false
204
- }
205
- return value1 == value2
206
- }
207
-
208
- // getPodAntiAffinityTerms gets the antiaffinity terms for the given pod.
209
- func getPodAntiAffinityTerms (podAntiAffinity * v1.PodAntiAffinity ) (terms []v1.PodAffinityTerm ) {
210
- if podAntiAffinity != nil {
211
- if len (podAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution ) != 0 {
212
- terms = podAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution
213
- }
214
- }
215
- return terms
216
- }
0 commit comments