@@ -21,10 +21,13 @@ import (
2121 "strings"
2222
2323 core "k8s.io/api/core/v1"
24+ "k8s.io/apimachinery/pkg/api/resource"
2425
2526 resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
2627 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource/pod/recommendation"
2728 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"
2831 resourcehelpers "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/resources"
2932 vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
3033)
@@ -37,13 +40,15 @@ const (
3740
3841type resourcesUpdatesPatchCalculator struct {
3942 recommendationProvider recommendation.Provider
43+ maxAllowedCPUBoost resource.Quantity
4044}
4145
4246// NewResourceUpdatesCalculator returns a calculator for
4347// resource update patches.
44- func NewResourceUpdatesCalculator (recommendationProvider recommendation.Provider ) Calculator {
48+ func NewResourceUpdatesCalculator (recommendationProvider recommendation.Provider , maxAllowedCPUBoost resource. QuantityValue ) Calculator {
4549 return & resourcesUpdatesPatchCalculator {
4650 recommendationProvider : recommendationProvider ,
51+ maxAllowedCPUBoost : maxAllowedCPUBoost .Quantity ,
4752 }
4853}
4954
@@ -59,15 +64,43 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
5964 return []resource_admission.PatchRecord {}, fmt .Errorf ("failed to calculate resource patch for pod %s/%s: %v" , pod .Namespace , pod .Name , err )
6065 }
6166
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+
6277 if annotationsPerContainer == nil {
6378 annotationsPerContainer = vpa_api_util.ContainerToAnnotationsMap {}
6479 }
6580
6681 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+ }
71104 }
72105
73106 if len (updatesAnnotation ) > 0 {
@@ -108,3 +141,114 @@ func appendPatchesAndAnnotations(patches []resource_admission.PatchRecord, annot
108141 }
109142 return patches , annotations
110143}
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