|
| 1 | +/* |
| 2 | +Copyright 2020 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 priority |
| 18 | + |
| 19 | +import ( |
| 20 | + "math" |
| 21 | + |
| 22 | + apiv1 "k8s.io/api/core/v1" |
| 23 | + vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" |
| 24 | + "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/annotations" |
| 25 | + vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa" |
| 26 | + "k8s.io/klog" |
| 27 | +) |
| 28 | + |
| 29 | +// PriorityProcessor calculates priority for pod updates. |
| 30 | +type PriorityProcessor interface { |
| 31 | + GetUpdatePriority(pod *apiv1.Pod, vpa *vpa_types.VerticalPodAutoscaler, |
| 32 | + recommendation *vpa_types.RecommendedPodResources) podPriority |
| 33 | +} |
| 34 | + |
| 35 | +// NewProcessor creates a new default PriorityProcessor. |
| 36 | +func NewProcessor() PriorityProcessor { |
| 37 | + return &defaultPriorityProcessor{} |
| 38 | +} |
| 39 | + |
| 40 | +type defaultPriorityProcessor struct { |
| 41 | +} |
| 42 | + |
| 43 | +func (*defaultPriorityProcessor) GetUpdatePriority(pod *apiv1.Pod, _ *vpa_types.VerticalPodAutoscaler, |
| 44 | + recommendation *vpa_types.RecommendedPodResources) podPriority { |
| 45 | + outsideRecommendedRange := false |
| 46 | + scaleUp := false |
| 47 | + // Sum of requests over all containers, per resource type. |
| 48 | + totalRequestPerResource := make(map[apiv1.ResourceName]int64) |
| 49 | + // Sum of recommendations over all containers, per resource type. |
| 50 | + totalRecommendedPerResource := make(map[apiv1.ResourceName]int64) |
| 51 | + |
| 52 | + hasObservedContainers, vpaContainerSet := parseVpaObservedContainers(pod) |
| 53 | + |
| 54 | + for _, podContainer := range pod.Spec.Containers { |
| 55 | + if hasObservedContainers && !vpaContainerSet.Has(podContainer.Name) { |
| 56 | + klog.V(4).Infof("Not listed in %s:%s. Skipping container %s priority calculations", |
| 57 | + annotations.VpaObservedContainersLabel, pod.GetAnnotations()[annotations.VpaObservedContainersLabel], podContainer.Name) |
| 58 | + continue |
| 59 | + } |
| 60 | + recommendedRequest := vpa_api_util.GetRecommendationForContainer(podContainer.Name, recommendation) |
| 61 | + if recommendedRequest == nil { |
| 62 | + continue |
| 63 | + } |
| 64 | + for resourceName, recommended := range recommendedRequest.Target { |
| 65 | + totalRecommendedPerResource[resourceName] += recommended.MilliValue() |
| 66 | + lowerBound, hasLowerBound := recommendedRequest.LowerBound[resourceName] |
| 67 | + upperBound, hasUpperBound := recommendedRequest.UpperBound[resourceName] |
| 68 | + if request, hasRequest := podContainer.Resources.Requests[resourceName]; hasRequest { |
| 69 | + totalRequestPerResource[resourceName] += request.MilliValue() |
| 70 | + if recommended.MilliValue() > request.MilliValue() { |
| 71 | + scaleUp = true |
| 72 | + } |
| 73 | + if (hasLowerBound && request.Cmp(lowerBound) < 0) || |
| 74 | + (hasUpperBound && request.Cmp(upperBound) > 0) { |
| 75 | + outsideRecommendedRange = true |
| 76 | + } |
| 77 | + } else { |
| 78 | + // Note: if the request is not specified, the container will use the |
| 79 | + // namespace default request. Currently we ignore it and treat such |
| 80 | + // containers as if they had 0 request. A more correct approach would |
| 81 | + // be to always calculate the 'effective' request. |
| 82 | + scaleUp = true |
| 83 | + outsideRecommendedRange = true |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + resourceDiff := 0.0 |
| 88 | + for resource, totalRecommended := range totalRecommendedPerResource { |
| 89 | + totalRequest := math.Max(float64(totalRequestPerResource[resource]), 1.0) |
| 90 | + resourceDiff += math.Abs(totalRequest-float64(totalRecommended)) / totalRequest |
| 91 | + } |
| 92 | + return podPriority{ |
| 93 | + outsideRecommendedRange: outsideRecommendedRange, |
| 94 | + scaleUp: scaleUp, |
| 95 | + resourceDiff: resourceDiff, |
| 96 | + } |
| 97 | +} |
0 commit comments