1/* 2Copyright 2015 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package qos 18 19import ( 20 v1 "k8s.io/api/core/v1" 21 v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos" 22 "k8s.io/kubernetes/pkg/kubelet/types" 23) 24 25const ( 26 // KubeletOOMScoreAdj is the OOM score adjustment for Kubelet 27 KubeletOOMScoreAdj int = -999 28 // KubeProxyOOMScoreAdj is the OOM score adjustment for kube-proxy 29 KubeProxyOOMScoreAdj int = -999 30 guaranteedOOMScoreAdj int = -997 31 besteffortOOMScoreAdj int = 1000 32) 33 34// GetContainerOOMScoreAdjust returns the amount by which the OOM score of all processes in the 35// container should be adjusted. 36// The OOM score of a process is the percentage of memory it consumes 37// multiplied by 10 (barring exceptional cases) + a configurable quantity which is between -1000 38// and 1000. Containers with higher OOM scores are killed if the system runs out of memory. 39// See https://lwn.net/Articles/391222/ for more information. 40func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapacity int64) int { 41 if types.IsNodeCriticalPod(pod) { 42 // Only node critical pod should be the last to get killed. 43 return guaranteedOOMScoreAdj 44 } 45 46 switch v1qos.GetPodQOS(pod) { 47 case v1.PodQOSGuaranteed: 48 // Guaranteed containers should be the last to get killed. 49 return guaranteedOOMScoreAdj 50 case v1.PodQOSBestEffort: 51 return besteffortOOMScoreAdj 52 } 53 54 // Burstable containers are a middle tier, between Guaranteed and Best-Effort. Ideally, 55 // we want to protect Burstable containers that consume less memory than requested. 56 // The formula below is a heuristic. A container requesting for 10% of a system's 57 // memory will have an OOM score adjust of 900. If a process in container Y 58 // uses over 10% of memory, its OOM score will be 1000. The idea is that containers 59 // which use more than their request will have an OOM score of 1000 and will be prime 60 // targets for OOM kills. 61 // Note that this is a heuristic, it won't work if a container has many small processes. 62 memoryRequest := container.Resources.Requests.Memory().Value() 63 oomScoreAdjust := 1000 - (1000*memoryRequest)/memoryCapacity 64 // A guaranteed pod using 100% of memory can have an OOM score of 10. Ensure 65 // that burstable pods have a higher OOM score adjustment. 66 if int(oomScoreAdjust) < (1000 + guaranteedOOMScoreAdj) { 67 return (1000 + guaranteedOOMScoreAdj) 68 } 69 // Give burstable pods a higher chance of survival over besteffort pods. 70 if int(oomScoreAdjust) == besteffortOOMScoreAdj { 71 return int(oomScoreAdjust - 1) 72 } 73 return int(oomScoreAdjust) 74} 75