Skip to content

Commit 2905275

Browse files
authored
Merge pull request kubernetes#84264 from ahg-g/ahg-antiaffinity
Optimize interpod affinity priority function
2 parents 3325cbb + b213255 commit 2905275

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

pkg/scheduler/algorithm/priorities/interpod_affinity.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package priorities
1818

1919
import (
2020
"context"
21-
"sync/atomic"
21+
"sync"
2222

2323
v1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -47,17 +47,20 @@ func NewInterPodAffinityPriority(nodeLister schedulerlisters.NodeLister, hardPod
4747
return interPodAffinity.CalculateInterPodAffinityPriority
4848
}
4949

50+
type topologyPairToScore map[string]map[string]int64
51+
5052
type podAffinityPriorityMap struct {
5153
// nodes contain all nodes that should be considered.
5254
nodes []*v1.Node
53-
// counts store the so-far computed score for each node.
54-
counts []int64
55+
// tracks a topology pair score so far.
56+
topologyScore topologyPairToScore
57+
sync.Mutex
5558
}
5659

5760
func newPodAffinityPriorityMap(nodes []*v1.Node) *podAffinityPriorityMap {
5861
return &podAffinityPriorityMap{
59-
nodes: nodes,
60-
counts: make([]int64, len(nodes)),
62+
nodes: nodes,
63+
topologyScore: make(topologyPairToScore),
6164
}
6265
}
6366

@@ -67,13 +70,19 @@ func (p *podAffinityPriorityMap) processTerm(term *v1.PodAffinityTerm, podDefini
6770
if err != nil {
6871
return err
6972
}
73+
if len(fixedNode.Labels) == 0 {
74+
return nil
75+
}
76+
7077
match := priorityutil.PodMatchesTermsNamespaceAndSelector(podToCheck, namespaces, selector)
71-
if match {
72-
for i, node := range p.nodes {
73-
if priorityutil.NodesHaveSameTopologyKey(node, fixedNode, term.TopologyKey) {
74-
atomic.AddInt64(&p.counts[i], weight)
75-
}
78+
tpValue, tpValueExist := fixedNode.Labels[term.TopologyKey]
79+
if match && tpValueExist {
80+
p.Lock()
81+
if p.topologyScore[term.TopologyKey] == nil {
82+
p.topologyScore[term.TopologyKey] = make(map[string]int64)
7683
}
84+
p.topologyScore[term.TopologyKey][tpValue] += weight
85+
p.Unlock()
7786
}
7887
return nil
7988
}
@@ -203,12 +212,20 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
203212
return nil, err
204213
}
205214

215+
counts := make([]int64, len(nodes))
206216
for i := range nodes {
207-
if pm.counts[i] > maxCount {
208-
maxCount = pm.counts[i]
217+
if nodes[i].Labels != nil {
218+
for tpKey, tpValues := range pm.topologyScore {
219+
if v, exist := nodes[i].Labels[tpKey]; exist {
220+
counts[i] += tpValues[v]
221+
}
222+
}
223+
}
224+
if counts[i] > maxCount {
225+
maxCount = counts[i]
209226
}
210-
if pm.counts[i] < minCount {
211-
minCount = pm.counts[i]
227+
if counts[i] < minCount {
228+
minCount = counts[i]
212229
}
213230
}
214231

@@ -218,7 +235,7 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
218235
for i, node := range nodes {
219236
fScore := float64(0)
220237
if maxMinDiff > 0 {
221-
fScore = float64(framework.MaxNodeScore) * (float64(pm.counts[i]-minCount) / float64(maxCount-minCount))
238+
fScore = float64(framework.MaxNodeScore) * (float64(counts[i]-minCount) / float64(maxCount-minCount))
222239
}
223240
result = append(result, framework.NodeScore{Name: node.Name, Score: int64(fScore)})
224241
if klog.V(10) {

0 commit comments

Comments
 (0)