Fix Node Resources plugins score when there are pods with no requests

Given that we give a default CPU/memory requests for containers that don't provide any, the calculated usage can exceed the allocatable.

Change-Id: I72e249652acacfbe8cea0dd6f895dabe43ff6376
This commit is contained in:
Aldo Culquicondor
2021-06-16 17:46:05 +00:00
parent 4b387beb0c
commit 63d1237102
4 changed files with 78 additions and 31 deletions

View File

@@ -111,6 +111,13 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
},
},
}
nonZeroContainer := v1.PodSpec{
Containers: []v1.Container{{}},
}
nonZeroContainer1 := v1.PodSpec{
NodeName: "machine1",
Containers: []v1.Container{{}},
}
defaultResourceMostAllocatedSet := []config.ResourceSpec{
{Name: string(v1.ResourceCPU), Weight: 1},
{Name: string(v1.ResourceMemory), Weight: 1},
@@ -203,18 +210,18 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
},
{
// Node1 scores on 0-MaxNodeScore scale
// CPU Score: 5000 > 4000 return 0
// CPU Score: 5000 * MaxNodeScore / 5000 return 100
// Memory Score: (9000 * MaxNodeScore) / 10000 = 90
// Node1 Score: (0 + 90) / 2 = 45
// Node1 Score: (100 + 90) / 2 = 95
// Node2 scores on 0-MaxNodeScore scale
// CPU Score: (5000 * MaxNodeScore) / 10000 = 50
// Memory Score: 9000 > 8000 return 0
// Node2 Score: (50 + 0) / 2 = 25
// Memory Score: 8000 *MaxNodeScore / 8000 return 100
// Node2 Score: (50 + 100) / 2 = 75
pod: &v1.Pod{Spec: bigCPUAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 10000, 8000)},
nodes: []*v1.Node{makeNode("machine1", 5000, 10000), makeNode("machine2", 10000, 9000)},
args: config.NodeResourcesMostAllocatedArgs{Resources: defaultResourceMostAllocatedSet},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 45}, {Name: "machine2", Score: 25}},
name: "resources requested with more than the node, pods scheduled with resources",
expectedList: []framework.NodeScore{{Name: "machine1", Score: 95}, {Name: "machine2", Score: 75}},
name: "resources requested equal node capacity",
},
{
// CPU Score: (3000 *100) / 4000 = 75
@@ -229,6 +236,25 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
expectedList: []framework.NodeScore{{Name: "machine1", Score: 58}, {Name: "machine2", Score: 50}},
name: "nothing scheduled, resources requested, differently sized machines",
},
{
// Node1 scores on 0-MaxNodeScore scale
// CPU Fraction: 300 / 250 = 100%
// Memory Fraction: 600 / 10000 = 60%
// Node1 Score: (100 + 60) / 2 = 80
// Node2 scores on 0-MaxNodeScore scale
// CPU Fraction: 100 / 250 = 40%
// Memory Fraction: 200 / 10000 = 20%
// Node2 Score: (20 + 40) / 2 = 30
pod: &v1.Pod{Spec: nonZeroContainer},
nodes: []*v1.Node{makeNode("machine1", 250, 1000*1024*1024), makeNode("machine2", 250, 1000*1024*1024)},
args: config.NodeResourcesMostAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 1}, {Name: "cpu", Weight: 1}}},
expectedList: []framework.NodeScore{{Name: "machine1", Score: 80}, {Name: "machine2", Score: 30}},
name: "no resources requested, pods scheduled",
pods: []*v1.Pod{
{Spec: nonZeroContainer1},
{Spec: nonZeroContainer1},
},
},
{
// resource with negtive weight is not allowed
pod: &v1.Pod{Spec: cpuAndMemory},
@@ -298,7 +324,7 @@ func TestNodeResourcesMostAllocated(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(test.expectedList[i].Score, hostResult) {
t.Errorf("expected %#v, got %#v", test.expectedList[i].Score, hostResult)
t.Errorf("got score %v for host %v, expected %v", hostResult, test.nodes[i].Name, test.expectedList[i].Score)
}
}
})