@@ -32,9 +32,11 @@ import (
32
32
// preScoreStateKey is the key in CycleState to InterPodAffinity pre-computed data for Scoring.
33
33
const preScoreStateKey = "PreScore" + Name
34
34
35
+ type scoreMap map [string ]map [string ]int64
36
+
35
37
// preScoreState computed at PreScore and used at Score.
36
38
type preScoreState struct {
37
- topologyScore map [ string ] map [ string ] int64
39
+ topologyScore scoreMap
38
40
affinityTerms []* weightedAffinityTerm
39
41
antiAffinityTerms []* weightedAffinityTerm
40
42
}
@@ -76,8 +78,7 @@ func getWeightedAffinityTerms(pod *v1.Pod, v1Terms []v1.WeightedPodAffinityTerm)
76
78
return terms , nil
77
79
}
78
80
79
- func (pl * InterPodAffinity ) processTerm (
80
- state * preScoreState ,
81
+ func (m scoreMap ) processTerm (
81
82
term * weightedAffinityTerm ,
82
83
podToCheck * v1.Pod ,
83
84
fixedNode * v1.Node ,
@@ -90,24 +91,34 @@ func (pl *InterPodAffinity) processTerm(
90
91
match := schedutil .PodMatchesTermsNamespaceAndSelector (podToCheck , term .namespaces , term .selector )
91
92
tpValue , tpValueExist := fixedNode .Labels [term .topologyKey ]
92
93
if match && tpValueExist {
93
- pl .Lock ()
94
- if state .topologyScore [term .topologyKey ] == nil {
95
- state .topologyScore [term .topologyKey ] = make (map [string ]int64 )
94
+ if m [term .topologyKey ] == nil {
95
+ m [term .topologyKey ] = make (map [string ]int64 )
96
96
}
97
- state.topologyScore [term.topologyKey ][tpValue ] += int64 (term .weight * int32 (multiplier ))
98
- pl .Unlock ()
97
+ m [term.topologyKey ][tpValue ] += int64 (term .weight * int32 (multiplier ))
99
98
}
100
99
return
101
100
}
102
101
103
- func (pl * InterPodAffinity ) processTerms (state * preScoreState , terms []* weightedAffinityTerm , podToCheck * v1.Pod , fixedNode * v1.Node , multiplier int ) error {
102
+ func (m scoreMap ) processTerms (terms []* weightedAffinityTerm , podToCheck * v1.Pod , fixedNode * v1.Node , multiplier int ) {
104
103
for _ , term := range terms {
105
- pl .processTerm (state , term , podToCheck , fixedNode , multiplier )
104
+ m .processTerm (term , podToCheck , fixedNode , multiplier )
105
+ }
106
+ }
107
+
108
+ func (m scoreMap ) append (other scoreMap ) {
109
+ for topology , oScores := range other {
110
+ scores := m [topology ]
111
+ if scores == nil {
112
+ m [topology ] = oScores
113
+ continue
114
+ }
115
+ for k , v := range oScores {
116
+ scores [k ] += v
117
+ }
106
118
}
107
- return nil
108
119
}
109
120
110
- func (pl * InterPodAffinity ) processExistingPod (state * preScoreState , existingPod * v1.Pod , existingPodNodeInfo * nodeinfo.NodeInfo , incomingPod * v1.Pod ) error {
121
+ func (pl * InterPodAffinity ) processExistingPod (state * preScoreState , existingPod * v1.Pod , existingPodNodeInfo * nodeinfo.NodeInfo , incomingPod * v1.Pod , topoScore scoreMap ) error {
111
122
existingPodAffinity := existingPod .Spec .Affinity
112
123
existingHasAffinityConstraints := existingPodAffinity != nil && existingPodAffinity .PodAffinity != nil
113
124
existingHasAntiAffinityConstraints := existingPodAffinity != nil && existingPodAffinity .PodAntiAffinity != nil
@@ -116,12 +127,12 @@ func (pl *InterPodAffinity) processExistingPod(state *preScoreState, existingPod
116
127
// For every soft pod affinity term of <pod>, if <existingPod> matches the term,
117
128
// increment <p.counts> for every node in the cluster with the same <term.TopologyKey>
118
129
// value as that of <existingPods>`s node by the term`s weight.
119
- pl .processTerms (state , state .affinityTerms , existingPod , existingPodNode , 1 )
130
+ topoScore .processTerms (state .affinityTerms , existingPod , existingPodNode , 1 )
120
131
121
132
// For every soft pod anti-affinity term of <pod>, if <existingPod> matches the term,
122
133
// decrement <p.counts> for every node in the cluster with the same <term.TopologyKey>
123
134
// value as that of <existingPod>`s node by the term`s weight.
124
- pl .processTerms (state , state .antiAffinityTerms , existingPod , existingPodNode , - 1 )
135
+ topoScore .processTerms (state .antiAffinityTerms , existingPod , existingPodNode , - 1 )
125
136
126
137
if existingHasAffinityConstraints {
127
138
// For every hard pod affinity term of <existingPod>, if <pod> matches the term,
@@ -139,7 +150,7 @@ func (pl *InterPodAffinity) processExistingPod(state *preScoreState, existingPod
139
150
if err != nil {
140
151
return err
141
152
}
142
- pl .processTerm (state , processedTerm , incomingPod , existingPodNode , 1 )
153
+ topoScore .processTerm (processedTerm , incomingPod , existingPodNode , 1 )
143
154
}
144
155
}
145
156
// For every soft pod affinity term of <existingPod>, if <pod> matches the term,
@@ -151,7 +162,7 @@ func (pl *InterPodAffinity) processExistingPod(state *preScoreState, existingPod
151
162
return nil
152
163
}
153
164
154
- pl .processTerms (state , terms , incomingPod , existingPodNode , 1 )
165
+ topoScore .processTerms (terms , incomingPod , existingPodNode , 1 )
155
166
}
156
167
if existingHasAntiAffinityConstraints {
157
168
// For every soft pod anti-affinity term of <existingPod>, if <pod> matches the term,
@@ -161,7 +172,7 @@ func (pl *InterPodAffinity) processExistingPod(state *preScoreState, existingPod
161
172
if err != nil {
162
173
return err
163
174
}
164
- pl .processTerms (state , terms , incomingPod , existingPodNode , - 1 )
175
+ topoScore .processTerms (terms , incomingPod , existingPodNode , - 1 )
165
176
}
166
177
return nil
167
178
}
@@ -235,12 +246,18 @@ func (pl *InterPodAffinity) PreScore(
235
246
podsToProcess = nodeInfo .Pods ()
236
247
}
237
248
249
+ topoScore := make (scoreMap )
238
250
for _ , existingPod := range podsToProcess {
239
- if err := pl .processExistingPod (state , existingPod , nodeInfo , pod ); err != nil {
251
+ if err := pl .processExistingPod (state , existingPod , nodeInfo , pod , topoScore ); err != nil {
240
252
errCh .SendErrorWithCancel (err , cancel )
241
253
return
242
254
}
243
255
}
256
+ if len (topoScore ) > 0 {
257
+ pl .Lock ()
258
+ state .topologyScore .append (topoScore )
259
+ pl .Unlock ()
260
+ }
244
261
}
245
262
workqueue .ParallelizeUntil (ctx , 16 , len (allNodes ), processNode )
246
263
if err := errCh .ReceiveError (); err != nil {
0 commit comments