@@ -21,10 +21,13 @@ import (
21
21
"strings"
22
22
23
23
core "k8s.io/api/core/v1"
24
+ "k8s.io/apimachinery/pkg/api/resource"
24
25
25
26
resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
26
27
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource/pod/recommendation"
27
28
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
29
+ "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/features"
30
+ "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/annotations"
28
31
resourcehelpers "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/resources"
29
32
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
30
33
)
@@ -37,13 +40,15 @@ const (
37
40
38
41
type resourcesUpdatesPatchCalculator struct {
39
42
recommendationProvider recommendation.Provider
43
+ maxAllowedCPUBoost resource.Quantity
40
44
}
41
45
42
46
// NewResourceUpdatesCalculator returns a calculator for
43
47
// resource update patches.
44
- func NewResourceUpdatesCalculator (recommendationProvider recommendation.Provider ) Calculator {
48
+ func NewResourceUpdatesCalculator (recommendationProvider recommendation.Provider , maxAllowedCPUBoost resource. QuantityValue ) Calculator {
45
49
return & resourcesUpdatesPatchCalculator {
46
50
recommendationProvider : recommendationProvider ,
51
+ maxAllowedCPUBoost : maxAllowedCPUBoost .Quantity ,
47
52
}
48
53
}
49
54
@@ -59,15 +64,43 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
59
64
return []resource_admission.PatchRecord {}, fmt .Errorf ("failed to calculate resource patch for pod %s/%s: %v" , pod .Namespace , pod .Name , err )
60
65
}
61
66
67
+ if vpa_api_util .GetUpdateMode (vpa ) == vpa_types .UpdateModeOff {
68
+ // If update mode is "Off", we don't want to apply any recommendations,
69
+ // but we still want to apply startup boost.
70
+ for i := range containersResources {
71
+ containersResources [i ].Requests = nil
72
+ containersResources [i ].Limits = nil
73
+ }
74
+ annotationsPerContainer = vpa_api_util.ContainerToAnnotationsMap {}
75
+ }
76
+
62
77
if annotationsPerContainer == nil {
63
78
annotationsPerContainer = vpa_api_util.ContainerToAnnotationsMap {}
64
79
}
65
80
66
81
updatesAnnotation := []string {}
67
- for i , containerResources := range containersResources {
68
- newPatches , newUpdatesAnnotation := getContainerPatch (pod , i , annotationsPerContainer , containerResources )
69
- result = append (result , newPatches ... )
70
- updatesAnnotation = append (updatesAnnotation , newUpdatesAnnotation )
82
+ cpuStartupBoostEnabled := features .Enabled (features .CPUStartupBoost )
83
+ for i := range containersResources {
84
+
85
+ // Apply startup boost if configured
86
+ if cpuStartupBoostEnabled {
87
+ // Get the container resource policy to check for scaling mode.
88
+ policy := vpa_api_util .GetContainerResourcePolicy (pod .Spec .Containers [i ].Name , vpa .Spec .ResourcePolicy )
89
+ if policy != nil && policy .Mode != nil && * policy .Mode == vpa_types .ContainerScalingModeOff {
90
+ continue
91
+ }
92
+ boostPatches , err := c .applyCPUStartupBoost (i , & pod .Spec .Containers [i ], vpa , & containersResources [i ])
93
+ if err != nil {
94
+ return nil , err
95
+ }
96
+ result = append (result , boostPatches ... )
97
+ }
98
+
99
+ newPatches , newUpdatesAnnotation := getContainerPatch (pod , i , annotationsPerContainer , containersResources [i ])
100
+ if len (newPatches ) > 0 {
101
+ result = append (result , newPatches ... )
102
+ updatesAnnotation = append (updatesAnnotation , newUpdatesAnnotation )
103
+ }
71
104
}
72
105
73
106
if len (updatesAnnotation ) > 0 {
@@ -108,3 +141,114 @@ func appendPatchesAndAnnotations(patches []resource_admission.PatchRecord, annot
108
141
}
109
142
return patches , annotations
110
143
}
144
+
145
+ func (c * resourcesUpdatesPatchCalculator ) applyCPUStartupBoost (containerIndex int , container * core.Container , vpa * vpa_types.VerticalPodAutoscaler , containerResources * vpa_api_util.ContainerResources ) ([]resource_admission.PatchRecord , error ) {
146
+ var patches []resource_admission.PatchRecord
147
+
148
+ startupBoostPolicy := getContainerStartupBoostPolicy (container , vpa )
149
+ if startupBoostPolicy == nil {
150
+ return nil , nil
151
+ }
152
+
153
+ originalRequest := container .Resources .Requests [core .ResourceCPU ]
154
+ boostedRequest , err := calculateBoostedCPU (originalRequest , startupBoostPolicy )
155
+ if err != nil {
156
+ return nil , err
157
+ }
158
+
159
+ if ! c .maxAllowedCPUBoost .IsZero () && boostedRequest .Cmp (c .maxAllowedCPUBoost ) > 0 {
160
+ boostedRequest = & c .maxAllowedCPUBoost
161
+ }
162
+
163
+ controlledCPUResourcePatches , err := c .applyControlledCPUResources (containerIndex , container , vpa , containerResources , boostedRequest , startupBoostPolicy )
164
+ if err != nil {
165
+ return nil , err
166
+ }
167
+ patches = append (patches , controlledCPUResourcePatches ... )
168
+
169
+ originalResources , err := annotations .GetOriginalResourcesAnnotationValue (container )
170
+ if err != nil {
171
+ return nil , err
172
+ }
173
+ patches = append (patches , GetAddAnnotationPatch (annotations .StartupCPUBoostAnnotation , originalResources ))
174
+
175
+ return patches , nil
176
+ }
177
+
178
+ func getContainerStartupBoostPolicy (container * core.Container , vpa * vpa_types.VerticalPodAutoscaler ) * vpa_types.StartupBoost {
179
+ policy := vpa_api_util .GetContainerResourcePolicy (container .Name , vpa .Spec .ResourcePolicy )
180
+ startupBoost := vpa .Spec .StartupBoost
181
+ if policy != nil && policy .StartupBoost != nil {
182
+ startupBoost = policy .StartupBoost
183
+ }
184
+ return startupBoost
185
+ }
186
+
187
+ func calculateBoostedCPU (baseCPU resource.Quantity , startupBoost * vpa_types.StartupBoost ) (* resource.Quantity , error ) {
188
+ if startupBoost == nil {
189
+ return & baseCPU , nil
190
+ }
191
+
192
+ boostType := startupBoost .CPU .Type
193
+ if boostType == "" {
194
+ boostType = vpa_types .FactorStartupBoostType
195
+ }
196
+
197
+ switch boostType {
198
+ case vpa_types .FactorStartupBoostType :
199
+ if startupBoost .CPU .Factor == nil {
200
+ return nil , fmt .Errorf ("startupBoost.CPU.Factor is required when Type is Factor or not specified" )
201
+ }
202
+ factor := * startupBoost .CPU .Factor
203
+ if factor < 1 {
204
+ return nil , fmt .Errorf ("boost factor must be >= 1" )
205
+ }
206
+ boostedCPU := baseCPU .MilliValue ()
207
+ boostedCPU = int64 (float64 (boostedCPU ) * float64 (factor ))
208
+ return resource .NewMilliQuantity (boostedCPU , resource .DecimalSI ), nil
209
+ case vpa_types .QuantityStartupBoostType :
210
+ if startupBoost .CPU .Quantity == nil {
211
+ return nil , fmt .Errorf ("startupBoost.CPU.Quantity is required when Type is Quantity" )
212
+ }
213
+ quantity := * startupBoost .CPU .Quantity
214
+ boostedCPU := baseCPU .MilliValue () + quantity .MilliValue ()
215
+ return resource .NewMilliQuantity (boostedCPU , resource .DecimalSI ), nil
216
+ default :
217
+ return nil , fmt .Errorf ("unsupported startup boost type: %s" , startupBoost .CPU .Type )
218
+ }
219
+ }
220
+
221
+ func (c * resourcesUpdatesPatchCalculator ) applyControlledCPUResources (containerIndex int , container * core.Container , vpa * vpa_types.VerticalPodAutoscaler , containerResources * vpa_api_util.ContainerResources , boostedRequest * resource.Quantity , startupBoostPolicy * vpa_types.StartupBoost ) ([]resource_admission.PatchRecord , error ) {
222
+ controlledValues := vpa_api_util .GetContainerControlledValues (container .Name , vpa .Spec .ResourcePolicy )
223
+
224
+ // Apply CPU Requests
225
+ if containerResources .Requests == nil {
226
+ containerResources .Requests = core.ResourceList {}
227
+ }
228
+ resourceList := core.ResourceList {core .ResourceCPU : * boostedRequest }
229
+ if controlledValues == vpa_types .ContainerControlledValuesRequestsOnly {
230
+ vpa_api_util .CapRecommendationToContainerLimit (resourceList , container .Resources .Limits )
231
+ }
232
+ containerResources .Requests [core .ResourceCPU ] = resourceList [core .ResourceCPU ]
233
+
234
+ // Apply CPU Limits
235
+ if controlledValues == vpa_types .ContainerControlledValuesRequestsAndLimits {
236
+ if containerResources .Limits == nil {
237
+ containerResources .Limits = core.ResourceList {}
238
+ }
239
+ originalLimit := container .Resources .Limits [core .ResourceCPU ]
240
+ if originalLimit .IsZero () {
241
+ originalLimit = container .Resources .Requests [core .ResourceCPU ]
242
+ }
243
+ boostedLimit , err := calculateBoostedCPU (originalLimit , startupBoostPolicy )
244
+ if err != nil {
245
+ return nil , err
246
+ }
247
+ if ! c .maxAllowedCPUBoost .IsZero () && boostedLimit .Cmp (c .maxAllowedCPUBoost ) > 0 {
248
+ boostedLimit = & c .maxAllowedCPUBoost
249
+ }
250
+ containerResources .Limits [core .ResourceCPU ] = * boostedLimit
251
+ }
252
+
253
+ return nil , nil
254
+ }
0 commit comments