feat(common): add support for Vertical Pod Autoscaler (#34502)

**Description**
vertical pod autoscaler would allow our users, even on single nodes, to
more dynamically adapt the resource limits and request of their charts.

For multi-node this will likely also lead to better resource spread.

⚒️ Fixes  #34180

**⚙️ Type of change**

- [x] ⚙️ Feature/App addition
- [ ] 🪛 Bugfix
- [ ] ⚠️ Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] 🔃 Refactor of current code
- [ ] 📜 Documentation Changes

**🧪 How Has This Been Tested?**
<!--
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration
-->

**📃 Notes:**
<!-- Please enter any other relevant information here -->

**✔️ Checklist:**

- [ ] ⚖️ My code follows the style guidelines of this project
- [ ] 👀 I have performed a self-review of my own code
- [ ] #️⃣ I have commented my code, particularly in hard-to-understand
areas
- [ ] 📄 I have made changes to the documentation
- [ ] 🧪 I have added tests to this description that prove my fix is
effective or that my feature works
- [ ] ⬆️ I increased versions for any altered app according to semantic
versioning
- [ ] I made sure the title starts with `feat(chart-name):`,
`fix(chart-name):`, `chore(chart-name):`, `docs(chart-name):` or
`fix(docs):`

** App addition**

If this PR is an app addition please make sure you have done the
following.

- [ ] 🖼️ I have added an icon in the Chart's root directory called
`icon.png`

---

_Please don't blindly check all the boxes. Read them and only check
those that apply.
Those checkboxes are there for the reviewer to see what is this all
about and
the status of this PR with a quick glance._

---------

