Skip to content

Commit 27cca7d

Browse files
committed
Apply CPU startup boost in admission controller if its set
1 parent 0000a7a commit 27cca7d

File tree

9 files changed

+785
-12
lines changed

9 files changed

+785
-12
lines changed

vertical-pod-autoscaler/docs/flags.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This document is auto-generated from the flag definitions in the VPA admission-c
2424
| `log-file` | string | | If non-empty, use this log file (no effect when -logtostderr=true) |
2525
| `log-file-max-size` | int | 1800 | uDefines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. |
2626
| `logtostderr` | | true | log to standard error instead of files |
27+
| `max-allowed-cpu-boost` | | | quantity Maximum amount of CPU that will be applied for a container with boost. |
2728
| `min-tls-version` | string | | The minimum TLS version to accept. Must be set to either tls1_2 or tls1_3. (default "tls1_2") |
2829
| `one-output` | severity | | If true, only write logs to their native level (vs also writing to each lower severity level; no effect when -logtostderr=true) |
2930
| `port` | int | 8000 | The port to listen on. |

vertical-pod-autoscaler/pkg/admission-controller/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/spf13/pflag"
28+
"k8s.io/apimachinery/pkg/api/resource"
2829
"k8s.io/client-go/informers"
2930
kube_client "k8s.io/client-go/kubernetes"
3031
typedadmregv1 "k8s.io/client-go/kubernetes/typed/admissionregistration/v1"
@@ -78,8 +79,13 @@ var (
7879
registerWebhook = flag.Bool("register-webhook", true, "If set to true, admission webhook object will be created on start up to register with the API server.")
7980
webhookLabels = flag.String("webhook-labels", "", "Comma separated list of labels to add to the webhook object. Format: key1:value1,key2:value2")
8081
registerByURL = flag.Bool("register-by-url", false, "If set to true, admission webhook will be registered by URL (webhookAddress:webhookPort) instead of by service name")
82+
maxAllowedCPUBoost = resource.QuantityValue{}
8183
)
8284

85+
func init() {
86+
flag.Var(&maxAllowedCPUBoost, "max-allowed-cpu-boost", "Maximum amount of CPU that will be applied for a container with boost.")
87+
}
88+
8389
func main() {
8490
commonFlags := common.InitCommonFlags()
8591
klog.InitFlags(nil)
@@ -145,7 +151,7 @@ func main() {
145151
hostname,
146152
)
147153

148-
calculators := []patch.Calculator{patch.NewResourceUpdatesCalculator(recommendationProvider), patch.NewObservedContainersCalculator()}
154+
calculators := []patch.Calculator{patch.NewResourceUpdatesCalculator(recommendationProvider, maxAllowedCPUBoost), patch.NewObservedContainersCalculator()}
149155
as := logic.NewAdmissionServer(podPreprocessor, vpaPreprocessor, limitRangeCalculator, vpaMatcher, calculators)
150156
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
151157
as.Serve(w, r)

vertical-pod-autoscaler/pkg/admission-controller/resource/pod/patch/resource_updates.go

Lines changed: 149 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3841
type 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

Comments
 (0)