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 v1 "k8s.io/api/core/v1" 21 utilfeature "k8s.io/apiserver/pkg/util/feature" 22 podutil "k8s.io/kubernetes/pkg/api/v1/pod" 23 v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos" 24 "k8s.io/kubernetes/pkg/features" 25 "k8s.io/kubernetes/pkg/kubelet/types" 26 ) 27 28 const ( 29 // KubeletOOMScoreAdj is the OOM score adjustment for Kubelet 30 KubeletOOMScoreAdj int = -999 31 // KubeProxyOOMScoreAdj is the OOM score adjustment for kube-proxy 32 KubeProxyOOMScoreAdj int = -999 33 guaranteedOOMScoreAdj int = -997 34 besteffortOOMScoreAdj int = 1000 35 ) 36 37 // GetContainerOOMScoreAdjust returns the amount by which the OOM score of all processes in the 38 // container should be adjusted. 39 // The OOM score of a process is the percentage of memory it consumes 40 // multiplied by 10 (barring exceptional cases) + a configurable quantity which is between -1000 41 // and 1000. Containers with higher OOM scores are killed if the system runs out of memory. 42 // See https://lwn.net/Articles/391222/ for more information. 43 func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapacity int64) int { 44 if types.IsNodeCriticalPod(pod) { 45 // Only node critical pod should be the last to get killed. 46 return guaranteedOOMScoreAdj 47 } 48 49 switch v1qos.GetPodQOS(pod) { 50 case v1.PodQOSGuaranteed: 51 // Guaranteed containers should be the last to get killed. 52 return guaranteedOOMScoreAdj 53 case v1.PodQOSBestEffort: 54 return besteffortOOMScoreAdj 55 } 56 57 // Burstable containers are a middle tier, between Guaranteed and Best-Effort. Ideally, 58 // we want to protect Burstable containers that consume less memory than requested. 59 // The formula below is a heuristic. A container requesting for 10% of a system's 60 // memory will have an OOM score adjust of 900. If a process in container Y 61 // uses over 10% of memory, its OOM score will be 1000. The idea is that containers 62 // which use more than their request will have an OOM score of 1000 and will be prime 63 // targets for OOM kills. 64 // Note that this is a heuristic, it won't work if a container has many small processes. 65 memoryRequest := container.Resources.Requests.Memory().Value() 66 if utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) { 67 if cs, ok := podutil.GetContainerStatus(pod.Status.ContainerStatuses, container.Name); ok { 68 memoryRequest = cs.AllocatedResources.Memory().Value() 69 } 70 } 71 oomScoreAdjust := 1000 - (1000*memoryRequest)/memoryCapacity 72 // A guaranteed pod using 100% of memory can have an OOM score of 10. Ensure 73 // that burstable pods have a higher OOM score adjustment. 74 if int(oomScoreAdjust) < (1000 + guaranteedOOMScoreAdj) { 75 return (1000 + guaranteedOOMScoreAdj) 76 } 77 // Give burstable pods a higher chance of survival over besteffort pods. 78 if int(oomScoreAdjust) == besteffortOOMScoreAdj { 79 return int(oomScoreAdjust - 1) 80 } 81 return int(oomScoreAdjust) 82 } 83