Signed-off-by: Kjeld Schouten <info@kjeldschouten.nl>
Co-authored-by: Stavros Kois <s.kois@outlook.com>
This commit is contained in:
Kjeld Schouten
2025-04-21 21:48:18 +02:00
committed by GitHub
co-authored by Stavros Kois
parent 38545d5bea
commit dc19a3ecdc
8 changed files with 400 additions and 1 deletions
+1 -1
View File
@@ -48,4 +48,4 @@ sources:
- https://hub.docker.com/_/
- https://hub.docker.com/r/mikefarah/yq
type: library
version: 28.4.0
version: 28.5.0
@@ -0,0 +1,45 @@
{{/*
This template serves as a blueprint for vertical pod autoscaler objects that are created
using the common library.
*/}}
{{- define "tc.v1.common.class.vpa" -}}
{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}
{{- $_ := set $objectData "updatePolicy" ($objectData.updatePolicy | default dict) -}}
{{- $_ := set $objectData "resourcePolicy" ($objectData.resourcePolicy | default dict) }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: {{ $objectData.name }}
namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "VPA") }}
{{- $labels := (mustMerge ($objectData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}}
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }}
labels:
{{- . | nindent 4 }}
{{- end -}}
{{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}}
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }}
annotations:
{{- . | nindent 4 }}
{{- end }}
spec:
targetRef:
apiVersion: apps/v1
kind: {{ $objectData.workload.type }}
name: {{ $objectData.name }}
updatePolicy:
updateMode: {{ $objectData.updatePolicy.updateMode | default "Auto" }}
{{- with $objectData.updatePolicy.minReplicas }}
minReplicas: {{ . }}
{{- end -}}
{{- if $objectData.updatePolicy.evictionRequirements }}
evictionRequirements:
{{- range $req := $objectData.updatePolicy.evictionRequirements }}
- resources: {{ $req.resources | toJson }}
changeRequirement: {{ $req.changeRequirement }}
{{- end }}
{{- end -}}
{{- end -}}
@@ -0,0 +1,55 @@
{{- define "tc.v1.common.lib.vpa.validation" -}}
{{- $objectData := .objectData -}}
{{- $rootCtx := .rootCtx -}}
{{- $updPolicy := $objectData.updatePolicy -}}
{{- if $updPolicy -}}
{{- if not (kindIs "map" $updPolicy) -}}
{{- fail (printf "Vertical Pod Autoscaler - Expected [updatePolicy] to be a dictionary, but got [%s]" (kindOf $updPolicy)) -}}
{{- end -}}
{{- if $updPolicy.updateMode -}}
{{- $validModes := list "Auto" "Off" "Initial" "Recreate" -}}
{{- if not (mustHas $updPolicy.updateMode $validModes) -}}
{{- fail (printf "Vertical Pod Autoscaler - Value [%s] on [vpa.%s.updatePolicy.updateMode] is not valid. Must be one of [%s]" $updPolicy.updateMode $objectData.vpaName (join ", " $validModes)) -}}
{{- end -}}
{{- end -}}
{{- if and $updPolicy.minReplicas (le ($updPolicy.minReplicas | int) 0) -}}
{{- fail (printf "Vertical Pod Autoscaler - Value [%v] on [vpa.%s.updatePolicy.minReplicas] must be greater than 0." $updPolicy.minReplicas $objectData.vpaName) -}}
{{- end -}}
{{- if $updPolicy.evictionRequirements -}}
{{- if not (kindIs "slice" $updPolicy.evictionRequirements) -}}
{{- fail (printf "Vertical Pod Autoscaler - Value on [vpa.%s.updatePolicy.evictionRequirements] must be a list, but got [%s]" $objectData.vpaName (kindOf $updPolicy.evictionRequirements)) -}}
{{- end -}}
{{- range $idx, $req := $updPolicy.evictionRequirements -}}
{{- if not (kindIs "map" $req) -}}
{{- fail (printf "Vertical Pod Autoscaler - Value on [vpa.%s.updatePolicy.evictionRequirements.%d] must be a map, but got [%s]" $objectData.vpaName $idx (kindOf $req)) -}}
{{- end -}}
{{- if not $req.resources -}}
{{- fail (printf "Vertical Pod Autoscaler - Value on [vpa.%s.updatePolicy.evictionRequirements.%d.resources] is required." $objectData.vpaName $idx) -}}
{{- end -}}
{{- if not (kindIs "slice" $req.resources) -}}
{{- fail (printf "Vertical Pod Autoscaler - Value on [vpa.%s.updatePolicy.evictionRequirements.%d.resources] must be a list, but got [%s]" $objectData.vpaName $idx (kindOf $req.resources)) -}}
{{- end -}}
{{- $validResources := (list "cpu" "memory") -}}
{{- range $x, $r := $req.resources -}}
{{- if not (mustHas $r $validResources) -}}
{{- fail (printf "Vertical Pod Autoscaler - Value [%s] on [vpa.%s.updatePolicy.evictionRequirements.%d.resources.%d] is not valid. Must be one of [%s]" $r $objectData.vpaName $idx $x (join ", " $validResources)) -}}
{{- end -}}
{{- end -}}
{{- $validReq := (list "TargetHigherThanRequests" "TargetLowerThanRequests") -}}
{{- if not (mustHas $req.changeRequirement $validReq) -}}
{{- fail (printf "Vertical Pod Autoscaler - Value [%s] on [vpa.%s.updatePolicy.evictionRequirements.%d.changeRequirement] is not valid. Must be one of [%s]" $req.changeRequirement $objectData.vpaName $idx (join ", " $validReq)) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
@@ -73,4 +73,7 @@
{{/* Render Cert-Manager Certificates(s) */}}
{{- include "tc.v1.common.spawner.certificate" . | nindent 0 -}}
{{/* Render Vertical Pod Autoscaler */}}
{{ include "tc.v1.common.spawner.vpa" . | nindent 0 -}}
{{- end -}}
@@ -0,0 +1,59 @@
{{/* Vertical Pod Autoscaler Spawner */}}
{{/* Call this template:
{{ include "tc.v1.common.spawner.vpa" $ -}}
*/}}
{{- define "tc.v1.common.spawner.vpa" -}}
{{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}}
{{- range $name, $vpa := .Values.vpa -}}
{{- $enabledVPA := (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $ "objectData" $vpa
"name" $name "caller" "Vertical Pod Autoscaler"
"key" "vpa")) -}}
{{- if ne $enabledVPA "true" -}}{{- continue -}}{{- end -}}
{{- $objectData := (mustDeepCopy $vpa) -}}
{{- $_ := set $objectData "vpaName" $name -}}
{{- include "tc.v1.common.lib.vpa.validation" (dict "objectData" $objectData "rootCtx" $) -}}
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $name) -}}
{{- range $workloadName, $workload := $.Values.workload -}}
{{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $ "objectData" $workload
"name" $name "caller" "vpa"
"key" "workload")) -}}
{{- if ne $enabled "true" -}}{{- continue -}}{{- end -}}
{{/* Create a copy of the workload */}}
{{- $_ := set $objectData "workload" (mustDeepCopy $workload) -}}
{{/* Generate the name of the vpa */}}
{{- $objectName := $fullname -}}
{{- if not $objectData.workload.primary -}}
{{- $objectName = printf "%s-%s" $fullname $workloadName -}}
{{- end -}}
{{/* Perform validations */}}
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $objectName) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "Vertical Pod Autoscaler") -}}
{{/* Set the name of the workload */}}
{{- $_ := set $objectData "name" $objectName -}}
{{/* Short name is the one that defined on the chart, used on selectors */}}
{{- $_ := set $objectData "shortName" $workloadName -}}
{{- if or (not $objectData.targetSelector) (hasKey $objectData.targetSelector $workloadName) -}}
{{/* Call class to create the object */}}
{{- $types := (list "Deployment" "StatefulSet" "DaemonSet") -}}
{{- if (mustHas $objectData.workload.type $types) -}}
{{- include "tc.v1.common.class.vpa" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
+18
View File
@@ -195,6 +195,24 @@ TZ: UTC
diagnosticMode:
enabled: false
# -- Vertical pod autoscaler
vpa:
main:
enabled: false
targetSelector: []
# updatePolicy:
# updateMode: auto
resourcePolicy:
containerPolicies:
- containerName: '*'
minAllowed:
cpu: 50m
memory: 50Mi
maxAllowed:
cpu: 8000m
memory: 20Gi
controlledResources: ["cpu", "memory"]
# -- (docs/service/README.md)
service:
main: