feat(scheduler): return error when score is out of range

This commit is contained in:
draveness 2019-08-09 17:03:55 +08:00
parent 34791349d6
commit 9fb0df5096
4 changed files with 74 additions and 4 deletions

View File

@ -431,8 +431,16 @@ func (f *framework) ApplyScoreWeights(pc *PluginContext, pod *v1.Pod, scores Plu
errCh.SendErrorWithCancel(err, cancel)
return
}
for i := range nodeScoreList {
nodeScoreList[i].Score = nodeScoreList[i].Score * weight
for i, nodeScore := range nodeScoreList {
// return error if score plugin returns invalid score.
if nodeScore.Score > MaxNodeScore || nodeScore.Score < MinNodeScore {
err := fmt.Errorf("score plugin %q returns an invalid score %q, it should in the range of [MinNodeScore, MaxNodeScore] after normalizing", pl.Name(), nodeScore.Score)
errCh.SendErrorWithCancel(err, cancel)
return
}
nodeScoreList[i].Score = nodeScore.Score * weight
}
})

View File

@ -604,6 +604,60 @@ func TestApplyScoreWeights(t *testing.T) {
},
err: true,
},
{
name: "Score plugin return score greater than MaxNodeScore",
plugins: plugin1And2,
input: PluginToNodeScores{
scorePlugin1: {
{
Name: "node1",
Score: MaxNodeScore + 1,
},
{
Name: "node2",
Score: 3,
},
},
scorePlugin2: {
{
Name: "node1",
Score: MinNodeScore,
},
{
Name: "node2",
Score: 5,
},
},
},
err: true,
},
{
name: "Score plugin return score less than MinNodeScore",
plugins: plugin1And2,
input: PluginToNodeScores{
scorePlugin1: {
{
Name: "node1",
Score: MaxNodeScore,
},
{
Name: "node2",
Score: 3,
},
},
scorePlugin2: {
{
Name: "node1",
Score: MinNodeScore - 1,
},
{
Name: "node2",
Score: 5,
},
},
},
err: true,
},
}
for _, tt := range tests {

View File

@ -61,6 +61,14 @@ const (
Skip
)
const (
// MaxNodeScore is the maximum score a Score plugin is expected to return.
MaxNodeScore int = 10
// MinNodeScore is the minimum score a Score plugin is expected to return.
MinNodeScore int = 0
)
// Status indicates the result of running a plugin. It consists of a code and a
// message. When the status code is not `Success`, the status message should
// explain why.

View File

@ -152,11 +152,11 @@ func (sp *ScorePlugin) Score(pc *framework.PluginContext, p *v1.Pod, nodeName st
return 0, framework.NewStatus(framework.Error, fmt.Sprintf("injecting failure for pod %v", p.Name))
}
score := 10
score := 1
if sp.numScoreCalled == 1 {
// The first node is scored the highest, the rest is scored lower.
sp.highScoreNode = nodeName
score = 100
score = framework.MaxNodeScore
}
return score, nil
}