Skip to content

Commit

Permalink
feat: create new hpa template using ContainerResource
Browse files Browse the repository at this point in the history
If Kube version is >=1.3.0, we switch the HPA template to use ContainerResource types instead of Resource.
We also add the 'autoscaling.extraMetrics' which shall enable adding scaling for sidecar containers.
  • Loading branch information
David-Wobrock committed Jan 28, 2025
1 parent f4ada24 commit ccbcf56
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 4 deletions.
15 changes: 15 additions & 0 deletions hacks/values/oathkeeper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ deployment:
behavior:
scaleDown:
stabilizationWindowSeconds: 60
extraMetrics:
- type: ContainerResource
containerResource:
name: cpu
container: sidecar
target:
type: Utilization
averageUtilization: 80
- type: ContainerResource
containerResource:
name: memory
container: sidecar
target:
type: Utilization
averageUtilization: 80
strategy:
type: RollingUpdate
rollingUpdate:
Expand Down
43 changes: 43 additions & 0 deletions helm/charts/hydra/templates/hpa-controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- if and .Values.deployment.autoscaling.enabled $containerResourceAvailable }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
{{- if .Release.Namespace }}
namespace: {{ .Release.Namespace }}
{{- end }}
name: {{ include "hydra.fullname" . }}
labels:
{{- include "hydra.labels" . | nindent 4 }}
spec:
{{- with .Values.deployment.autoscaling.behavior }}
behavior: {{- toYaml . | nindent 4 }}
{{- end }}
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "hydra.fullname" . }}
minReplicas: {{ .Values.deployment.autoscaling.minReplicas }}
maxReplicas: {{ .Values.deployment.autoscaling.maxReplicas }}
metrics:
{{- with .Values.deployment.autoscaling.targetMemory }}
- type: ContainerResource
containerResource:
name: memory
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.deployment.autoscaling.targetCPU}}
- type: ContainerResource
containerResource:
name: cpu
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if not (empty .Values.deployment.autoscaling.extraMetrics) }}
{{- toYaml .Values.deployment.autoscaling.extraMetrics | nindent 2 }}
{{- end }}
{{- end }}
4 changes: 3 additions & 1 deletion helm/charts/hydra/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{- if .Values.deployment.autoscaling.enabled }}
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- if and .Values.deployment.autoscaling.enabled (not $containerResourceAvailable) }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
Expand Down
3 changes: 3 additions & 0 deletions helm/charts/hydra/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ deployment:
# -- Set custom behavior
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#configurable-scaling-behavior
behavior: {}
# -- Add extraContainer container resource metrics
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics
extraMetrics: []

# -- Default probe timers
readinessProbe:
Expand Down
45 changes: 45 additions & 0 deletions helm/charts/keto/templates/hpa-controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- $autoscaling := ternary .Values.deployment.autoscaling .Values.autoscaling (not (empty .Values.deployment.autoscaling )) -}}

{{- if and $autoscaling.enabled $containerResourceAvailable }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "keto.fullname" . }}
{{- if .Release.Namespace }}
namespace: {{ .Release.Namespace }}
{{- end }}
labels:
{{- include "keto.labels" . | nindent 4 }}
spec:
{{- with $autoscaling.behavior }}
behavior: {{- toYaml . | nindent 4 }}
{{- end }}
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "keto.fullname" . }}
minReplicas: {{ $autoscaling.minReplicas }}
maxReplicas: {{ $autoscaling.maxReplicas }}
metrics:
{{- with $autoscaling.targetMemory }}
- type: ContainerResource
containerResource:
name: memory
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with $autoscaling.targetCPU}}
- type: ContainerResource
containerResource:
name: cpu
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if not (empty $autoscaling.extraMetrics) }}
{{- toYaml $autoscaling.extraMetrics | nindent 2 }}
{{- end }}
{{- end }}
4 changes: 3 additions & 1 deletion helm/charts/keto/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- $autoscaling := ternary .Values.deployment.autoscaling .Values.autoscaling (not (empty .Values.deployment.autoscaling )) -}}

