Skip to content

Commit 418c2cb

Browse files
authored
Merge pull request kubernetes#91918 from alculquicondor/new_max_skew
Use maxSkew in PodTopologySpread scoring as tolerance to skew
2 parents 3b14329 + d353cc1 commit 418c2cb

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ func (pl *PodTopologySpread) Score(ctx context.Context, cycleState *framework.Cy
200200
pair := topologyPair{key: c.TopologyKey, value: tpVal}
201201
cnt = *s.TopologyPairToPodCounts[pair]
202202
}
203-
cnt = adjustForMaxSkew(cnt, int64(c.MaxSkew))
204-
score += float64(cnt) * s.TopologyNormalizingWeight[i]
203+
score += scoreForCount(cnt, c.MaxSkew, s.TopologyNormalizingWeight[i])
205204
}
206205
}
207206
return int64(score), nil
@@ -287,13 +286,10 @@ func topologyNormalizingWeight(size int) float64 {
287286
return math.Log(float64(size + 2))
288287
}
289288

290-
// adjustForMaxSkew adjusts the number of matching pods in a topology domain
291-
// using the constraint's maxSkew.
292-
// Topology domains with less than maxSkew number of pods are considered to have
293-
// the same priority.
294-
func adjustForMaxSkew(cnt, maxSkew int64) int64 {
295-
if cnt < maxSkew {
296-
return maxSkew - 1
297-
}
298-
return cnt
289+
// scoreForCount calculates the score based on number of matching pods in a
290+
// topology domain, the constraint's maxSkew and the topology weight.
291+
// `maxSkew-1` is added to the score so that differences between topology
292+
// domains get watered down, controlling the tolerance of the score to skews.
293+
func scoreForCount(cnt int64, maxSkew int32, tpWeight float64) float64 {
294+
return float64(cnt)*tpWeight + float64(maxSkew-1)
299295
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,11 @@ func TestPodTopologySpreadScore(t *testing.T) {
314314
},
315315
},
316316
{
317-
// matching pods spread as 2/1/0/3.
318-
// With maxSkew=2, counts change internally to 2/1/1/3.
319317
name: "one constraint on node, all 4 nodes are candidates, maxSkew=2",
320318
pod: st.MakePod().Name("p").Label("foo", "").
321319
SpreadConstraint(2, v1.LabelHostname, v1.ScheduleAnyway, st.MakeLabelSelector().Exists("foo").Obj()).
322320
Obj(),
321+
// matching pods spread as 2/1/0/3.
323322
existingPods: []*v1.Pod{
324323
st.MakePod().Name("p-a1").Node("node-a").Label("foo", "").Obj(),
325324
st.MakePod().Name("p-a2").Node("node-a").Label("foo", "").Obj(),
@@ -336,20 +335,19 @@ func TestPodTopologySpreadScore(t *testing.T) {
336335
},
337336
failedNodes: []*v1.Node{},
338337
want: []framework.NodeScore{
339-
{Name: "node-a", Score: 60}, // +20, compared to maxSkew=1
340-
{Name: "node-b", Score: 100}, // +20, compared to maxSkew=1
338+
{Name: "node-a", Score: 50}, // +10, compared to maxSkew=1
339+
{Name: "node-b", Score: 83}, // +3, compared to maxSkew=1
341340
{Name: "node-c", Score: 100},
342-
{Name: "node-d", Score: 20}, // +20, compared to maxSkew=1
341+
{Name: "node-d", Score: 16}, // +16, compared to maxSkew=1
343342
},
344343
},
345344
{
346-
// matching pods spread as 4/3/2/1.
347-
// With maxSkew=3, counts change internally to 4/3/2/2.
348345
name: "one constraint on node, all 4 nodes are candidates, maxSkew=3",
349346
pod: st.MakePod().Name("p").Label("foo", "").
350347
SpreadConstraint(3, v1.LabelHostname, v1.ScheduleAnyway, st.MakeLabelSelector().Exists("foo").Obj()).
351348
Obj(),
352349
existingPods: []*v1.Pod{
350+
// matching pods spread as 4/3/2/1.
353351
st.MakePod().Name("p-a1").Node("node-a").Label("foo", "").Obj(),
354352
st.MakePod().Name("p-a2").Node("node-a").Label("foo", "").Obj(),
355353
st.MakePod().Name("p-a3").Node("node-a").Label("foo", "").Obj(),
@@ -369,9 +367,9 @@ func TestPodTopologySpreadScore(t *testing.T) {
369367
},
370368
failedNodes: []*v1.Node{},
371369
want: []framework.NodeScore{
372-
{Name: "node-a", Score: 42}, // +28 compared to maxSkew=1
373-
{Name: "node-b", Score: 71}, // +29 compared to maxSkew=1
374-
{Name: "node-c", Score: 100}, // +29 compared to maxSkew=1
370+
{Name: "node-a", Score: 33}, // +19 compared to maxSkew=1
371+
{Name: "node-b", Score: 55}, // +13 compared to maxSkew=1
372+
{Name: "node-c", Score: 77}, // +6 compared to maxSkew=1
375373
{Name: "node-d", Score: 100},
376374
},
377375
},

0 commit comments

Comments
 (0)