123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- /*
- Copyright 2017 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"
- "k8s.io/api/core/v1"
- "k8s.io/apimachinery/pkg/api/resource"
- //metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
- schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
- )
- func TestResourceLimitsPriority(t *testing.T) {
- noResources := v1.PodSpec{
- Containers: []v1.Container{},
- }
- cpuOnly := v1.PodSpec{
- NodeName: "machine1",
- Containers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("1000m"),
- v1.ResourceMemory: resource.MustParse("0"),
- },
- },
- },
- {
- Resources: v1.ResourceRequirements{
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("2000m"),
- v1.ResourceMemory: resource.MustParse("0"),
- },
- },
- },
- },
- }
- memOnly := v1.PodSpec{
- NodeName: "machine2",
- Containers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("0"),
- v1.ResourceMemory: resource.MustParse("2000"),
- },
- },
- },
- {
- Resources: v1.ResourceRequirements{
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("0"),
- v1.ResourceMemory: resource.MustParse("3000"),
- },
- },
- },
- },
- }
- cpuAndMemory := v1.PodSpec{
- NodeName: "machine2",
- Containers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("1000m"),
- v1.ResourceMemory: resource.MustParse("2000"),
- },
- },
- },
- {
- Resources: v1.ResourceRequirements{
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("2000m"),
- v1.ResourceMemory: resource.MustParse("3000"),
- },
- },
- },
- },
- }
- tests := []struct {
- // input pod
- pod *v1.Pod
- nodes []*v1.Node
- expectedList schedulerapi.HostPriorityList
- name string
- }{
- {
- pod: &v1.Pod{Spec: noResources},
- nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 0), makeNode("machine3", 0, 10000), makeNode("machine4", 0, 0)},
- expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}, {Host: "machine4", Score: 0}},
- name: "pod does not specify its resource limits",
- },
- {
- pod: &v1.Pod{Spec: cpuOnly},
- nodes: []*v1.Node{makeNode("machine1", 3000, 10000), makeNode("machine2", 2000, 10000)},
- expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 0}},
- name: "pod only specifies cpu limits",
- },
- {
- pod: &v1.Pod{Spec: memOnly},
- nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
- expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 1}},
- name: "pod only specifies mem limits",
- },
- {
- pod: &v1.Pod{Spec: cpuAndMemory},
- nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
- expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 1}},
- name: "pod specifies both cpu and mem limits",
- },
- {
- pod: &v1.Pod{Spec: cpuAndMemory},
- nodes: []*v1.Node{makeNode("machine1", 0, 0)},
- expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}},
- name: "node does not advertise its allocatables",
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- nodeNameToInfo := schedulernodeinfo.CreateNodeNameToInfoMap(nil, test.nodes)
- metadata := &priorityMetadata{
- podLimits: getResourceLimits(test.pod),
- }
- for _, hasMeta := range []bool{true, false} {
- var function PriorityFunction
- if hasMeta {
- function = priorityFunction(ResourceLimitsPriorityMap, nil, metadata)
- } else {
- function = priorityFunction(ResourceLimitsPriorityMap, nil, nil)
- }
- list, err := function(test.pod, nodeNameToInfo, test.nodes)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if !reflect.DeepEqual(test.expectedList, list) {
- t.Errorf("hasMeta %#v expected %#v, got %#v", hasMeta, test.expectedList, list)
- }
- }
- })
- }
- }
|