Skip to content

Commit 81a5697

Browse files
committed
feat(common): refactor horizontal pod autoscaler
1 parent ae61bd0 commit 81a5697

File tree

5 files changed

+120
-51
lines changed

5 files changed

+120
-51
lines changed

charts/library/common/Chart.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ sources:
4848
- https://hub.docker.com/_/
4949
- https://hub.docker.com/r/mikefarah/yq
5050
type: library
51-
version: 28.5.0
51+
version: 28.6.0

charts/library/common/templates/class/_horizontalPodAutoscaler.tpl

+23-38
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,41 @@ This template serves as a blueprint for horizontal pod autoscaler objects that a
33
using the common library.
44
*/}}
55
{{- define "tc.v1.common.class.hpa" -}}
6-
{{- $targetName := include "tc.v1.common.lib.chart.names.fullname" . -}}
7-
{{- $fullName := include "tc.v1.common.lib.chart.names.fullname" . -}}
8-
{{- $hpaName := $fullName -}}
9-
{{- $values := .Values.hpa -}}
6+
{{- $rootCtx := .rootCtx -}}
7+
{{- $objectData := .objectData -}}
108

11-
{{- if hasKey . "ObjectValues" -}}
12-
{{- with .ObjectValues.hpa -}}
13-
{{- $values = . -}}
14-
{{- end -}}
15-
{{- end -}}
16-
{{- $hpaLabels := $values.labels -}}
17-
{{- $hpaAnnotations := $values.annotations -}}
18-
19-
{{- if and (hasKey $values "nameOverride") $values.nameOverride -}}
20-
{{- $hpaName = printf "%v-%v" $hpaName $values.nameOverride -}}
21-
{{- end }}
9+
{{- $_ := set $objectData "updatePolicy" ($objectData.updatePolicy | default dict) -}}
10+
{{- $_ := set $objectData "resourcePolicy" ($objectData.resourcePolicy | default dict) }}
2211
---
2312
apiVersion: {{ include "tc.v1.common.capabilities.hpa.apiVersion" $ }}
2413
kind: HorizontalPodAutoscaler
2514
metadata:
26-
name: {{ $hpaName }}
27-
namespace: {{ $.Values.namespace | default $.Values.global.namespace | default $.Release.Namespace }}
28-
{{- $labels := (mustMerge ($hpaLabels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $ | fromYaml)) -}}
29-
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $ "labels" $labels) | trim) }}
15+
name: {{ $objectData.name }}
16+
namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "hpa") }}
17+
{{- $labels := (mustMerge ($objectData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}}
18+
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }}
3019
labels:
3120
{{- . | nindent 4 }}
3221
{{- end -}}
33-
{{- $annotations := (mustMerge ($hpaAnnotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $ | fromYaml)) -}}
34-
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $ "annotations" $annotations) | trim) }}
22+
{{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}}
23+
{{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }}
3524
annotations:
3625
{{- . | nindent 4 }}
37-
{{- end -}}
26+
{{- end }}
3827
spec:
3928
scaleTargetRef:
4029
apiVersion: apps/v1
41-
kind: {{ $values.targetKind | default ( include "tc.v1.common.names.controllerType" . ) }}
42-
name: {{ $values.target | default $targetName }}
43-
minReplicas: {{ $values.minReplicas | default 1 }}
44-
maxReplicas: {{ $values.maxReplicas | default 3 }}
30+
kind: {{ $objectData.workload.type }}
31+
name: {{ $objectData.name }}
32+
minReplicas: {{ $objectData.minReplicas | default 1 }}
33+
maxReplicas: {{ $objectData.maxReplicas | default 3 }}
34+
{{- with $objectData.metrics }}
4535
metrics:
46-
{{- if $values.targetCPUUtilizationPercentage }}
47-
- type: Resource
48-
resource:
49-
name: cpu
50-
targetAverageUtilization: {{ $values.targetCPUUtilizationPercentage }}
51-
{{- end -}}
52-
{{- if $values.targetMemoryUtilizationPercentage }}
53-
- type: Resource
54-
resource:
55-
name: memory
56-
targetAverageUtilization: {{ $values.targetMemoryUtilizationPercentage }}
57-
{{- end -}}
36+
{{- $objectData.metrics | toYaml | nindent 4 }}
37+
{{- end -}}
38+
39+
{{- with $objectData.behavior }}
40+
behavior:
41+
{{- $objectData.behavior | toYaml | nindent 4 }}
42+
{{- end -}}
5843
{{- end -}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{- define "tc.v1.common.lib.hpa.validation" -}}
2+
{{- $objectData := .objectData -}}
3+
{{- $rootCtx := .rootCtx -}}
4+
5+
{{- $updPolicy := $objectData.updatePolicy -}}
6+
7+
{{- end -}}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,58 @@
1-
{{/*
2-
Renders the configMap objects required by the chart.
1+
{{/* horizontal Pod Autoscaler Spawner */}}
2+
{{/* Call this template:
3+
{{ include "tc.v1.common.spawner.hpa" $ -}}
34
*/}}
5+
46
{{- define "tc.v1.common.spawner.hpa" -}}
5-
{{/* Generate named configMaps as required */}}
6-
{{- range $name, $hpa := .Values.horizontalPodAutoscaler -}}
7-
{{- if $hpa.enabled -}}
8-
{{- $hpaValues := $hpa -}}
9-
10-
{{/* set the default nameOverride to the hpa name */}}
11-
{{- if not $hpaValues.nameOverride -}}
12-
{{- $_ := set $hpaValues "nameOverride" $name -}}
7+
{{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}}
8+
{{- range $name, $hpa := .Values.hpa -}}
9+
{{- $enabledhpa := (include "tc.v1.common.lib.util.enabled" (dict
10+
"rootCtx" $ "objectData" $hpa
11+
"name" $name "caller" "horizontal Pod Autoscaler"
12+
"key" "hpa")) -}}
13+
14+
{{- if ne $enabledhpa "true" -}}{{- continue -}}{{- end -}}
15+
16+
{{- $objectData := (mustDeepCopy $hpa) -}}
17+
{{- $_ := set $objectData "hpaName" $name -}}
18+
{{- include "tc.v1.common.lib.hpa.validation" (dict "objectData" $objectData "rootCtx" $) -}}
19+
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $name) -}}
20+
21+
{{- range $workloadName, $workload := $.Values.workload -}}
22+
23+
{{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict
24+
"rootCtx" $ "objectData" $workload
25+
"name" $name "caller" "hpa"
26+
"key" "workload")) -}}
27+
28+
{{- if ne $enabled "true" -}}{{- continue -}}{{- end -}}
29+
30+
{{/* Create a copy of the workload */}}
31+
{{- $_ := set $objectData "workload" (mustDeepCopy $workload) -}}
32+
33+
{{/* Generate the name of the hpa */}}
34+
{{- $objectName := $fullname -}}
35+
{{- if not $objectData.workload.primary -}}
36+
{{- $objectName = printf "%s-%s" $fullname $workloadName -}}
1337
{{- end -}}
1438

