From 1f3d7a0727a542be716c089933515f9ceabda158 Mon Sep 17 00:00:00 2001 From: "alexandre.barriere" Date: Thu, 5 Dec 2024 10:07:27 -0500 Subject: [PATCH 1/8] add optional hpa autoscaler support. --- .../aspnetcore/templates/deployment-hpa.yaml | 24 +++++++++++++++++++ charts/aspnetcore/values.yaml | 10 ++++++++ 2 files changed, 34 insertions(+) create mode 100644 charts/aspnetcore/templates/deployment-hpa.yaml diff --git a/charts/aspnetcore/templates/deployment-hpa.yaml b/charts/aspnetcore/templates/deployment-hpa.yaml new file mode 100644 index 0000000..260cc8e --- /dev/null +++ b/charts/aspnetcore/templates/deployment-hpa.yaml @@ -0,0 +1,24 @@ +{{- if (((.Values.deployment).autoscaling).enabled) }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ .Release.Name }}-hpa + annotations: + {{- with .Values.commonAnnotations }} + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + metrics: + - resource: + name: cpu + target: + averageUtilization: {{ .Values.deployment.autoscaling.targetCPUUtilizationPercentage }} + type: Utilization + type: Resource + minReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.deployment.autoscaling.minReplicas) }} + maxReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.deployment.autoscaling.minReplicas) (.Values.deployment.autoscaling.maxReplicas) }} + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ default (printf "%s-deployment" .Release.Name) .Values.deployment.name }} +{{- end }} diff --git a/charts/aspnetcore/values.yaml b/charts/aspnetcore/values.yaml index 4d60e91..0b3fcf9 100644 --- a/charts/aspnetcore/values.yaml +++ b/charts/aspnetcore/values.yaml @@ -66,9 +66,19 @@ ingress: ## ASP.NET Core deployment parameters ## @param deployment.name Name of the deployment, automatically generated from the release name if not specified +## ref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ +## @param deployment.autoscaling.enabled Default to false. Enable autoscaling +## @param deployment.autoscaling.minReplicas Optional if PodDistributionBudget is set. Minimum number of ASP.NET Core replicas minimum is PodDisruptionBudget.minAvailable + 1 +## @param deployment.autoscaling.maxReplicas Optional if PodDistributionBudget is set or minReplicas is set. Maximum number of ASP.NET Core replicas minimum is minReplicas value. +## @param deployment.autoscaling.targetCPUUtilizationPercentage Target CPU utilization percentage for autoscaling ## deployment: name: "" + autoscaling: + enabled: true + #minReplicas: 2 + #maxReplicas: 3 + targetCPUUtilizationPercentage: 80 ## ASP.NET Core containers' resource requests and limits defined in YAML ## ref: https://kubernetes.io/docs/user-guide/compute-resources/ From 1d9d705f4d6a31d7d7eac374c0a46749134b7f73 Mon Sep 17 00:00:00 2001 From: "alexandre.barriere" Date: Thu, 5 Dec 2024 10:09:58 -0500 Subject: [PATCH 2/8] chart version bump --- charts/aspnetcore/Chart.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/aspnetcore/Chart.yaml b/charts/aspnetcore/Chart.yaml index 6e2b392..352b6e1 100644 --- a/charts/aspnetcore/Chart.yaml +++ b/charts/aspnetcore/Chart.yaml @@ -1,7 +1,7 @@ apiVersion: v2 name: aspnetcore description: A generic Helm chart for ASP.NET Core services -version: 2.2.0 +version: 2.3.0 home: https://github.com/gsoft-inc/gsoft-helm-charts sources: - https://github.com/gsoft-inc/gsoft-helm-charts From f6428601090f67702b4e247def9f5698b360429d Mon Sep 17 00:00:00 2001 From: "alexandre.barriere" Date: Thu, 5 Dec 2024 10:16:01 -0500 Subject: [PATCH 3/8] disable hpa by default --- charts/aspnetcore/values.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/aspnetcore/values.yaml b/charts/aspnetcore/values.yaml index 0b3fcf9..ac57122 100644 --- a/charts/aspnetcore/values.yaml +++ b/charts/aspnetcore/values.yaml @@ -75,7 +75,7 @@ ingress: deployment: name: "" autoscaling: - enabled: true + enabled: false #minReplicas: 2 #maxReplicas: 3 targetCPUUtilizationPercentage: 80 From d03b4365d2d8ce47f5458142dac45f722eacac9c Mon Sep 17 00:00:00 2001 From: "alexandre.barriere" Date: Thu, 5 Dec 2024 14:31:12 -0500 Subject: [PATCH 4/8] unnested autoscaling --- .../aspnetcore/templates/deployment-hpa.yaml | 12 +++++----- charts/aspnetcore/values.yaml | 23 +++++++++++-------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/charts/aspnetcore/templates/deployment-hpa.yaml b/charts/aspnetcore/templates/deployment-hpa.yaml index 260cc8e..588cd98 100644 --- a/charts/aspnetcore/templates/deployment-hpa.yaml +++ b/charts/aspnetcore/templates/deployment-hpa.yaml @@ -1,9 +1,9 @@ -{{- if (((.Values.deployment).autoscaling).enabled) }} +{{- if ((.Values.autoscaling).enabled) }} apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: - name: {{ .Release.Name }}-hpa - annotations: + name: {{ default (printf "%s-deployment" .Release.Name) .Values.autoscaling.name }} + labels: {{- with .Values.commonAnnotations }} {{- toYaml . | nindent 4 }} {{- end }} @@ -12,11 +12,11 @@ spec: - resource: name: cpu target: - averageUtilization: {{ .Values.deployment.autoscaling.targetCPUUtilizationPercentage }} + averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} type: Utilization type: Resource - minReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.deployment.autoscaling.minReplicas) }} - maxReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.deployment.autoscaling.minReplicas) (.Values.deployment.autoscaling.maxReplicas) }} + minReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.autoscaling.minReplicas) }} + maxReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.autoscaling.minReplicas) (.Values.autoscaling.maxReplicas) }} scaleTargetRef: apiVersion: apps/v1 kind: Deployment diff --git a/charts/aspnetcore/values.yaml b/charts/aspnetcore/values.yaml index ac57122..ebdf9e5 100644 --- a/charts/aspnetcore/values.yaml +++ b/charts/aspnetcore/values.yaml @@ -66,19 +66,9 @@ ingress: ## ASP.NET Core deployment parameters ## @param deployment.name Name of the deployment, automatically generated from the release name if not specified -## ref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ -## @param deployment.autoscaling.enabled Default to false. Enable autoscaling -## @param deployment.autoscaling.minReplicas Optional if PodDistributionBudget is set. Minimum number of ASP.NET Core replicas minimum is PodDisruptionBudget.minAvailable + 1 -## @param deployment.autoscaling.maxReplicas Optional if PodDistributionBudget is set or minReplicas is set. Maximum number of ASP.NET Core replicas minimum is minReplicas value. -## @param deployment.autoscaling.targetCPUUtilizationPercentage Target CPU utilization percentage for autoscaling ## deployment: name: "" - autoscaling: - enabled: false - #minReplicas: 2 - #maxReplicas: 3 - targetCPUUtilizationPercentage: 80 ## ASP.NET Core containers' resource requests and limits defined in YAML ## ref: https://kubernetes.io/docs/user-guide/compute-resources/ @@ -180,6 +170,19 @@ azureWorkloadIdentity: podDisruptionBudget: minAvailable: 1 +## Autoscaling deployment settings +## ref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ +## @param autoscaling.enabled Default to false. Enable autoscaling +## @param autoscaling.minReplicas Optional if PodDistributionBudget is set. Minimum number of ASP.NET Core replicas minimum is PodDisruptionBudget.minAvailable + 1 +## @param autoscaling.maxReplicas Optional if PodDistributionBudget is set or minReplicas is set. Maximum number of ASP.NET Core replicas minimum is minReplicas value. +## @param autoscaling.targetCPUUtilizationPercentage Target CPU utilization percentage for autoscaling +autoscaling: + enabled: false + name: "" + minReplicas: 2 + maxReplicas: 3 + targetCPUUtilizationPercentage: 80 + ## @param extraVolumes Optionally specify extra list of additional volumes, e.g: ## extraVolumes: ## - name: config-vol From a0aa7d01e33236d46356494c4046b74039c9cb84 Mon Sep 17 00:00:00 2001 From: "alexandre.barriere" Date: Thu, 5 Dec 2024 14:48:55 -0500 Subject: [PATCH 5/8] miss understood the comment --- charts/aspnetcore/templates/deployment-hpa.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/charts/aspnetcore/templates/deployment-hpa.yaml b/charts/aspnetcore/templates/deployment-hpa.yaml index 588cd98..0e9cc2e 100644 --- a/charts/aspnetcore/templates/deployment-hpa.yaml +++ b/charts/aspnetcore/templates/deployment-hpa.yaml @@ -4,6 +4,11 @@ kind: HorizontalPodAutoscaler metadata: name: {{ default (printf "%s-deployment" .Release.Name) .Values.autoscaling.name }} labels: + {{- include "aspnetcore.standardLabels" . | nindent 4 }} + {{- if .Values.commonLabels }} + {{- toYaml .Values.commonLabels | nindent 4 }} + {{- end }} + annotations: {{- with .Values.commonAnnotations }} {{- toYaml . | nindent 4 }} {{- end }} From 1e04936bc98c87c45d4d236c5fccad187b3d811b Mon Sep 17 00:00:00 2001 From: Alex Barriere Date: Fri, 6 Dec 2024 10:44:33 -0500 Subject: [PATCH 6/8] Update charts/aspnetcore/templates/deployment-hpa.yaml Co-authored-by: Guillaume Caya-Letourneau --- charts/aspnetcore/templates/deployment-hpa.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/aspnetcore/templates/deployment-hpa.yaml b/charts/aspnetcore/templates/deployment-hpa.yaml index 0e9cc2e..d6c8c8e 100644 --- a/charts/aspnetcore/templates/deployment-hpa.yaml +++ b/charts/aspnetcore/templates/deployment-hpa.yaml @@ -2,7 +2,7 @@ apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: - name: {{ default (printf "%s-deployment" .Release.Name) .Values.autoscaling.name }} + name: {{ default (printf "%s-hpa" .Release.Name) .Values.autoscaling.name }} labels: {{- include "aspnetcore.standardLabels" . | nindent 4 }} {{- if .Values.commonLabels }} From 873e259de9365057c2f61625f51685636d9e7b4b Mon Sep 17 00:00:00 2001 From: "alexandre.barriere" Date: Fri, 6 Dec 2024 10:54:27 -0500 Subject: [PATCH 7/8] use prechecks instead of calculating them in templates --- charts/aspnetcore/templates/deployment-hpa.yaml | 4 ++-- charts/aspnetcore/templates/prechecks.tpl | 9 +++++++++ charts/aspnetcore/values.yaml | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 charts/aspnetcore/templates/prechecks.tpl diff --git a/charts/aspnetcore/templates/deployment-hpa.yaml b/charts/aspnetcore/templates/deployment-hpa.yaml index d6c8c8e..727d892 100644 --- a/charts/aspnetcore/templates/deployment-hpa.yaml +++ b/charts/aspnetcore/templates/deployment-hpa.yaml @@ -20,8 +20,8 @@ spec: averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} type: Utilization type: Resource - minReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.autoscaling.minReplicas) }} - maxReplicas: {{ max (add ((.Values.podDisruptionBudget).minAvailable) 1) (.Values.autoscaling.minReplicas) (.Values.autoscaling.maxReplicas) }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} scaleTargetRef: apiVersion: apps/v1 kind: Deployment diff --git a/charts/aspnetcore/templates/prechecks.tpl b/charts/aspnetcore/templates/prechecks.tpl new file mode 100644 index 0000000..bc3a0f8 --- /dev/null +++ b/charts/aspnetcore/templates/prechecks.tpl @@ -0,0 +1,9 @@ +{{- if (.Values.autoscaling).enabled }} + {{- if not (.Values.autoscaling).minReplicas }} + {{- fail "autoscaling.minReplicas is required" }} + {{- else}} + {{- if le (.Values.autoscaling).minReplicas (.Values.podDisruptionBudget).minAvailable }} + {{- fail "autoscaling.minReplicas cannot be less than podDisruptionBudget.minAvailable" }} + {{- end }} + {{- end }} +{{- end }} diff --git a/charts/aspnetcore/values.yaml b/charts/aspnetcore/values.yaml index ebdf9e5..7594d4d 100644 --- a/charts/aspnetcore/values.yaml +++ b/charts/aspnetcore/values.yaml @@ -177,7 +177,7 @@ podDisruptionBudget: ## @param autoscaling.maxReplicas Optional if PodDistributionBudget is set or minReplicas is set. Maximum number of ASP.NET Core replicas minimum is minReplicas value. ## @param autoscaling.targetCPUUtilizationPercentage Target CPU utilization percentage for autoscaling autoscaling: - enabled: false + enabled: true name: "" minReplicas: 2 maxReplicas: 3 From c1b375feb5b7348b2a47a779a9a49693cbb57cf3 Mon Sep 17 00:00:00 2001 From: "alexandre.barriere" Date: Mon, 9 Dec 2024 09:00:47 -0500 Subject: [PATCH 8/8] removed nullcheck for default value in template --- charts/aspnetcore/templates/prechecks.tpl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/charts/aspnetcore/templates/prechecks.tpl b/charts/aspnetcore/templates/prechecks.tpl index bc3a0f8..28fffcc 100644 --- a/charts/aspnetcore/templates/prechecks.tpl +++ b/charts/aspnetcore/templates/prechecks.tpl @@ -1,8 +1,8 @@ -{{- if (.Values.autoscaling).enabled }} - {{- if not (.Values.autoscaling).minReplicas }} +{{- if .Values.autoscaling.enabled }} + {{- if not .Values.autoscaling.minReplicas }} {{- fail "autoscaling.minReplicas is required" }} {{- else}} - {{- if le (.Values.autoscaling).minReplicas (.Values.podDisruptionBudget).minAvailable }} + {{- if le (int .Values.autoscaling.minReplicas) (int .Values.podDisruptionBudget.minAvailable) }} {{- fail "autoscaling.minReplicas cannot be less than podDisruptionBudget.minAvailable" }} {{- end }} {{- end }}