Skip to content

Commit 4b31b55

Browse files
Count spreading node matches for hostname topology in Score
1 parent 1616525 commit 4b31b55

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

pkg/scheduler/framework/plugins/podtopologyspread/common.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,17 @@ func filterTopologySpreadConstraints(constraints []v1.TopologySpreadConstraint,
8282
}
8383
return result, nil
8484
}
85+
86+
func countPodsMatchSelector(pods []*v1.Pod, selector labels.Selector, ns string) int {
87+
count := 0
88+
for _, p := range pods {
89+
// Bypass terminating Pod (see #87621).
90+
if p.DeletionTimestamp != nil || p.Namespace != ns {
91+
continue
92+
}
93+
if selector.Matches(labels.Set(p.Labels)) {
94+
count++
95+
}
96+
}
97+
return count
98+
}

pkg/scheduler/framework/plugins/podtopologyspread/scoring.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"sync/atomic"
2424

2525
v1 "k8s.io/api/core/v1"
26-
"k8s.io/apimachinery/pkg/labels"
2726
"k8s.io/apimachinery/pkg/util/sets"
2827
"k8s.io/klog"
2928
pluginhelper "k8s.io/kubernetes/pkg/scheduler/framework/plugins/helper"
@@ -74,6 +73,10 @@ func (pl *PodTopologySpread) initPreScoreState(s *preScoreState, pod *v1.Pod, fi
7473
continue
7574
}
7675
for _, constraint := range s.Constraints {
76+
// per-node counts are calculated during Score.
77+
if constraint.TopologyKey == v1.LabelHostname {
78+
continue
79+
}
7780
pair := topologyPair{key: constraint.TopologyKey, value: node.Labels[constraint.TopologyKey]}
7881
if s.TopologyPairToPodCounts[pair] == nil {
7982
s.TopologyPairToPodCounts[pair] = new(int64)
@@ -104,7 +107,7 @@ func (pl *PodTopologySpread) PreScore(
104107
}
105108

106109
state := &preScoreState{
107-
NodeNameSet: sets.String{},
110+
NodeNameSet: make(sets.String, len(filteredNodes)),
108111
TopologyPairToPodCounts: make(map[topologyPair]*int64),
109112
}
110113
err = pl.initPreScoreState(state, pod, filteredNodes)
@@ -135,22 +138,13 @@ func (pl *PodTopologySpread) PreScore(
135138
pair := topologyPair{key: c.TopologyKey, value: node.Labels[c.TopologyKey]}
136139
// If current topology pair is not associated with any candidate node,
137140
// 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 {
139144
continue
140145
}
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))
154148
}
155149
}
156150
parallelize.Until(ctx, len(allNodes), processAllNode)
@@ -184,9 +178,14 @@ func (pl *PodTopologySpread) Score(ctx context.Context, cycleState *framework.Cy
184178
var score int64
185179
for _, c := range s.Constraints {
186180
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+
}
190189
}
191190
}
192191
return score, nil

0 commit comments

Comments
 (0)