Skip to content

feat(common): refactor horizontal pod autoscaler #34625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion charts/library/common-test/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ sources:
- https://github.com/truecharts/charts/tree/master/charts/library/common-test
type: application
version: 1.0.0

3 changes: 1 addition & 2 deletions charts/library/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,4 @@ sources:
- https://hub.docker.com/_/
- https://hub.docker.com/r/mikefarah/yq
type: library
version: 28.5.0

version: 28.6.0
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,41 @@ This template serves as a blueprint for horizontal pod autoscaler objects that a
using the common library.
*/}}
{{- define "tc.v1.common.class.hpa" -}}
{{- $targetName := include "tc.v1.common.lib.chart.names.fullname" . -}}
{{- $fullName := include "tc.v1.common.lib.chart.names.fullname" . -}}
{{- $hpaName := $fullName -}}
{{- $values := .Values.hpa -}}
{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}

{{- if hasKey . "ObjectValues" -}}
{{- with .ObjectValues.hpa -}}
{{- $values = . -}}
{{- end -}}
{{- end -}}
{{- $hpaLabels := $values.labels -}}
{{- $hpaAnnotations := $values.annotations -}}

{{- if and (hasKey $values "nameOverride") $values.nameOverride -}}
{{- $hpaName = printf "%v-%v" $hpaName $values.nameOverride -}}
{{- end }}
{{- $_ := set $objectData "updatePolicy" ($objectData.updatePolicy | default dict) -}}
{{- $_ := set $objectData "resourcePolicy" ($objectData.resourcePolicy | default dict) }}
---
apiVersion: {{ include "tc.v1.common.capabilities.hpa.apiVersion" $ }}
kind: HorizontalPodAutoscaler
metadata:
name: {{ $hpaName }}
namespace: {{ $.Values.namespace | default $.Values.global.namespace | default $.Release.Namespace }}
{{- $labels := (mustMerge ($hpaLabels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $ | fromYaml)) -}}
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $ "labels" $labels) | trim) }}
name: {{ $objectData.name }}
namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "hpa") }}
{{- $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 ($hpaAnnotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $ | fromYaml)) -}}
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $ "annotations" $annotations) | trim) }}
{{- $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 -}}
{{- end }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: {{ $values.targetKind | default ( include "tc.v1.common.names.controllerType" . ) }}
name: {{ $values.target | default $targetName }}
minReplicas: {{ $values.minReplicas | default 1 }}
maxReplicas: {{ $values.maxReplicas | default 3 }}
kind: {{ $objectData.workload.type }}
name: {{ $objectData.name }}
minReplicas: {{ $objectData.minReplicas | default 1 }}
maxReplicas: {{ $objectData.maxReplicas | default 3 }}
{{- with $objectData.metrics }}
metrics:
{{- if $values.targetCPUUtilizationPercentage }}
- type: Resource
resource:
name: cpu
targetAverageUtilization: {{ $values.targetCPUUtilizationPercentage }}
{{- end -}}
{{- if $values.targetMemoryUtilizationPercentage }}
- type: Resource
resource:
name: memory
targetAverageUtilization: {{ $values.targetMemoryUtilizationPercentage }}
{{- end -}}
{{- $objectData.metrics | toYaml | nindent 4 }}
{{- end -}}

{{- with $objectData.behavior }}
behavior:
{{- $objectData.behavior | toYaml | nindent 4 }}
{{- end -}}
{{- end -}}
7 changes: 7 additions & 0 deletions charts/library/common/templates/lib/hpa/_validation.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{- define "tc.v1.common.lib.hpa.validation" -}}
{{- $objectData := .objectData -}}
{{- $rootCtx := .rootCtx -}}

{{- $updPolicy := $objectData.updatePolicy -}}

{{- end -}}
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
{{/*
Renders the configMap objects required by the chart.
{{/* horizontal Pod Autoscaler Spawner */}}
{{/* Call this template:
{{ include "tc.v1.common.spawner.hpa" $ -}}
*/}}

{{- define "tc.v1.common.spawner.hpa" -}}
{{/* Generate named configMaps as required */}}
{{- range $name, $hpa := .Values.horizontalPodAutoscaler -}}
{{- if $hpa.enabled -}}
{{- $hpaValues := $hpa -}}

{{/* set the default nameOverride to the hpa name */}}
{{- if not $hpaValues.nameOverride -}}
{{- $_ := set $hpaValues "nameOverride" $name -}}
{{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}}
{{- range $name, $hpa := .Values.hpa -}}
{{- $enabledhpa := (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $ "objectData" $hpa
"name" $name "caller" "horizontal Pod Autoscaler"
"key" "hpa")) -}}

{{- if ne $enabledhpa "true" -}}{{- continue -}}{{- end -}}

{{- $objectData := (mustDeepCopy $hpa) -}}
{{- $_ := set $objectData "hpaName" $name -}}
{{- include "tc.v1.common.lib.hpa.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" "hpa"
"key" "workload")) -}}

{{- if ne $enabled "true" -}}{{- continue -}}{{- end -}}

{{/* Create a copy of the workload */}}
{{- $_ := set $objectData "workload" (mustDeepCopy $workload) -}}

{{/* Generate the name of the hpa */}}
{{- $objectName := $fullname -}}
{{- if not $objectData.workload.primary -}}
{{- $objectName = printf "%s-%s" $fullname $workloadName -}}
{{- end -}}

{{- $_ := set $ "ObjectValues" (dict "hpa" $hpaValues) -}}
{{- include "tc.v1.common.class.hpa" $ -}}
{{/* Perform validations */}}
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $objectName) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "horizontal 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.hpa" (dict "rootCtx" $ "objectData" $objectData) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
38 changes: 38 additions & 0 deletions charts/library/common/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,44 @@ vpa:
memory: 20Gi
controlledResources: ["cpu", "memory"]

# -- Horizontal pod autoscaler
hpa:
main:
enabled: false
targetSelector: []
# minReplicas: 1
# maxReplicas: 3

# metrics: # Optional, list of metric specs
# - type: Resource # Can be Resource, Pods, Object, External, or ContainerResource
# resource:
# name: cpu
# target:
# type: Utilization # Or Value / AverageValue
# averageUtilization: 50

# - type: Resource
# resource:
# name: memory
# target:
# type: AverageValue
# averageValue: 500Mi

# behavior: # Optional: controls scaling behavior
# scaleUp:
# stabilizationWindowSeconds: 0
# policies:
# - type: Percent
# value: 100
# periodSeconds: 15
# scaleDown:
# stabilizationWindowSeconds: 300
# policies:
# - type: Pods
# value: 4
# periodSeconds: 60


# -- (docs/service/README.md)
service:
main:
Expand Down
Loading