|
| 1 | +/* |
| 2 | +Copyright 2024 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 eviction |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + core "k8s.io/api/core/v1" |
| 23 | + "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource/pod/recommendation" |
| 24 | + "k8s.io/klog/v2" |
| 25 | + |
| 26 | + vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" |
| 27 | + "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/limitrange" |
| 28 | + vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa" |
| 29 | +) |
| 30 | + |
| 31 | +type inPlaceRecommendationProvider struct { |
| 32 | + limitsRangeCalculator limitrange.LimitRangeCalculator |
| 33 | + recommendationProcessor vpa_api_util.RecommendationProcessor |
| 34 | +} |
| 35 | + |
| 36 | +// NewProvider constructs the recommendation provider that can be used to determine recommendations for pods. |
| 37 | +func NewInPlaceProvider(calculator limitrange.LimitRangeCalculator, |
| 38 | + recommendationProcessor vpa_api_util.RecommendationProcessor) recommendation.Provider { |
| 39 | + return &inPlaceRecommendationProvider{ |
| 40 | + limitsRangeCalculator: calculator, |
| 41 | + recommendationProcessor: recommendationProcessor, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TODO(maxcao13): Strip down function to remove unnecessary stuff, or refactor so that it can be used in the admission controller as well |
| 46 | +// GetContainersResources returns the recommended resources for each container in the given pod in the same order they are specified in the pod.Spec. |
| 47 | +// If addAll is set to true, containers w/o a recommendation are also added to the list (and their non-recommended requests and limits will always be preserved if present), |
| 48 | +// otherwise they're skipped (default behaviour). |
| 49 | +func GetContainersResources(pod *core.Pod, vpaResourcePolicy *vpa_types.PodResourcePolicy, podRecommendation vpa_types.RecommendedPodResources, limitRange *core.LimitRangeItem, |
| 50 | + addAll bool) []vpa_api_util.ContainerResources { |
| 51 | + resources := make([]vpa_api_util.ContainerResources, len(pod.Spec.Containers)) |
| 52 | + for i, container := range pod.Spec.Containers { |
| 53 | + recommendation := vpa_api_util.GetRecommendationForContainer(container.Name, &podRecommendation) |
| 54 | + if recommendation == nil { |
| 55 | + if !addAll { |
| 56 | + klog.V(2).InfoS("No recommendation found for container, skipping", "container", container.Name) |
| 57 | + continue |
| 58 | + } |
| 59 | + klog.V(2).InfoS("No match found for container, using Pod request", "container", container.Name) |
| 60 | + resources[i].Requests = container.Resources.Requests |
| 61 | + } else { |
| 62 | + resources[i].Requests = recommendation.Target |
| 63 | + } |
| 64 | + defaultLimit := core.ResourceList{} |
| 65 | + if limitRange != nil { |
| 66 | + defaultLimit = limitRange.Default |
| 67 | + } |
| 68 | + containerControlledValues := vpa_api_util.GetContainerControlledValues(container.Name, vpaResourcePolicy) |
| 69 | + if containerControlledValues == vpa_types.ContainerControlledValuesRequestsAndLimits { |
| 70 | + proportionalLimits, _ := vpa_api_util.GetProportionalLimit(container.Resources.Limits, container.Resources.Requests, resources[i].Requests, defaultLimit) |
| 71 | + if proportionalLimits != nil { |
| 72 | + resources[i].Limits = proportionalLimits |
| 73 | + } |
| 74 | + } |
| 75 | + // If the recommendation only contains CPU or Memory (if the VPA was configured this way), we need to make sure we "backfill" the other. |
| 76 | + // Only do this when the addAll flag is true. |
| 77 | + if addAll { |
| 78 | + cpuRequest, hasCpuRequest := container.Resources.Requests[core.ResourceCPU] |
| 79 | + if _, ok := resources[i].Requests[core.ResourceCPU]; !ok && hasCpuRequest { |
| 80 | + resources[i].Requests[core.ResourceCPU] = cpuRequest |
| 81 | + } |
| 82 | + memRequest, hasMemRequest := container.Resources.Requests[core.ResourceMemory] |
| 83 | + if _, ok := resources[i].Requests[core.ResourceMemory]; !ok && hasMemRequest { |
| 84 | + resources[i].Requests[core.ResourceMemory] = memRequest |
| 85 | + } |
| 86 | + cpuLimit, hasCpuLimit := container.Resources.Limits[core.ResourceCPU] |
| 87 | + if _, ok := resources[i].Limits[core.ResourceCPU]; !ok && hasCpuLimit { |
| 88 | + resources[i].Limits[core.ResourceCPU] = cpuLimit |
| 89 | + } |
| 90 | + memLimit, hasMemLimit := container.Resources.Limits[core.ResourceMemory] |
| 91 | + if _, ok := resources[i].Limits[core.ResourceMemory]; !ok && hasMemLimit { |
| 92 | + resources[i].Limits[core.ResourceMemory] = memLimit |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return resources |
| 97 | +} |
| 98 | + |
| 99 | +// GetContainersResourcesForPod returns recommended request for a given pod and associated annotations. |
| 100 | +// The returned slice corresponds 1-1 to containers in the Pod. |
| 101 | +func (p *inPlaceRecommendationProvider) GetContainersResourcesForPod(pod *core.Pod, vpa *vpa_types.VerticalPodAutoscaler) ([]vpa_api_util.ContainerResources, vpa_api_util.ContainerToAnnotationsMap, error) { |
| 102 | + if vpa == nil || pod == nil { |
| 103 | + klog.V(2).InfoS("Can't calculate recommendations, one of VPA or Pod is nil", "vpa", vpa, "pod", pod) |
| 104 | + return nil, nil, nil |
| 105 | + } |
| 106 | + klog.V(2).InfoS("Updating requirements for pod", "pod", pod.Name) |
| 107 | + |
| 108 | + recommendedPodResources := &vpa_types.RecommendedPodResources{} |
| 109 | + |
| 110 | + if vpa.Status.Recommendation != nil { |
| 111 | + var err error |
| 112 | + // ignore annotations as they are not used in the in-place update |
| 113 | + recommendedPodResources, _, err = p.recommendationProcessor.Apply(vpa, pod) |
| 114 | + if err != nil { |
| 115 | + klog.V(2).InfoS("Cannot process recommendation for pod", "pod", klog.KObj(pod)) |
| 116 | + return nil, nil, err |
| 117 | + } |
| 118 | + } |
| 119 | + containerLimitRange, err := p.limitsRangeCalculator.GetContainerLimitRangeItem(pod.Namespace) |
| 120 | + if err != nil { |
| 121 | + return nil, nil, fmt.Errorf("error getting containerLimitRange: %s", err) |
| 122 | + } |
| 123 | + var resourcePolicy *vpa_types.PodResourcePolicy |
| 124 | + if vpa.Spec.UpdatePolicy == nil || vpa.Spec.UpdatePolicy.UpdateMode == nil || *vpa.Spec.UpdatePolicy.UpdateMode != vpa_types.UpdateModeOff { |
| 125 | + resourcePolicy = vpa.Spec.ResourcePolicy |
| 126 | + } |
| 127 | + containerResources := GetContainersResources(pod, resourcePolicy, *recommendedPodResources, containerLimitRange, false) |
| 128 | + |
| 129 | + // Ensure that we are not propagating empty resource key if any. |
| 130 | + for _, resource := range containerResources { |
| 131 | + if resource.RemoveEmptyResourceKeyIfAny() { |
| 132 | + klog.InfoS("An empty resource key was found and purged", "pod", klog.KObj(pod), "vpa", klog.KObj(vpa)) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return containerResources, nil, nil |
| 137 | +} |
0 commit comments