@@ -18,7 +18,7 @@ package priorities
18
18
19
19
import (
20
20
"context"
21
- "sync/atomic "
21
+ "sync"
22
22
23
23
v1 "k8s.io/api/core/v1"
24
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -47,17 +47,20 @@ func NewInterPodAffinityPriority(nodeLister schedulerlisters.NodeLister, hardPod
47
47
return interPodAffinity .CalculateInterPodAffinityPriority
48
48
}
49
49
50
+ type topologyPairToScore map [string ]map [string ]int64
51
+
50
52
type podAffinityPriorityMap struct {
51
53
// nodes contain all nodes that should be considered.
52
54
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
55
58
}
56
59
57
60
func newPodAffinityPriorityMap (nodes []* v1.Node ) * podAffinityPriorityMap {
58
61
return & podAffinityPriorityMap {
59
- nodes : nodes ,
60
- counts : make ([] int64 , len ( nodes ) ),
62
+ nodes : nodes ,
63
+ topologyScore : make (topologyPairToScore ),
61
64
}
62
65
}
63
66
@@ -67,13 +70,19 @@ func (p *podAffinityPriorityMap) processTerm(term *v1.PodAffinityTerm, podDefini
67
70
if err != nil {
68
71
return err
69
72
}
73
+ if len (fixedNode .Labels ) == 0 {
74
+ return nil
75
+ }
76
+
70
77
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 )
76
83
}
84
+ p.topologyScore [term.TopologyKey ][tpValue ] += weight
85
+ p .Unlock ()
77
86
}
78
87
return nil
79
88
}
@@ -203,12 +212,20 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
203
212
return nil , err
204
213
}
205
214
215
+ counts := make ([]int64 , len (nodes ))
206
216
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 ]
209
226
}
210
- if pm . counts [i ] < minCount {
211
- minCount = pm . counts [i ]
227
+ if counts [i ] < minCount {
228
+ minCount = counts [i ]
212
229
}
213
230
}
214
231
@@ -218,7 +235,7 @@ func (ipa *InterPodAffinity) CalculateInterPodAffinityPriority(pod *v1.Pod, node
218
235
for i , node := range nodes {
219
236
fScore := float64 (0 )
220
237
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 ))
222
239
}
223
240
result = append (result , framework.NodeScore {Name : node .Name , Score : int64 (fScore )})
224
241
if klog .V (10 ) {
0 commit comments