resource_limits_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package priorities
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. //metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  21. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  22. )
  23. func TestResourceLimitsPriority(t *testing.T) {
  24. noResources := v1.PodSpec{
  25. Containers: []v1.Container{},
  26. }
  27. cpuOnly := v1.PodSpec{
  28. NodeName: "machine1",
  29. Containers: []v1.Container{
  30. {
  31. Resources: v1.ResourceRequirements{
  32. Limits: v1.ResourceList{
  33. v1.ResourceCPU: resource.MustParse("1000m"),
  34. v1.ResourceMemory: resource.MustParse("0"),
  35. },
  36. },
  37. },
  38. {
  39. Resources: v1.ResourceRequirements{
  40. Limits: v1.ResourceList{
  41. v1.ResourceCPU: resource.MustParse("2000m"),
  42. v1.ResourceMemory: resource.MustParse("0"),
  43. },
  44. },
  45. },
  46. },
  47. }
  48. memOnly := v1.PodSpec{
  49. NodeName: "machine2",
  50. Containers: []v1.Container{
  51. {
  52. Resources: v1.ResourceRequirements{
  53. Limits: v1.ResourceList{
  54. v1.ResourceCPU: resource.MustParse("0"),
  55. v1.ResourceMemory: resource.MustParse("2000"),
  56. },
  57. },
  58. },
  59. {
  60. Resources: v1.ResourceRequirements{
  61. Limits: v1.ResourceList{
  62. v1.ResourceCPU: resource.MustParse("0"),
  63. v1.ResourceMemory: resource.MustParse("3000"),
  64. },
  65. },
  66. },
  67. },
  68. }
  69. cpuAndMemory := v1.PodSpec{
  70. NodeName: "machine2",
  71. Containers: []v1.Container{
  72. {
  73. Resources: v1.ResourceRequirements{
  74. Limits: v1.ResourceList{
  75. v1.ResourceCPU: resource.MustParse("1000m"),
  76. v1.ResourceMemory: resource.MustParse("2000"),
  77. },
  78. },
  79. },
  80. {
  81. Resources: v1.ResourceRequirements{
  82. Limits: v1.ResourceList{
  83. v1.ResourceCPU: resource.MustParse("2000m"),
  84. v1.ResourceMemory: resource.MustParse("3000"),
  85. },
  86. },
  87. },
  88. },
  89. }
  90. tests := []struct {
  91. // input pod
  92. pod *v1.Pod
  93. nodes []*v1.Node
  94. expectedList schedulerapi.HostPriorityList
  95. name string
  96. }{
  97. {
  98. pod: &v1.Pod{Spec: noResources},
  99. nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 0), makeNode("machine3", 0, 10000), makeNode("machine4", 0, 0)},
  100. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}, {Host: "machine4", Score: 0}},
  101. name: "pod does not specify its resource limits",
  102. },
  103. {
  104. pod: &v1.Pod{Spec: cpuOnly},
  105. nodes: []*v1.Node{makeNode("machine1", 3000, 10000), makeNode("machine2", 2000, 10000)},
  106. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 0}},
  107. name: "pod only specifies cpu limits",
  108. },
  109. {
  110. pod: &v1.Pod{Spec: memOnly},
  111. nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
  112. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 1}},
  113. name: "pod only specifies mem limits",
  114. },
  115. {
  116. pod: &v1.Pod{Spec: cpuAndMemory},
  117. nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
  118. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 1}},
  119. name: "pod specifies both cpu and mem limits",
  120. },
  121. {
  122. pod: &v1.Pod{Spec: cpuAndMemory},
  123. nodes: []*v1.Node{makeNode("machine1", 0, 0)},
  124. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}},
  125. name: "node does not advertise its allocatables",
  126. },
  127. }
  128. for _, test := range tests {
  129. t.Run(test.name, func(t *testing.T) {
  130. nodeNameToInfo := schedulernodeinfo.CreateNodeNameToInfoMap(nil, test.nodes)
  131. metadata := &priorityMetadata{
  132. podLimits: getResourceLimits(test.pod),
  133. }
  134. for _, hasMeta := range []bool{true, false} {
  135. var function PriorityFunction
  136. if hasMeta {
  137. function = priorityFunction(ResourceLimitsPriorityMap, nil, metadata)
  138. } else {
  139. function = priorityFunction(ResourceLimitsPriorityMap, nil, nil)
  140. }
  141. list, err := function(test.pod, nodeNameToInfo, test.nodes)
  142. if err != nil {
  143. t.Errorf("unexpected error: %v", err)
  144. }
  145. if !reflect.DeepEqual(test.expectedList, list) {
  146. t.Errorf("hasMeta %#v expected %#v, got %#v", hasMeta, test.expectedList, list)
  147. }
  148. }
  149. })
  150. }
  151. }