fix potential panic for node resource plugin

This commit is contained in:
yuzhiquan 2021-05-08 18:03:24 +08:00
parent 3dd0597843
commit 2b6af46624
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