mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
fix potential panic for node resource plugin
This commit is contained in:
parent
3dd0597843
commit
2b6af46624
@ -97,6 +97,9 @@ func leastResourceScorer(resToWeightMap resourceToWeightMap) func(resourceToValu
|
|||||||
nodeScore += resourceScore * weight
|
nodeScore += resourceScore * weight
|
||||||
weightSum += weight
|
weightSum += weight
|
||||||
}
|
}
|
||||||
|
if weightSum == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return nodeScore / weightSum
|
return nodeScore / weightSum
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,6 +138,13 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
|
|||||||
expectedList: []framework.NodeScore{{Name: "machine1", Score: 37}, {Name: "machine2", Score: 50}},
|
expectedList: []framework.NodeScore{{Name: "machine1", Score: 37}, {Name: "machine2", Score: 50}},
|
||||||
name: "nothing scheduled, resources requested, differently sized machines",
|
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
|
// Node1 scores on 0-MaxNodeScore scale
|
||||||
// CPU Score: ((4000 - 0) * MaxNodeScore) / 4000 = MaxNodeScore
|
// CPU Score: ((4000 - 0) * MaxNodeScore) / 4000 = MaxNodeScore
|
||||||
|
@ -95,6 +95,9 @@ func mostResourceScorer(resToWeightMap resourceToWeightMap) func(requested, allo
|
|||||||
nodeScore += resourceScore * weight
|
nodeScore += resourceScore * weight
|
||||||
weightSum += weight
|
weightSum += weight
|
||||||
}
|
}
|
||||||
|
if weightSum == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return (nodeScore / weightSum)
|
return (nodeScore / weightSum)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,6 +153,13 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
|
|||||||
expectedList: []framework.NodeScore{{Name: "machine1", Score: 62}, {Name: "machine2", Score: 50}},
|
expectedList: []framework.NodeScore{{Name: "machine1", Score: 62}, {Name: "machine2", Score: 50}},
|
||||||
name: "nothing scheduled, resources requested, differently sized machines",
|
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
|
// Node1 scores on 0-MaxNodeScore scale
|
||||||
// CPU Score: (6000 * MaxNodeScore) / 10000 = 60
|
// CPU Score: (6000 * MaxNodeScore) / 10000 = 60
|
||||||
|
Loading…
Reference in New Issue
Block a user