@@ -23,7 +23,6 @@ import (
23
23
"sync/atomic"
24
24
25
25
v1 "k8s.io/api/core/v1"
26
- "k8s.io/apimachinery/pkg/labels"
27
26
"k8s.io/apimachinery/pkg/util/sets"
28
27
"k8s.io/klog"
29
28
pluginhelper "k8s.io/kubernetes/pkg/scheduler/framework/plugins/helper"
@@ -74,6 +73,10 @@ func (pl *PodTopologySpread) initPreScoreState(s *preScoreState, pod *v1.Pod, fi
74
73
continue
75
74
}
76
75
for _ , constraint := range s .Constraints {
76
+ // per-node counts are calculated during Score.
77
+ if constraint .TopologyKey == v1 .LabelHostname {
78
+ continue
79
+ }
77
80
pair := topologyPair {key : constraint .TopologyKey , value : node .Labels [constraint .TopologyKey ]}
78
81
if s .TopologyPairToPodCounts [pair ] == nil {
79
82
s .TopologyPairToPodCounts [pair ] = new (int64 )
@@ -104,7 +107,7 @@ func (pl *PodTopologySpread) PreScore(
104
107
}
105
108
106
109
state := & preScoreState {
107
- NodeNameSet : sets.String {} ,
110
+ NodeNameSet : make ( sets.String , len ( filteredNodes )) ,
108
111
TopologyPairToPodCounts : make (map [topologyPair ]* int64 ),
109
112
}
110
113
err = pl .initPreScoreState (state , pod , filteredNodes )
@@ -135,22 +138,13 @@ func (pl *PodTopologySpread) PreScore(
135
138
pair := topologyPair {key : c .TopologyKey , value : node .Labels [c .TopologyKey ]}
136
139
// If current topology pair is not associated with any candidate node,
137
140
// continue to avoid unnecessary calculation.
138
- if state .TopologyPairToPodCounts [pair ] == nil {
141
+ // Per-node counts are also skipped, as they are done during Score.
142
+ tpCount := state .TopologyPairToPodCounts [pair ]
143
+ if tpCount == nil {
139
144
continue
140
145
}
141
-
142
- // <matchSum> indicates how many pods (on current node) match the <constraint>.
143
- matchSum := int64 (0 )
144
- for _ , existingPod := range nodeInfo .Pods () {
145
- // Bypass terminating Pod (see #87621).
146
- if existingPod .DeletionTimestamp != nil || existingPod .Namespace != pod .Namespace {
147
- continue
148
- }
149
- if c .Selector .Matches (labels .Set (existingPod .Labels )) {
150
- matchSum ++
151
- }
152
- }
153
- atomic .AddInt64 (state .TopologyPairToPodCounts [pair ], matchSum )
146
+ count := countPodsMatchSelector (nodeInfo .Pods (), c .Selector , pod .Namespace )
147
+ atomic .AddInt64 (tpCount , int64 (count ))
154
148
}
155
149
}
156
150
parallelize .Until (ctx , len (allNodes ), processAllNode )
@@ -184,9 +178,14 @@ func (pl *PodTopologySpread) Score(ctx context.Context, cycleState *framework.Cy
184
178
var score int64
185
179
for _ , c := range s .Constraints {
186
180
if tpVal , ok := node .Labels [c .TopologyKey ]; ok {
187
- pair := topologyPair {key : c .TopologyKey , value : tpVal }
188
- matchSum := * s .TopologyPairToPodCounts [pair ]
189
- score += matchSum
181
+ if c .TopologyKey == v1 .LabelHostname {
182
+ count := countPodsMatchSelector (nodeInfo .Pods (), c .Selector , pod .Namespace )
183
+ score += int64 (count )
184
+ } else {
185
+ pair := topologyPair {key : c .TopologyKey , value : tpVal }
186
+ matchSum := * s .TopologyPairToPodCounts [pair ]
187
+ score += matchSum
188
+ }
190
189
}
191
190
}
192
191
return score , nil
0 commit comments