k8s.io/kubernetes@v1.29.3/pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2021 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package kuberuntime 21 22 import ( 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/resource" 25 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" 26 27 resourcehelper "k8s.io/kubernetes/pkg/api/v1/resource" 28 ) 29 30 func (m *kubeGenericRuntimeManager) convertOverheadToLinuxResources(pod *v1.Pod) *runtimeapi.LinuxContainerResources { 31 resources := &runtimeapi.LinuxContainerResources{} 32 if pod.Spec.Overhead != nil { 33 cpu := pod.Spec.Overhead.Cpu() 34 memory := pod.Spec.Overhead.Memory() 35 36 // For overhead, we do not differentiate between requests and limits. Treat this overhead 37 // as "guaranteed", with requests == limits 38 resources = m.calculateLinuxResources(cpu, cpu, memory) 39 } 40 41 return resources 42 } 43 44 func (m *kubeGenericRuntimeManager) calculateSandboxResources(pod *v1.Pod) *runtimeapi.LinuxContainerResources { 45 opts := resourcehelper.PodResourcesOptions{ 46 ExcludeOverhead: true, 47 } 48 req := resourcehelper.PodRequests(pod, opts) 49 lim := resourcehelper.PodLimits(pod, opts) 50 var cpuRequest *resource.Quantity 51 if _, cpuRequestExists := req[v1.ResourceCPU]; cpuRequestExists { 52 cpuRequest = req.Cpu() 53 } 54 return m.calculateLinuxResources(cpuRequest, lim.Cpu(), lim.Memory()) 55 } 56 57 func (m *kubeGenericRuntimeManager) applySandboxResources(pod *v1.Pod, config *runtimeapi.PodSandboxConfig) error { 58 59 if config.Linux == nil { 60 return nil 61 } 62 config.Linux.Resources = m.calculateSandboxResources(pod) 63 config.Linux.Overhead = m.convertOverheadToLinuxResources(pod) 64 65 return nil 66 }