Merge pull request #101859 from yuzhiquan/fix-potential-painc-scheduler

Fix potential panic for node resource plugin
This commit is contained in:
Kubernetes Prow Robot 2021-05-10 09:45:58 -07:00 committed by GitHub
commit 20b0f6b2a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 0 deletions

View File

@ -97,6 +97,9 @@ func leastResourceScorer(resToWeightMap resourceToWeightMap) func(resourceToValu
nodeScore += resourceScore * weight
weightSum += weight
}
if weightSum == 0 {
return 0
}
return nodeScore / weightSum
}
}

View File

@ -138,6 +138,13 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
expectedList: []framework.NodeScore{{Name: "machine1", Score: 37}, {Name: "machine2", Score: 50}},
name: "nothing scheduled, resources requested, differently sized machines",
},
{
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{}},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}},
name: "Resources not set, nothing scheduled, resources requested, differently sized machines",
},
{
// Node1 scores on 0-MaxNodeScore scale
// CPU Score: ((4000 - 0) * MaxNodeScore) / 4000 = MaxNodeScore

View File

@ -95,6 +95,9 @@ func mostResourceScorer(resToWeightMap resourceToWeightMap) func(requested, allo
nodeScore += resourceScore * weight
weightSum += weight
}
if weightSum == 0 {
return 0
}
return (nodeScore / weightSum)
}
}

View File

@ -153,6 +153,13 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
expectedList: []framework.NodeScore{{Name: "machine1", Score: 62}, {Name: "machine2", Score: 50}},
name: "nothing scheduled, resources requested, differently sized machines",
},
{
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: []config.ResourceSpec{}},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}},
name: "Resources not set, nothing scheduled, resources requested, differently sized machines",
},
{
// Node1 scores on 0-MaxNodeScore scale
// CPU Score: (6000 * MaxNodeScore) / 10000 = 60