-
Notifications
You must be signed in to change notification settings - Fork 0
/
capping.go
397 lines (348 loc) · 16.7 KB
/
capping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"fmt"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/limitrange"
"k8s.io/klog"
)
// NewCappingRecommendationProcessor constructs new RecommendationsProcessor that adjusts recommendation
// for given pod to obey VPA resources policy and container limits
func NewCappingRecommendationProcessor(limitsRangeCalculator limitrange.LimitRangeCalculator) RecommendationProcessor {
return &cappingRecommendationProcessor{limitsRangeCalculator: limitsRangeCalculator}
}
type cappingAction string
var (
cappedToMinAllowed cappingAction = "capped to minAllowed"
cappedToMaxAllowed cappingAction = "capped to maxAllowed"
cappedProportionallyToMaxLimit cappingAction = "capped to fit Max in container LimitRange"
cappedProportionallyToMinLimit cappingAction = "capped to fit Min in container LimitRange"
)
func toCappingAnnotation(resourceName apiv1.ResourceName, action cappingAction) string {
return fmt.Sprintf("%s %s", resourceName, action)
}
type cappingRecommendationProcessor struct {
limitsRangeCalculator limitrange.LimitRangeCalculator
}
// Apply returns a recommendation for the given pod, adjusted to obey policy and limits.
func (c *cappingRecommendationProcessor) Apply(
podRecommendation *vpa_types.RecommendedPodResources,
policy *vpa_types.PodResourcePolicy,
conditions []vpa_types.VerticalPodAutoscalerCondition,
pod *apiv1.Pod) (*vpa_types.RecommendedPodResources, ContainerToAnnotationsMap, error) {
// TODO: Annotate if request enforced by maintaining proportion with limit and allowed limit range is in conflict with policy.
if podRecommendation == nil && policy == nil {
// If there is no recommendation and no policies have been defined then no recommendation can be computed.
return nil, nil, nil
}
if podRecommendation == nil {
// Policies have been specified. Create an empty recommendation so that the policies can be applied correctly.
podRecommendation = new(vpa_types.RecommendedPodResources)
}
updatedRecommendations := []vpa_types.RecommendedContainerResources{}
containerToAnnotationsMap := ContainerToAnnotationsMap{}
limitAdjustedRecommendation, err := c.capProportionallyToPodLimitRange(podRecommendation.ContainerRecommendations, pod)
if err != nil {
return nil, nil, err
}
for _, containerRecommendation := range limitAdjustedRecommendation {
container := getContainer(containerRecommendation.ContainerName, pod)
if container == nil {
klog.V(2).Infof("no matching Container found for recommendation %s", containerRecommendation.ContainerName)
continue
}
containerLimitRange, err := c.limitsRangeCalculator.GetContainerLimitRangeItem(pod.Namespace)
if err != nil {
klog.Warningf("failed to fetch LimitRange for %v namespace", pod.Namespace)
}
updatedContainerResources, containerAnnotations, err := getCappedRecommendationForContainer(
*container, &containerRecommendation, policy, containerLimitRange)
if len(containerAnnotations) != 0 {
containerToAnnotationsMap[containerRecommendation.ContainerName] = containerAnnotations
}
if err != nil {
return nil, nil, fmt.Errorf("cannot update recommendation for container name %v", container.Name)
}
updatedRecommendations = append(updatedRecommendations, *updatedContainerResources)
}
return &vpa_types.RecommendedPodResources{ContainerRecommendations: updatedRecommendations}, containerToAnnotationsMap, nil
}
// getCappedRecommendationForContainer returns a recommendation for the given container, adjusted to obey policy and limits.
func getCappedRecommendationForContainer(
container apiv1.Container,
containerRecommendation *vpa_types.RecommendedContainerResources,
policy *vpa_types.PodResourcePolicy, limitRange *apiv1.LimitRangeItem) (*vpa_types.RecommendedContainerResources, []string, error) {
if containerRecommendation == nil {
return nil, nil, fmt.Errorf("no recommendation available for container name %v", container.Name)
}
// containerPolicy can be nil (user does not have to configure it).
containerPolicy := GetContainerResourcePolicy(container.Name, policy)
cappedRecommendations := containerRecommendation.DeepCopy()
cappingAnnotations := make([]string, 0)
process := func(recommendation apiv1.ResourceList, genAnnotations bool) {
// TODO: Add anotation if limitRange is conflicting with VPA policy.
limitAnnotations := applyContainerLimitRange(recommendation, container, limitRange)
annotations := applyVPAPolicy(recommendation, containerPolicy)
if genAnnotations {
cappingAnnotations = append(cappingAnnotations, limitAnnotations...)
cappingAnnotations = append(cappingAnnotations, annotations...)
}
}
process(cappedRecommendations.Target, true)
process(cappedRecommendations.LowerBound, false)
process(cappedRecommendations.UpperBound, false)
return cappedRecommendations, cappingAnnotations, nil
}
// applyVPAPolicy updates recommendation if recommended resources are outside of limits defined in VPA resources policy
func applyVPAPolicy(recommendation apiv1.ResourceList, policy *vpa_types.ContainerResourcePolicy) []string {
if policy == nil {
return nil
}
annotations := make([]string, 0)
for resourceName, recommended := range recommendation {
cappedToMin, isCapped := maybeCapToPolicyMin(recommended, resourceName, policy)
recommendation[resourceName] = cappedToMin
if isCapped {
annotations = append(annotations, toCappingAnnotation(resourceName, cappedToMinAllowed))
}
cappedToMax, isCapped := maybeCapToPolicyMax(cappedToMin, resourceName, policy)
recommendation[resourceName] = cappedToMax
if isCapped {
annotations = append(annotations, toCappingAnnotation(resourceName, cappedToMaxAllowed))
}
}
return annotations
}
func applyVPAPolicyForContainer(containerName string,
containerRecommendation *vpa_types.RecommendedContainerResources,
policy *vpa_types.PodResourcePolicy) (*vpa_types.RecommendedContainerResources, error) {
if containerRecommendation == nil {
return nil, fmt.Errorf("no recommendation available for container name %v", containerName)
}
cappedRecommendations := containerRecommendation.DeepCopy()
// containerPolicy can be nil (user does not have to configure it).
containerPolicy := GetContainerResourcePolicy(containerName, policy)
if containerPolicy == nil {
return cappedRecommendations, nil
}
process := func(recommendation apiv1.ResourceList) {
for resourceName, recommended := range recommendation {
cappedToMin, _ := maybeCapToPolicyMin(recommended, resourceName, containerPolicy)
recommendation[resourceName] = cappedToMin
cappedToMax, _ := maybeCapToPolicyMax(cappedToMin, resourceName, containerPolicy)
recommendation[resourceName] = cappedToMax
}
}
process(cappedRecommendations.Target)
process(cappedRecommendations.LowerBound)
process(cappedRecommendations.UpperBound)
return cappedRecommendations, nil
}
func maybeCapToPolicyMin(recommended resource.Quantity, resourceName apiv1.ResourceName,
containerPolicy *vpa_types.ContainerResourcePolicy) (resource.Quantity, bool) {
return maybeCapToMin(recommended, resourceName, containerPolicy.MinAllowed)
}
func maybeCapToPolicyMax(recommended resource.Quantity, resourceName apiv1.ResourceName,
containerPolicy *vpa_types.ContainerResourcePolicy) (resource.Quantity, bool) {
return maybeCapToMax(recommended, resourceName, containerPolicy.MaxAllowed)
}
func maybeCapToMax(recommended resource.Quantity, resourceName apiv1.ResourceName,
max apiv1.ResourceList) (resource.Quantity, bool) {
maxResource, found := max[resourceName]
if found && !maxResource.IsZero() && recommended.Cmp(maxResource) > 0 {
return maxResource, true
}
return recommended, false
}
func maybeCapToMin(recommended resource.Quantity, resourceName apiv1.ResourceName,
min apiv1.ResourceList) (resource.Quantity, bool) {
minResource, found := min[resourceName]
if found && !minResource.IsZero() && recommended.Cmp(minResource) < 0 {
return minResource, true
}
return recommended, false
}
// ApplyVPAPolicy returns a recommendation, adjusted to obey policy.
func ApplyVPAPolicy(podRecommendation *vpa_types.RecommendedPodResources,
policy *vpa_types.PodResourcePolicy) (*vpa_types.RecommendedPodResources, error) {
if podRecommendation == nil {
return nil, nil
}
if policy == nil {
return podRecommendation, nil
}
updatedRecommendations := []vpa_types.RecommendedContainerResources{}
for _, containerRecommendation := range podRecommendation.ContainerRecommendations {
containerName := containerRecommendation.ContainerName
updatedContainerResources, err := applyVPAPolicyForContainer(containerName,
&containerRecommendation, policy)
if err != nil {
return nil, fmt.Errorf("cannot apply policy on recommendation for container name %v", containerName)
}
updatedRecommendations = append(updatedRecommendations, *updatedContainerResources)
}
return &vpa_types.RecommendedPodResources{ContainerRecommendations: updatedRecommendations}, nil
}
// GetRecommendationForContainer returns recommendation for given container name
func GetRecommendationForContainer(containerName string, recommendation *vpa_types.RecommendedPodResources) *vpa_types.RecommendedContainerResources {
if recommendation != nil {
for i, containerRec := range recommendation.ContainerRecommendations {
if containerRec.ContainerName == containerName {
recommendationCopy := recommendation.ContainerRecommendations[i]
return &recommendationCopy
}
}
}
return nil
}
func getContainer(containerName string, pod *apiv1.Pod) *apiv1.Container {
for i, container := range pod.Spec.Containers {
if container.Name == containerName {
return &pod.Spec.Containers[i]
}
}
return nil
}
// applyContainerLimitRange updates recommendation if recommended resources are outside of limits defined in VPA resources policy
func applyContainerLimitRange(recommendation apiv1.ResourceList, container apiv1.Container, limitRange *apiv1.LimitRangeItem) []string {
annotations := make([]string, 0)
if limitRange == nil {
return annotations
}
maxAllowedRecommendation := getMaxAllowedRecommendation(recommendation, container, limitRange)
minAllowedRecommendation := getMinAllowedRecommendation(recommendation, container, limitRange)
for resourceName, recommended := range recommendation {
cappedToMin, isCapped := maybeCapToMin(recommended, resourceName, minAllowedRecommendation)
recommendation[resourceName] = cappedToMin
if isCapped {
annotations = append(annotations, toCappingAnnotation(resourceName, cappedProportionallyToMinLimit))
}
cappedToMax, isCapped := maybeCapToMax(cappedToMin, resourceName, maxAllowedRecommendation)
recommendation[resourceName] = cappedToMax
if isCapped {
annotations = append(annotations, toCappingAnnotation(resourceName, cappedProportionallyToMaxLimit))
}
}
return annotations
}
func getMaxAllowedRecommendation(recommendation apiv1.ResourceList, container apiv1.Container,
podLimitRange *apiv1.LimitRangeItem) apiv1.ResourceList {
if podLimitRange == nil {
return apiv1.ResourceList{}
}
return getBoundaryRecommendation(recommendation, container, podLimitRange.Max, podLimitRange.Default)
}
func getMinAllowedRecommendation(recommendation apiv1.ResourceList, container apiv1.Container,
podLimitRange *apiv1.LimitRangeItem) apiv1.ResourceList {
// Both limit and request must be higher than min set in the limit range:
// https://github.com/kubernetes/kubernetes/blob/016e9d5c06089774c6286fd825302cbae661a446/plugin/pkg/admission/limitranger/admission.go#L303
if podLimitRange == nil {
return apiv1.ResourceList{}
}
minForLimit := getBoundaryRecommendation(recommendation, container, podLimitRange.Min, podLimitRange.Default)
minForRequest := podLimitRange.Min
if minForRequest == nil {
return minForLimit
}
result := minForLimit
if minForRequest.Cpu() != nil && minForRequest.Cpu().Cmp(*minForLimit.Cpu()) > 0 {
result[apiv1.ResourceCPU] = *minForRequest.Cpu()
}
if minForRequest.Memory() != nil && minForRequest.Memory().Cmp(*minForLimit.Memory()) > 0 {
result[apiv1.ResourceMemory] = *minForRequest.Memory()
}
return result
}
func getBoundaryRecommendation(recommendation apiv1.ResourceList, container apiv1.Container,
boundaryLimit, defaultLimit apiv1.ResourceList) apiv1.ResourceList {
if boundaryLimit == nil {
return apiv1.ResourceList{}
}
cpuMaxRequest := GetBoundaryRequest(container.Resources.Requests.Cpu(), container.Resources.Limits.Cpu(), boundaryLimit.Cpu(), defaultLimit.Cpu())
memMaxRequest := GetBoundaryRequest(container.Resources.Requests.Memory(), container.Resources.Limits.Memory(), boundaryLimit.Memory(), defaultLimit.Memory())
return apiv1.ResourceList{
apiv1.ResourceCPU: *cpuMaxRequest,
apiv1.ResourceMemory: *memMaxRequest,
}
}
func applyPodLimitRange(resources []vpa_types.RecommendedContainerResources,
pod *apiv1.Pod, limitRange apiv1.LimitRangeItem, resourceName apiv1.ResourceName,
fieldGetter func(vpa_types.RecommendedContainerResources) *apiv1.ResourceList) []vpa_types.RecommendedContainerResources {
minLimit := limitRange.Min[resourceName]
maxLimit := limitRange.Max[resourceName]
defaultLimit := limitRange.Default[resourceName]
var sumLimit, sumRecommendation resource.Quantity
for i, container := range pod.Spec.Containers {
if i >= len(resources) {
continue
}
limit := container.Resources.Limits[resourceName]
request := container.Resources.Requests[resourceName]
recommendation := (*fieldGetter(resources[i]))[resourceName]
containerLimit, _ := getProportionalResourceLimit(resourceName, &limit, &request, &recommendation, &defaultLimit)
if containerLimit != nil {
sumLimit.Add(*containerLimit)
}
sumRecommendation.Add(recommendation)
}
if minLimit.Cmp(sumLimit) <= 0 && minLimit.Cmp(sumRecommendation) <= 0 && (maxLimit.IsZero() || maxLimit.Cmp(sumLimit) >= 0) {
return resources
}
if minLimit.Cmp(sumRecommendation) > 0 && !sumLimit.IsZero() {
for i := range pod.Spec.Containers {
request := (*fieldGetter(resources[i]))[resourceName]
cappedContainerRequest, _ := scaleQuantityProportionally(&request, &sumRecommendation, &minLimit)
(*fieldGetter(resources[i]))[resourceName] = *cappedContainerRequest
}
return resources
}
if sumLimit.IsZero() {
return resources
}
var targetTotalLimit resource.Quantity
if minLimit.Cmp(sumLimit) > 0 {
targetTotalLimit = minLimit
}
if !maxLimit.IsZero() && maxLimit.Cmp(sumLimit) < 0 {
targetTotalLimit = maxLimit
}
for i := range pod.Spec.Containers {
limit := (*fieldGetter(resources[i]))[resourceName]
cappedContainerRequest, _ := scaleQuantityProportionally(&limit, &sumLimit, &targetTotalLimit)
(*fieldGetter(resources[i]))[resourceName] = *cappedContainerRequest
}
return resources
}
func (c *cappingRecommendationProcessor) capProportionallyToPodLimitRange(
containerRecommendations []vpa_types.RecommendedContainerResources, pod *apiv1.Pod) ([]vpa_types.RecommendedContainerResources, error) {
podLimitRange, err := c.limitsRangeCalculator.GetPodLimitRangeItem(pod.Namespace)
if err != nil {
return nil, fmt.Errorf("error obtaining limit range: %s", err)
}
if podLimitRange == nil {
return containerRecommendations, nil
}
getTarget := func(rl vpa_types.RecommendedContainerResources) *apiv1.ResourceList { return &rl.Target }
getUpper := func(rl vpa_types.RecommendedContainerResources) *apiv1.ResourceList { return &rl.UpperBound }
getLower := func(rl vpa_types.RecommendedContainerResources) *apiv1.ResourceList { return &rl.LowerBound }
containerRecommendations = applyPodLimitRange(containerRecommendations, pod, *podLimitRange, apiv1.ResourceCPU, getUpper)
containerRecommendations = applyPodLimitRange(containerRecommendations, pod, *podLimitRange, apiv1.ResourceMemory, getUpper)
containerRecommendations = applyPodLimitRange(containerRecommendations, pod, *podLimitRange, apiv1.ResourceCPU, getTarget)
containerRecommendations = applyPodLimitRange(containerRecommendations, pod, *podLimitRange, apiv1.ResourceMemory, getTarget)
containerRecommendations = applyPodLimitRange(containerRecommendations, pod, *podLimitRange, apiv1.ResourceCPU, getLower)
containerRecommendations = applyPodLimitRange(containerRecommendations, pod, *podLimitRange, apiv1.ResourceMemory, getLower)
return containerRecommendations, nil
}