k8s.io/kubernetes@v1.29.3/pkg/kubelet/kubelet_resources.go (about) 1 /* 2 Copyright 2016 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 kubelet 18 19 import ( 20 "fmt" 21 22 "k8s.io/klog/v2" 23 24 "k8s.io/api/core/v1" 25 "k8s.io/kubernetes/pkg/api/v1/resource" 26 ) 27 28 // defaultPodLimitsForDownwardAPI copies the input pod, and optional container, 29 // and applies default resource limits. it returns a copy of the input pod, 30 // and a copy of the input container (if specified) with default limits 31 // applied. if a container has no limit specified, it will default the limit to 32 // the node allocatable. 33 // TODO: if/when we have pod level resources, we need to update this function 34 // to use those limits instead of node allocatable. 35 func (kl *Kubelet) defaultPodLimitsForDownwardAPI(pod *v1.Pod, container *v1.Container) (*v1.Pod, *v1.Container, error) { 36 if pod == nil { 37 return nil, nil, fmt.Errorf("invalid input, pod cannot be nil") 38 } 39 40 node, err := kl.getNodeAnyWay() 41 if err != nil { 42 return nil, nil, fmt.Errorf("failed to find node object, expected a node") 43 } 44 allocatable := node.Status.Allocatable 45 klog.InfoS("Allocatable", "allocatable", allocatable) 46 outputPod := pod.DeepCopy() 47 for idx := range outputPod.Spec.Containers { 48 resource.MergeContainerResourceLimits(&outputPod.Spec.Containers[idx], allocatable) 49 } 50 51 var outputContainer *v1.Container 52 if container != nil { 53 outputContainer = container.DeepCopy() 54 resource.MergeContainerResourceLimits(outputContainer, allocatable) 55 } 56 return outputPod, outputContainer, nil 57 }