@@ -122,9 +122,13 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
122
122
// the node.
123
123
pm := newPodAffinityPriorityMap (nodes )
124
124
allNodeNames := make ([]string , 0 , len (nodeNameToInfo ))
125
+ lazyInit := hasAffinityConstraints || hasAntiAffinityConstraints
125
126
for name := range nodeNameToInfo {
126
127
allNodeNames = append (allNodeNames , name )
127
- pm .counts [name ] = new (int64 )
128
+ // if pod has affinity defined, or target node has affinityPods
129
+ if lazyInit || len (nodeNameToInfo [name ].PodsWithAffinity ()) != 0 {
130
+ pm .counts [name ] = new (int64 )
131
+ }
128
132
}
129
133
130
134
// convert the topology key based weights to the node name based weights
@@ -191,7 +195,7 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
191
195
nodeInfo := nodeNameToInfo [allNodeNames [i ]]
192
196
if nodeInfo .Node () != nil {
193
197
if hasAffinityConstraints || hasAntiAffinityConstraints {
194
- // We need to process all the nodes .
198
+ // We need to process all the pods .
195
199
for _ , existingPod := range nodeInfo .Pods () {
196
200
if err := processPod (existingPod ); err != nil {
197
201
pm .setError (err )
@@ -214,6 +218,9 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
214
218
}
215
219
216
220
for _ , node := range nodes {
221
+ if pm .counts [node .Name ] == nil {
222
+ continue
223
+ }
217
224
if * pm .counts [node .Name ] > maxCount {
218
225
maxCount = * pm .counts [node .Name ]
219
226
}
@@ -224,9 +231,10 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
224
231
225
232
// calculate final priority score for each node
226
233
result := make (schedulerapi.HostPriorityList , 0 , len (nodes ))
234
+ maxMinDiff := maxCount - minCount
227
235
for _ , node := range nodes {
228
236
fScore := float64 (0 )
229
- if ( maxCount - minCount ) > 0 {
237
+ if maxMinDiff > 0 && pm . counts [ node . Name ] != nil {
230
238
fScore = float64 (schedulerapi .MaxPriority ) * (float64 (* pm .counts [node .Name ]- minCount ) / float64 (maxCount - minCount ))
231
239
}
232
240
result = append (result , schedulerapi.HostPriority {Host : node .Name , Score : int (fScore )})
0 commit comments