From 4fc33d4858ec42c34c05a783f6dec5c978793969 Mon Sep 17 00:00:00 2001 From: Matei Date: Tue, 9 Jan 2024 07:28:36 +0800 Subject: [PATCH] Avoid setting replicase while HPA is enabled (#181) * fix: avoid setting replicase while HPA is enabled * fix: add tests --- .../templates/_deployment_spec.tpl | 8 ++++- test/k8s_service_template_test.go | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/charts/k8s-service/templates/_deployment_spec.tpl b/charts/k8s-service/templates/_deployment_spec.tpl index 16b6443..b6a69c2 100644 --- a/charts/k8s-service/templates/_deployment_spec.tpl +++ b/charts/k8s-service/templates/_deployment_spec.tpl @@ -96,7 +96,13 @@ metadata: {{ toYaml . | indent 4 }} {{- end }} spec: - replicas: {{ if .isCanary }}{{ .Values.canary.replicaCount | default 1 }}{{ else }}{{ .Values.replicaCount }}{{ end }} +{{- if .isCanary }} + replicas: {{ .Values.canary.replicaCount | default 1 }} +{{ else }} +{{- if not .Values.horizontalPodAutoscaler.enabled }} + replicas: {{ .Values.replicaCount }} +{{- end }} +{{- end }} {{- if .Values.deploymentStrategy.enabled }} strategy: type: {{ .Values.deploymentStrategy.type }} diff --git a/test/k8s_service_template_test.go b/test/k8s_service_template_test.go index 80c28d1..7471460 100644 --- a/test/k8s_service_template_test.go +++ b/test/k8s_service_template_test.go @@ -1077,3 +1077,33 @@ func TestK8SServiceClusterIP(t *testing.T) { }) } } + +// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to not render replicas +func TestK8SServiceServiceHorizontalPodAutoscalerDoesNotConfigureReplicas(t *testing.T) { + t.Parallel() + + deployment := renderK8SServiceDeploymentWithSetValues( + t, + map[string]string{ + "horizontalPodAutoscaler.enabled": "true", + "replicaCount": "3", + }, + ) + assert.Equal(t, (*int32)(nil), deployment.Spec.Replicas) +} + +// Test that setting horizontalPodAutoscaler = true will not affect the canary +func TestK8SServiceServiceHorizontalPodAutoscalerDoesNotAffectCanaryConfiguration(t *testing.T) { + t.Parallel() + deployment := renderK8SServiceCanaryDeploymentWithSetValues( + t, + map[string]string{ + "canary.enabled": "true", + "canary.replicaCount": "6", + "canary.containerImage.repository": "nginx", + "canary.containerImage.tag": "1.16.0", + "horizontalPodAutoscaler.enabled": "true", + }, + ) + assert.EqualValues(t, 6, *deployment.Spec.Replicas) +}