15-
{{- $_ := set $ "ObjectValues" (dict "hpa" $hpaValues) -}}
16-
{{- include "tc.v1.common.class.hpa" $ -}}
39+
{{/* Perform validations */}}
40+
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $objectName) -}}
41+
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "horizontal Pod Autoscaler") -}}
42+
43+
{{/* Set the name of the workload */}}
44+
{{- $_ := set $objectData "name" $objectName -}}
45+
46+
{{/* Short name is the one that defined on the chart, used on selectors */}}
47+
{{- $_ := set $objectData "shortName" $workloadName -}}
48+
49+
{{- if or (not $objectData.targetSelector) (hasKey $objectData.targetSelector $workloadName) -}}
50+
{{/* Call class to create the object */}}
51+
{{- $types := (list "Deployment" "StatefulSet" "DaemonSet") -}}
52+
{{- if (mustHas $objectData.workload.type $types) -}}
53+
{{- include "tc.v1.common.class.hpa" (dict "rootCtx" $ "objectData" $objectData) -}}
54+
{{- end -}}
55+
{{- end -}}
1756
{{- end -}}
1857
{{- end -}}
1958
{{- end -}}

charts/library/common/values.yaml

+38
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,44 @@ vpa:
213213
memory: 20Gi
214214
controlledResources: ["cpu", "memory"]
215215

216+
# -- Horizontal pod autoscaler
217+
hpa:
218+
main:
219+
enabled: false
220+
targetSelector: []
221+
# minReplicas: 1
222+
# maxReplicas: 3
223+
224+
# metrics: # Optional, list of metric specs
225+
# - type: Resource # Can be Resource, Pods, Object, External, or ContainerResource
226+
# resource:
227+
# name: cpu
228+
# target:
229+
# type: Utilization # Or Value / AverageValue
230+
# averageUtilization: 50
231+
232+
# - type: Resource
233+
# resource:
234+
# name: memory
235+
# target:
236+
# type: AverageValue
237+
# averageValue: 500Mi
238+
239+
# behavior: # Optional: controls scaling behavior
240+
# scaleUp:
241+
# stabilizationWindowSeconds: 0
242+
# policies:
243+
# - type: Percent
244+
# value: 100
245+
# periodSeconds: 15
246+
# scaleDown:
247+
# stabilizationWindowSeconds: 300
248+
# policies:
249+
# - type: Pods
250+
# value: 4
251+
# periodSeconds: 60
252+
253+
216254
# -- (docs/service/README.md)
217255
service:
218256
main:

0 commit comments

Comments
 (0)