Skip to content

Commit 492b970

Browse files
committed
lazy/dynamic initilization on the int64 pointers of inter-podaffinity priority
1 parent 854a266 commit 492b970

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pkg/scheduler/algorithm/priorities/interpod_affinity.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
122122
// the node.
123123
pm := newPodAffinityPriorityMap(nodes)
124124
allNodeNames := make([]string, 0, len(nodeNameToInfo))
125+
lazyInit := hasAffinityConstraints || hasAntiAffinityConstraints
125126
for name := range nodeNameToInfo {
126127
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+
}
128132
}
129133

130134
// convert the topology key based weights to the node name based weights
@@ -191,7 +195,7 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
191195
nodeInfo := nodeNameToInfo[allNodeNames[i]]
192196
if nodeInfo.Node() != nil {
193197
if hasAffinityConstraints || hasAntiAffinityConstraints {
194-
// We need to process all the nodes.
198+
// We need to process all the pods.
195199
for _, existingPod := range nodeInfo.Pods() {
196200
if err := processPod(existingPod); err != nil {
197201
pm.setError(err)
@@ -214,6 +218,9 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
214218
}
215219

216220
for _, node := range nodes {
221+
if pm.counts[node.Name] == nil {
222+
continue
223+
}
217224
if *pm.counts[node.Name] > maxCount {
218225
maxCount = *pm.counts[node.Name]
219226
}
@@ -224,9 +231,10 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
224231

225232
// calculate final priority score for each node
226233
result := make(schedulerapi.HostPriorityList, 0, len(nodes))
234+
maxMinDiff := maxCount - minCount
227235
for _, node := range nodes {
228236
fScore := float64(0)
229-
if (maxCount - minCount) > 0 {
237+
if maxMinDiff > 0 && pm.counts[node.Name] != nil {
230238
fScore = float64(schedulerapi.MaxPriority) * (float64(*pm.counts[node.Name]-minCount) / float64(maxCount-minCount))
231239
}
232240
result = append(result, schedulerapi.HostPriority{Host: node.Name, Score: int(fScore)})

0 commit comments

Comments
 (0)