k3s/pkg/scheduler/algorithm/priorities/least_requested_test.go

267 lines
8.7 KiB
Go
Raw Normal View History

2016-10-26 09:51:11 +00:00
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package priorities
import (
"reflect"
"testing"
2017-06-22 18:24:23 +00:00
"k8s.io/api/core/v1"
2017-01-25 13:13:07 +00:00
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
"k8s.io/kubernetes/pkg/scheduler/schedulercache"
2016-10-26 09:51:11 +00:00
)
func TestLeastRequested(t *testing.T) {
labels1 := map[string]string{
"foo": "bar",
"baz": "blah",
}
labels2 := map[string]string{
"bar": "foo",
"baz": "blah",
}
2016-11-18 20:52:35 +00:00
machine1Spec := v1.PodSpec{
2016-10-26 09:51:11 +00:00
NodeName: "machine1",
}
2016-11-18 20:52:35 +00:00
machine2Spec := v1.PodSpec{
2016-10-26 09:51:11 +00:00
NodeName: "machine2",
}
2016-11-18 20:52:35 +00:00
noResources := v1.PodSpec{
Containers: []v1.Container{},
2016-10-26 09:51:11 +00:00
}
2016-11-18 20:52:35 +00:00
cpuOnly := v1.PodSpec{
2016-10-26 09:51:11 +00:00
NodeName: "machine1",
2016-11-18 20:52:35 +00:00
Containers: []v1.Container{
2016-10-26 09:51:11 +00:00
{
2016-11-18 20:52:35 +00:00
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("1000m"),
v1.ResourceMemory: resource.MustParse("0"),
2016-10-26 09:51:11 +00:00
},
},
},
{
2016-11-18 20:52:35 +00:00
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("2000m"),
v1.ResourceMemory: resource.MustParse("0"),
2016-10-26 09:51:11 +00:00
},
},
},
},
}
cpuOnly2 := cpuOnly
cpuOnly2.NodeName = "machine2"
2016-11-18 20:52:35 +00:00
cpuAndMemory := v1.PodSpec{
2016-10-26 09:51:11 +00:00
NodeName: "machine2",
2016-11-18 20:52:35 +00:00
Containers: []v1.Container{
2016-10-26 09:51:11 +00:00
{
2016-11-18 20:52:35 +00:00
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("1000m"),
v1.ResourceMemory: resource.MustParse("2000"),
2016-10-26 09:51:11 +00:00
},
},
},
{
2016-11-18 20:52:35 +00:00
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("2000m"),
v1.ResourceMemory: resource.MustParse("3000"),
2016-10-26 09:51:11 +00:00
},
},
},
},
}
tests := []struct {
2016-11-18 20:52:35 +00:00
pod *v1.Pod
pods []*v1.Pod
nodes []*v1.Node
2016-10-26 09:51:11 +00:00
expectedList schedulerapi.HostPriorityList
2018-05-10 11:12:20 +00:00
name string
2016-10-26 09:51:11 +00:00
}{
{
/*
Node1 scores (remaining resources) on 0-10 scale
CPU Score: ((4000 - 0) *10) / 4000 = 10
Memory Score: ((10000 - 0) *10) / 10000 = 10
Node1 Score: (10 + 10) / 2 = 10
Node2 scores (remaining resources) on 0-10 scale
CPU Score: ((4000 - 0) *10) / 4000 = 10
Memory Score: ((10000 - 0) *10) / 10000 = 10
Node2 Score: (10 + 10) / 2 = 10
*/
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}},
2018-05-10 11:12:20 +00:00
name: "nothing scheduled, nothing requested",
2016-10-26 09:51:11 +00:00
},
{
/*
Node1 scores on 0-10 scale
CPU Score: ((4000 - 3000) *10) / 4000 = 2.5
Memory Score: ((10000 - 5000) *10) / 10000 = 5
Node1 Score: (2.5 + 5) / 2 = 3
Node2 scores on 0-10 scale
CPU Score: ((6000 - 3000) *10) / 6000 = 5
Memory Score: ((10000 - 5000) *10) / 10000 = 5
Node2 Score: (5 + 5) / 2 = 5
*/
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
2016-10-26 09:51:11 +00:00
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 3}, {Host: "machine2", Score: 5}},
2018-05-10 11:12:20 +00:00
name: "nothing scheduled, resources requested, differently sized machines",
2016-10-26 09:51:11 +00:00
},
{
/*
Node1 scores on 0-10 scale
CPU Score: ((4000 - 0) *10) / 4000 = 10
Memory Score: ((10000 - 0) *10) / 10000 = 10
Node1 Score: (10 + 10) / 2 = 10
Node2 scores on 0-10 scale
CPU Score: ((4000 - 0) *10) / 4000 = 10
Memory Score: ((10000 - 0) *10) / 10000 = 10
Node2 Score: (10 + 10) / 2 = 10
*/
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}},
2018-05-10 11:12:20 +00:00
name: "no resources requested, pods scheduled",
2016-11-18 20:52:35 +00:00
pods: []*v1.Pod{
{Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels2}},
{Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
{Spec: machine2Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
{Spec: machine2Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
2016-10-26 09:51:11 +00:00
},
},
{
/*
Node1 scores on 0-10 scale
CPU Score: ((10000 - 6000) *10) / 10000 = 4
Memory Score: ((20000 - 0) *10) / 20000 = 10
Node1 Score: (4 + 10) / 2 = 7
Node2 scores on 0-10 scale
CPU Score: ((10000 - 6000) *10) / 10000 = 4
Memory Score: ((20000 - 5000) *10) / 20000 = 7.5
Node2 Score: (4 + 7.5) / 2 = 5
*/
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
2016-10-26 09:51:11 +00:00
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 7}, {Host: "machine2", Score: 5}},
2018-05-10 11:12:20 +00:00
name: "no resources requested, pods scheduled with resources",
2016-11-18 20:52:35 +00:00
pods: []*v1.Pod{
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels2}},
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
{Spec: cpuOnly2, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
{Spec: cpuAndMemory, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
2016-10-26 09:51:11 +00:00
},
},
{
/*
Node1 scores on 0-10 scale
CPU Score: ((10000 - 6000) *10) / 10000 = 4
Memory Score: ((20000 - 5000) *10) / 20000 = 7.5
Node1 Score: (4 + 7.5) / 2 = 5
Node2 scores on 0-10 scale
CPU Score: ((10000 - 6000) *10) / 10000 = 4
Memory Score: ((20000 - 10000) *10) / 20000 = 5
Node2 Score: (4 + 5) / 2 = 4
*/
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
2016-10-26 09:51:11 +00:00
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 4}},
2018-05-10 11:12:20 +00:00
name: "resources requested, pods scheduled with resources",
2016-11-18 20:52:35 +00:00
pods: []*v1.Pod{
2016-10-26 09:51:11 +00:00
{Spec: cpuOnly},
{Spec: cpuAndMemory},
},
},
{
/*
Node1 scores on 0-10 scale
CPU Score: ((10000 - 6000) *10) / 10000 = 4
Memory Score: ((20000 - 5000) *10) / 20000 = 7.5
Node1 Score: (4 + 7.5) / 2 = 5
Node2 scores on 0-10 scale
CPU Score: ((10000 - 6000) *10) / 10000 = 4
Memory Score: ((50000 - 10000) *10) / 50000 = 8
Node2 Score: (4 + 8) / 2 = 6
*/
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 50000)},
2016-10-26 09:51:11 +00:00
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 6}},
2018-05-10 11:12:20 +00:00
name: "resources requested, pods scheduled with resources, differently sized machines",
2016-11-18 20:52:35 +00:00
pods: []*v1.Pod{
2016-10-26 09:51:11 +00:00
{Spec: cpuOnly},
{Spec: cpuAndMemory},
},
},
{
/*
Node1 scores on 0-10 scale
CPU Score: ((4000 - 6000) *10) / 4000 = 0
Memory Score: ((10000 - 0) *10) / 10000 = 10
Node1 Score: (0 + 10) / 2 = 5
Node2 scores on 0-10 scale
CPU Score: ((4000 - 6000) *10) / 4000 = 0
Memory Score: ((10000 - 5000) *10) / 10000 = 5
Node2 Score: (0 + 5) / 2 = 2
*/
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: cpuOnly},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
2016-10-26 09:51:11 +00:00
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 2}},
2018-05-10 11:12:20 +00:00
name: "requested resources exceed node capacity",
2016-11-18 20:52:35 +00:00
pods: []*v1.Pod{
2016-10-26 09:51:11 +00:00
{Spec: cpuOnly},
{Spec: cpuAndMemory},
},
},
{
2016-11-18 20:52:35 +00:00
pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 0, 0), makeNode("machine2", 0, 0)},
2016-10-26 09:51:11 +00:00
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}},
2018-05-10 11:12:20 +00:00
name: "zero node resources, pods scheduled with resources",
2016-11-18 20:52:35 +00:00
pods: []*v1.Pod{
2016-10-26 09:51:11 +00:00
{Spec: cpuOnly},
{Spec: cpuAndMemory},
},
},
}
for _, test := range tests {
2018-05-10 11:12:20 +00:00
t.Run(test.name, func(t *testing.T) {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes)
list, err := priorityFunction(LeastRequestedPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(test.expectedList, list) {
t.Errorf("expected %#v, got %#v", test.expectedList, list)
}
})
2016-10-26 09:51:11 +00:00
}
}