{{- if $autoscaling.enabled }}
{{- if and $autoscaling.enabled (not $containerResourceAvailable) }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
Expand Down
3 changes: 3 additions & 0 deletions helm/charts/keto/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ deployment:
# -- Set custom behavior
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#configurable-scaling-behavior
behavior: {}
# -- Add extraContainer container resource metrics
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics
extraMetrics: []

nodeSelector: {}

Expand Down
40 changes: 40 additions & 0 deletions helm/charts/kratos/templates/hpa-kratos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- if and .Values.autoscaling.enabled $containerResourceAvailable }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "kratos.fullname" . }}
labels:
{{- include "kratos.labels" . | nindent 4 }}
spec:
{{- with .Values.autoscaling.behavior }}
behavior: {{- toYaml . | nindent 4 }}
{{- end }}
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "kratos.fullname" . }}
minReplicas: {{ .Values.autoscaling.minReplicas }}
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
metrics:
{{- with .Values.autoscaling.targetMemory }}
- type: ContainerResource
containerResource:
name: memory
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.autoscaling.targetCPU}}
- type: ContainerResource
containerResource:
name: cpu
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if not (empty $.Values.autoscaling.extraMetrics) }}
{{- toYaml .Values.autoscaling.extraMetrics | nindent 2 }}
{{- end }}
{{- end }}
4 changes: 3 additions & 1 deletion helm/charts/kratos/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{- if .Values.autoscaling.enabled }}
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- if and .Values.autoscaling.enabled (not $containerResourceAvailable) }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
Expand Down
3 changes: 3 additions & 0 deletions helm/charts/kratos/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ autoscaling:
# -- Set custom behavior
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#configurable-scaling-behavior
behavior: {}
# -- Add extraContainer container resource metrics
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics
extraMetrics: []

## -- Values for initialization job
job:
Expand Down
4 changes: 3 additions & 1 deletion helm/charts/oathkeeper/templates/hpa-controller.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{- if .Values.deployment.autoscaling.enabled }}
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- if and .Values.deployment.autoscaling.enabled (not $containerResourceAvailable) }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
Expand Down
43 changes: 43 additions & 0 deletions helm/charts/oathkeeper/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{- $kubeVersion := .Capabilities.KubeVersion.GitVersion -}}
{{- $containerResourceAvailable := and (not (empty $kubeVersion)) (semverCompare ">=1.3.0" $kubeVersion) -}}
{{- if and .Values.deployment.autoscaling.enabled $containerResourceAvailable }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "oathkeeper.fullname" . }}
{{- if .Release.Namespace }}
namespace: {{ .Release.Namespace }}
{{- end }}
labels:
{{- include "oathkeeper.labels" . | nindent 4 }}
spec:
{{- with .Values.deployment.autoscaling.behavior }}
behavior: {{- toYaml . | nindent 4 }}
{{- end }}
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "oathkeeper.fullname" . }}
minReplicas: {{ .Values.deployment.autoscaling.minReplicas }}
maxReplicas: {{ .Values.deployment.autoscaling.maxReplicas }}
metrics:
{{- with .Values.deployment.autoscaling.targetMemory }}
- type: ContainerResource
containerResource:
name: memory
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.deployment.autoscaling.targetCPU}}
- type: ContainerResource
containerResource:
name: cpu
container: {{ $.Chart.Name }}
target:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if not (empty $.Values.deployment.autoscaling.extraMetrics) }}
{{- toYaml .Values.deployment.autoscaling.extraMetrics | nindent 2 }}
{{- end }}
{{- end }}
3 changes: 3 additions & 0 deletions helm/charts/oathkeeper/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ deployment:
# -- Set custom behavior
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#configurable-scaling-behavior
behavior: {}
# -- Add extraContainer container resource metrics
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics
extraMetrics: []

# -- Configure node affinity
affinity: {}
Expand Down

0 comments on commit ccbcf56

Please sign in to comment.