Skip to content

Commit d0713f2

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

File tree

9 files changed

+763
-11
lines changed

9 files changed

+763
-11
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` | string | | 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: 10 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,6 +79,7 @@ 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 = flag.String("max-allowed-cpu-boost", "", "Maximum amount of CPU that will be applied for a container with boost.")
8183
)
8284

8385
func main() {
@@ -93,6 +95,13 @@ func main() {
9395
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
9496
}
9597

98+
if *maxAllowedCPUBoost != "" {
99+
if _, err := resource.ParseQuantity(*maxAllowedCPUBoost); err != nil {
100+
klog.ErrorS(err, "Failed to parse maxAllowedCPUBoost")
101+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
102+
}
103+
}
104+
96105
healthCheck := metrics.NewHealthCheck(time.Minute)
97106
metrics_admission.Register()
98107
server.Initialize(&commonFlags.EnableProfiling, healthCheck, address)
@@ -145,7 +154,7 @@ func main() {
145154
hostname,
146155
)
147156

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

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

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ import (
2121
"strings"
2222

2323
core "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/api/resource"
25+
"k8s.io/klog/v2"
2426

2527
resource_admission "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
2628
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource/pod/recommendation"
2729
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
30+
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/features"
31+
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/annotations"
2832
resourcehelpers "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/resources"
2933
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
3034
)
@@ -37,13 +41,19 @@ const (
3741

3842
type resourcesUpdatesPatchCalculator struct {
3943
recommendationProvider recommendation.Provider
44+
maxAllowedCPUBoost resource.Quantity
4045
}
4146

4247
// NewResourceUpdatesCalculator returns a calculator for
4348
// resource update patches.
44-
func NewResourceUpdatesCalculator(recommendationProvider recommendation.Provider) Calculator {
49+
func NewResourceUpdatesCalculator(recommendationProvider recommendation.Provider, maxAllowedCPUBoost string) Calculator {
50+
var maxAllowedCPUBoostQuantity resource.Quantity
51+
if maxAllowedCPUBoost != "" {
52+
maxAllowedCPUBoostQuantity = resource.MustParse(maxAllowedCPUBoost)
53+
}
4554
return &resourcesUpdatesPatchCalculator{
4655
recommendationProvider: recommendationProvider,
56+
maxAllowedCPUBoost: maxAllowedCPUBoostQuantity,
4757
}
4858
}
4959

@@ -52,11 +62,22 @@ func (*resourcesUpdatesPatchCalculator) PatchResourceTarget() PatchResourceTarge
5262
}
5363

5464
func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *vpa_types.VerticalPodAutoscaler) ([]resource_admission.PatchRecord, error) {
65+
klog.Infof("Calculating patches for pod %s/%s with VPA %s", pod.Namespace, pod.Name, vpa.Name)
5566
result := []resource_admission.PatchRecord{}
5667

5768
containersResources, annotationsPerContainer, err := c.recommendationProvider.GetContainersResourcesForPod(pod, vpa)
5869
if err != nil {
59-
return []resource_admission.PatchRecord{}, fmt.Errorf("failed to calculate resource patch for pod %s/%s: %v", pod.Namespace, pod.Name, err)
70+
return nil, fmt.Errorf("failed to calculate resource patch for pod %s/%s: %v", pod.Namespace, pod.Name, err)
71+
}
72+
73+
if vpa_api_util.GetUpdateMode(vpa) == vpa_types.UpdateModeOff {
74+
// If update mode is "Off", we don't want to apply any recommendations,
75+
// but we still want to apply startup boost.
76+
for i := range containersResources {
77+
containersResources[i].Requests = nil
78+
containersResources[i].Limits = nil
79+
}
80+
annotationsPerContainer = vpa_api_util.ContainerToAnnotationsMap{}
6081
}
6182

6283
if annotationsPerContainer == nil {
@@ -65,9 +86,65 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
6586

6687
updatesAnnotation := []string{}
6788
for i, containerResources := range containersResources {
89+
// Apply startup boost if configured
90+
if features.Enabled(features.CPUStartupBoost) {
91+
policy := vpa_api_util.GetContainerResourcePolicy(pod.Spec.Containers[i].Name, vpa.Spec.ResourcePolicy)
92+
if policy != nil && policy.Mode != nil && *policy.Mode == vpa_types.ContainerScalingModeOff {
93+
klog.V(4).InfoS("Not applying startup boost for container", "containerName", pod.Spec.Containers[i].Name, "reason", "scaling mode is Off")
94+
continue
95+
} else {
96+
startupBoostPolicy := getContainerStartupBoostPolicy(&pod.Spec.Containers[i], vpa)
97+
if startupBoostPolicy != nil {
98+
originalRequest := pod.Spec.Containers[i].Resources.Requests[core.ResourceCPU]
99+
boostedRequest, err := calculateBoostedCPU(originalRequest, startupBoostPolicy)
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
if !c.maxAllowedCPUBoost.IsZero() && boostedRequest.Cmp(c.maxAllowedCPUBoost) > 0 {
105+
boostedRequest = &c.maxAllowedCPUBoost
106+
}
107+
if containerResources.Requests == nil {
108+
containerResources.Requests = core.ResourceList{}
109+
}
110+
controlledValues := vpa_api_util.GetContainerControlledValues(pod.Spec.Containers[i].Name, vpa.Spec.ResourcePolicy)
111+
resourceList := core.ResourceList{core.ResourceCPU: *boostedRequest}
112+
if controlledValues == vpa_types.ContainerControlledValuesRequestsOnly {
113+
vpa_api_util.CapRecommendationToContainerLimit(resourceList, pod.Spec.Containers[i].Resources.Limits)
114+
}
115+
containerResources.Requests[core.ResourceCPU] = resourceList[core.ResourceCPU]
116+
117+
if controlledValues == vpa_types.ContainerControlledValuesRequestsAndLimits {
118+
if containerResources.Limits == nil {
119+
containerResources.Limits = core.ResourceList{}
120+
}
121+
originalLimit := pod.Spec.Containers[i].Resources.Limits[core.ResourceCPU]
122+
if originalLimit.IsZero() {
123+
originalLimit = pod.Spec.Containers[i].Resources.Requests[core.ResourceCPU]
124+
}
125+
boostedLimit, err := calculateBoostedCPU(originalLimit, startupBoostPolicy)
126+
if err != nil {
127+
return nil, err
128+
}
129+
if !c.maxAllowedCPUBoost.IsZero() && boostedLimit.Cmp(c.maxAllowedCPUBoost) > 0 {
130+
boostedLimit = &c.maxAllowedCPUBoost
131+
}
132+
containerResources.Limits[core.ResourceCPU] = *boostedLimit
133+
}
134+
originalResources, err := annotations.GetOriginalResourcesAnnotationValue(&pod.Spec.Containers[i])
135+
if err != nil {
136+
return nil, err
137+
}
138+
result = append(result, GetAddAnnotationPatch(annotations.StartupCPUBoostAnnotation, originalResources))
139+
}
140+
}
141+
}
142+
68143
newPatches, newUpdatesAnnotation := getContainerPatch(pod, i, annotationsPerContainer, containerResources)
69-
result = append(result, newPatches...)
70-
updatesAnnotation = append(updatesAnnotation, newUpdatesAnnotation)
144+
if len(newPatches) > 0 {
145+
result = append(result, newPatches...)
146+
updatesAnnotation = append(updatesAnnotation, newUpdatesAnnotation)
147+
}
71148
}
72149

73150
if len(updatesAnnotation) > 0 {
@@ -77,6 +154,49 @@ func (c *resourcesUpdatesPatchCalculator) CalculatePatches(pod *core.Pod, vpa *v
77154
return result, nil
78155
}
79156

157+
func getContainerStartupBoostPolicy(container *core.Container, vpa *vpa_types.VerticalPodAutoscaler) *vpa_types.StartupBoost {
158+
policy := vpa_api_util.GetContainerResourcePolicy(container.Name, vpa.Spec.ResourcePolicy)
159+
startupBoost := vpa.Spec.StartupBoost
160+
if policy != nil && policy.StartupBoost != nil {
161+
startupBoost = policy.StartupBoost
162+
}
163+
return startupBoost
164+
}
165+
166+
func calculateBoostedCPU(baseCPU resource.Quantity, startupBoost *vpa_types.StartupBoost) (*resource.Quantity, error) {
167+
if startupBoost == nil {
168+
return &baseCPU, nil
169+
}
170+
171+
boostType := startupBoost.CPU.Type
172+
if boostType == "" {
173+
boostType = vpa_types.FactorStartupBoostType
174+
}
175+
176+
switch boostType {
177+
case vpa_types.FactorStartupBoostType:
178+
if startupBoost.CPU.Factor == nil {
179+
return nil, fmt.Errorf("startupBoost.CPU.Factor is required when Type is Factor or not specified")
180+
}
181+
factor := *startupBoost.CPU.Factor
182+
if factor < 1 {
183+
return nil, fmt.Errorf("boost factor must be >= 1")
184+
}
185+
boostedCPU := baseCPU.MilliValue()
186+
boostedCPU = int64(float64(boostedCPU) * float64(factor))
187+
return resource.NewMilliQuantity(boostedCPU, resource.DecimalSI), nil
188+
case vpa_types.QuantityStartupBoostType:
189+
if startupBoost.CPU.Quantity == nil {
190+
return nil, fmt.Errorf("startupBoost.CPU.Quantity is required when Type is Quantity")
191+
}
192+
quantity := *startupBoost.CPU.Quantity
193+
boostedCPU := baseCPU.MilliValue() + quantity.MilliValue()
194+
return resource.NewMilliQuantity(boostedCPU, resource.DecimalSI), nil
195+
default:
196+
return nil, fmt.Errorf("unsupported startup boost type: %s", startupBoost.CPU.Type)
197+
}
198+
}
199+
80200
func getContainerPatch(pod *core.Pod, i int, annotationsPerContainer vpa_api_util.ContainerToAnnotationsMap, containerResources vpa_api_util.ContainerResources) ([]resource_admission.PatchRecord, string) {
81201
var patches []resource_admission.PatchRecord
82202
// Add empty resources object if missing.

0 commit comments

Comments
 (0)