diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go b/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go index a5f0f4f516b..760b8580c62 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go @@ -200,8 +200,7 @@ func (pl *PodTopologySpread) Score(ctx context.Context, cycleState *framework.Cy pair := topologyPair{key: c.TopologyKey, value: tpVal} cnt = *s.TopologyPairToPodCounts[pair] } - cnt = adjustForMaxSkew(cnt, int64(c.MaxSkew)) - score += float64(cnt) * s.TopologyNormalizingWeight[i] + score += scoreForCount(cnt, c.MaxSkew, s.TopologyNormalizingWeight[i]) } } return int64(score), nil @@ -287,13 +286,10 @@ func topologyNormalizingWeight(size int) float64 { return math.Log(float64(size + 2)) } -// adjustForMaxSkew adjusts the number of matching pods in a topology domain -// using the constraint's maxSkew. -// Topology domains with less than maxSkew number of pods are considered to have -// the same priority. -func adjustForMaxSkew(cnt, maxSkew int64) int64 { - if cnt < maxSkew { - return maxSkew - 1 - } - return cnt +// scoreForCount calculates the score based on number of matching pods in a +// topology domain, the constraint's maxSkew and the topology weight. +// `maxSkew-1` is added to the score so that differences between topology +// domains get watered down, controlling the tolerance of the score to skews. +func scoreForCount(cnt int64, maxSkew int32, tpWeight float64) float64 { + return float64(cnt)*tpWeight + float64(maxSkew-1) } diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go b/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go index 046b120b3bc..3150a815e0a 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go @@ -314,12 +314,11 @@ func TestPodTopologySpreadScore(t *testing.T) { }, }, { - // matching pods spread as 2/1/0/3. - // With maxSkew=2, counts change internally to 2/1/1/3. name: "one constraint on node, all 4 nodes are candidates, maxSkew=2", pod: st.MakePod().Name("p").Label("foo", ""). SpreadConstraint(2, v1.LabelHostname, v1.ScheduleAnyway, st.MakeLabelSelector().Exists("foo").Obj()). Obj(), + // matching pods spread as 2/1/0/3. existingPods: []*v1.Pod{ st.MakePod().Name("p-a1").Node("node-a").Label("foo", "").Obj(), st.MakePod().Name("p-a2").Node("node-a").Label("foo", "").Obj(), @@ -336,20 +335,19 @@ func TestPodTopologySpreadScore(t *testing.T) { }, failedNodes: []*v1.Node{}, want: []framework.NodeScore{ - {Name: "node-a", Score: 60}, // +20, compared to maxSkew=1 - {Name: "node-b", Score: 100}, // +20, compared to maxSkew=1 + {Name: "node-a", Score: 50}, // +10, compared to maxSkew=1 + {Name: "node-b", Score: 83}, // +3, compared to maxSkew=1 {Name: "node-c", Score: 100}, - {Name: "node-d", Score: 20}, // +20, compared to maxSkew=1 + {Name: "node-d", Score: 16}, // +16, compared to maxSkew=1 }, }, { - // matching pods spread as 4/3/2/1. - // With maxSkew=3, counts change internally to 4/3/2/2. name: "one constraint on node, all 4 nodes are candidates, maxSkew=3", pod: st.MakePod().Name("p").Label("foo", ""). SpreadConstraint(3, v1.LabelHostname, v1.ScheduleAnyway, st.MakeLabelSelector().Exists("foo").Obj()). Obj(), existingPods: []*v1.Pod{ + // matching pods spread as 4/3/2/1. st.MakePod().Name("p-a1").Node("node-a").Label("foo", "").Obj(), st.MakePod().Name("p-a2").Node("node-a").Label("foo", "").Obj(), st.MakePod().Name("p-a3").Node("node-a").Label("foo", "").Obj(), @@ -369,9 +367,9 @@ func TestPodTopologySpreadScore(t *testing.T) { }, failedNodes: []*v1.Node{}, want: []framework.NodeScore{ - {Name: "node-a", Score: 42}, // +28 compared to maxSkew=1 - {Name: "node-b", Score: 71}, // +29 compared to maxSkew=1 - {Name: "node-c", Score: 100}, // +29 compared to maxSkew=1 + {Name: "node-a", Score: 33}, // +19 compared to maxSkew=1 + {Name: "node-b", Score: 55}, // +13 compared to maxSkew=1 + {Name: "node-c", Score: 77}, // +6 compared to maxSkew=1 {Name: "node-d", Score: 100}, }, },