k8s.io/kubernetes@v1.29.3/pkg/kubelet/qos/policy_test.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package qos 18 19 import ( 20 "strconv" 21 "testing" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/resource" 25 "k8s.io/kubernetes/pkg/apis/scheduling" 26 ) 27 28 const ( 29 standardMemoryAmount = 8000000000 30 ) 31 32 var ( 33 cpuLimit = v1.Pod{ 34 Spec: v1.PodSpec{ 35 Containers: []v1.Container{ 36 { 37 Resources: v1.ResourceRequirements{ 38 Limits: v1.ResourceList{ 39 v1.ResourceName(v1.ResourceCPU): resource.MustParse("10"), 40 }, 41 }, 42 }, 43 }, 44 }, 45 } 46 47 memoryLimitCPURequest = v1.Pod{ 48 Spec: v1.PodSpec{ 49 Containers: []v1.Container{ 50 { 51 Resources: v1.ResourceRequirements{ 52 Requests: v1.ResourceList{ 53 v1.ResourceName(v1.ResourceCPU): resource.MustParse("0"), 54 }, 55 Limits: v1.ResourceList{ 56 v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"), 57 }, 58 }, 59 }, 60 }, 61 }, 62 } 63 64 zeroMemoryLimit = v1.Pod{ 65 Spec: v1.PodSpec{ 66 Containers: []v1.Container{ 67 { 68 Resources: v1.ResourceRequirements{ 69 Limits: v1.ResourceList{ 70 v1.ResourceName(v1.ResourceMemory): resource.MustParse("0"), 71 }, 72 }, 73 }, 74 }, 75 }, 76 } 77 78 noRequestLimit = v1.Pod{ 79 Spec: v1.PodSpec{ 80 Containers: []v1.Container{ 81 { 82 Resources: v1.ResourceRequirements{}, 83 }, 84 }, 85 }, 86 } 87 88 equalRequestLimitCPUMemory = v1.Pod{ 89 Spec: v1.PodSpec{ 90 Containers: []v1.Container{ 91 { 92 Resources: v1.ResourceRequirements{ 93 Requests: v1.ResourceList{ 94 v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"), 95 v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"), 96 }, 97 Limits: v1.ResourceList{ 98 v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"), 99 v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"), 100 }, 101 }, 102 }, 103 }, 104 }, 105 } 106 107 cpuUnlimitedMemoryLimitedWithRequests = v1.Pod{ 108 Spec: v1.PodSpec{ 109 Containers: []v1.Container{ 110 { 111 Resources: v1.ResourceRequirements{ 112 Requests: v1.ResourceList{ 113 v1.ResourceName(v1.ResourceMemory): resource.MustParse(strconv.FormatInt(standardMemoryAmount/2, 10)), 114 v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"), 115 }, 116 Limits: v1.ResourceList{ 117 v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"), 118 }, 119 }, 120 }, 121 }, 122 }, 123 } 124 125 requestNoLimit = v1.Pod{ 126 Spec: v1.PodSpec{ 127 Containers: []v1.Container{ 128 { 129 Resources: v1.ResourceRequirements{ 130 Requests: v1.ResourceList{ 131 v1.ResourceName(v1.ResourceMemory): resource.MustParse(strconv.FormatInt(standardMemoryAmount-1, 10)), 132 v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"), 133 }, 134 }, 135 }, 136 }, 137 }, 138 } 139 140 systemCritical = scheduling.SystemCriticalPriority 141 142 clusterCritical = v1.Pod{ 143 Spec: v1.PodSpec{ 144 PriorityClassName: scheduling.SystemClusterCritical, 145 Priority: &systemCritical, 146 Containers: []v1.Container{ 147 { 148 Resources: v1.ResourceRequirements{}, 149 }, 150 }, 151 }, 152 } 153 154 systemNodeCritical = scheduling.SystemCriticalPriority + 1000 155 156 nodeCritical = v1.Pod{ 157 Spec: v1.PodSpec{ 158 PriorityClassName: scheduling.SystemNodeCritical, 159 Priority: &systemNodeCritical, 160 Containers: []v1.Container{ 161 { 162 Resources: v1.ResourceRequirements{}, 163 }, 164 }, 165 }, 166 } 167 ) 168 169 type oomTest struct { 170 pod *v1.Pod 171 memoryCapacity int64 172 lowOOMScoreAdj int // The max oom_score_adj score the container should be assigned. 173 highOOMScoreAdj int // The min oom_score_adj score the container should be assigned. 174 } 175 176 func TestGetContainerOOMScoreAdjust(t *testing.T) { 177 oomTests := []oomTest{ 178 { 179 pod: &cpuLimit, 180 memoryCapacity: 4000000000, 181 lowOOMScoreAdj: 999, 182 highOOMScoreAdj: 999, 183 }, 184 { 185 pod: &memoryLimitCPURequest, 186 memoryCapacity: 8000000000, 187 lowOOMScoreAdj: 999, 188 highOOMScoreAdj: 999, 189 }, 190 { 191 pod: &zeroMemoryLimit, 192 memoryCapacity: 7230457451, 193 lowOOMScoreAdj: 1000, 194 highOOMScoreAdj: 1000, 195 }, 196 { 197 pod: &noRequestLimit, 198 memoryCapacity: 4000000000, 199 lowOOMScoreAdj: 1000, 200 highOOMScoreAdj: 1000, 201 }, 202 { 203 pod: &equalRequestLimitCPUMemory, 204 memoryCapacity: 123456789, 205 lowOOMScoreAdj: -997, 206 highOOMScoreAdj: -997, 207 }, 208 { 209 pod: &cpuUnlimitedMemoryLimitedWithRequests, 210 memoryCapacity: standardMemoryAmount, 211 lowOOMScoreAdj: 495, 212 highOOMScoreAdj: 505, 213 }, 214 { 215 pod: &requestNoLimit, 216 memoryCapacity: standardMemoryAmount, 217 lowOOMScoreAdj: 3, 218 highOOMScoreAdj: 3, 219 }, 220 { 221 pod: &clusterCritical, 222 memoryCapacity: 4000000000, 223 lowOOMScoreAdj: 1000, 224 highOOMScoreAdj: 1000, 225 }, 226 { 227 pod: &nodeCritical, 228 memoryCapacity: 4000000000, 229 lowOOMScoreAdj: -997, 230 highOOMScoreAdj: -997, 231 }, 232 } 233 for _, test := range oomTests { 234 oomScoreAdj := GetContainerOOMScoreAdjust(test.pod, &test.pod.Spec.Containers[0], test.memoryCapacity) 235 if oomScoreAdj < test.lowOOMScoreAdj || oomScoreAdj > test.highOOMScoreAdj { 236 t.Errorf("oom_score_adj should be between %d and %d, but was %d", test.lowOOMScoreAdj, test.highOOMScoreAdj, oomScoreAdj) 237 } 238 } 239 }