From 3b7bf8d112fc60897ca5c3f93087c6de22bfbf6f Mon Sep 17 00:00:00 2001 From: Swalloow Date: Sun, 5 Jun 2022 19:56:06 +0900 Subject: [PATCH 1/2] feat: add support for KEDA HPA config to Helm Chart --- .../workers/worker-kedaautoscaler.yaml | 4 + chart/values.schema.json | 85 +++++++++++++++++++ chart/values.yaml | 11 +++ tests/charts/test_keda.py | 44 ++++++++++ 4 files changed, 144 insertions(+) diff --git a/chart/templates/workers/worker-kedaautoscaler.yaml b/chart/templates/workers/worker-kedaautoscaler.yaml index 35965526a8a94..9fe869faccb52 100644 --- a/chart/templates/workers/worker-kedaautoscaler.yaml +++ b/chart/templates/workers/worker-kedaautoscaler.yaml @@ -41,6 +41,10 @@ spec: cooldownPeriod: {{ .Values.workers.keda.cooldownPeriod }} # Optional. Default: 300 seconds minReplicaCount: {{ .Values.workers.keda.minReplicaCount }} # Optional. Default: 0 maxReplicaCount: {{ .Values.workers.keda.maxReplicaCount }} # Optional. Default: 100 +{{- if .Values.workers.keda.advanced }} + advanced: +{{ toYaml .Values.workers.keda.advanced | indent 4 }} +{{- end }} triggers: - type: postgresql metadata: diff --git a/chart/values.schema.json b/chart/values.schema.json index 1dbfdd2f2e3f0..bd0ac13dcfc22 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -1249,6 +1249,27 @@ "description": "Maximum number of workers created by KEDA.", "type": "integer", "default": 10 + }, + "advanced": { + "description": "Advanced KEDA configuration.", + "type": "object", + "default": {}, + "additionalProperties": false, + "properties": { + "horizontalPodAutoscalerConfig": { + "description": "HorizontalPodAutoscalerConfig specifies horizontal scale config.", + "type": "object", + "default": {}, + "properties": { + "behavior": { + "description": "HorizontalPodAutoscalerBehavior configures the scaling behavior of the target.", + "type": "object", + "default": {}, + "$ref": "#/definitions/io.k8s.api.autoscaling.v2beta2.HorizontalPodAutoscalerBehavior" + } + } + } + } } } }, @@ -4608,6 +4629,70 @@ "type": "object", "additionalProperties": false }, + "io.k8s.api.autoscaling.v2beta2.HPAScalingPolicy": { + "description": "HPAScalingPolicy is a single policy which must hold true for a specified past interval.", + "properties": { + "periodSeconds": { + "description": "PeriodSeconds specifies the window of time for which the policy should hold true. PeriodSeconds must be greater than zero and less than or equal to 1800 (30 min).", + "format": "int32", + "type": "integer" + }, + "type": { + "description": "Type is used to specify the scaling policy.", + "type": "string" + }, + "value": { + "description": "Value contains the amount of change which is permitted by the policy. It must be greater than zero", + "format": "int32", + "type": "integer" + } + }, + "required": [ + "type", + "value", + "periodSeconds" + ], + "type": "object", + "additionalProperties": false + }, + "io.k8s.api.autoscaling.v2beta2.HPAScalingRules": { + "description": "HPAScalingRules configures the scaling behavior for one direction. These Rules are applied after calculating DesiredReplicas from metrics for the HPA. They can limit the scaling velocity by specifying scaling policies. They can prevent flapping by specifying the stabilization window, so that the number of replicas is not set instantly, instead, the safest value from the stabilization window is chosen.", + "properties": { + "policies": { + "description": "policies is a list of potential scaling polices which can be used during scaling. At least one policy must be specified, otherwise the HPAScalingRules will be discarded as invalid", + "items": { + "$ref": "#/definitions/io.k8s.api.autoscaling.v2beta2.HPAScalingPolicy" + }, + "type": "array" + }, + "selectPolicy": { + "description": "selectPolicy is used to specify which policy should be used. If not set, the default value MaxPolicySelect is used.", + "type": "string" + }, + "stabilizationWindowSeconds": { + "description": "StabilizationWindowSeconds is the number of seconds for which past recommendations should be considered while scaling up or scaling down. StabilizationWindowSeconds must be greater than or equal to zero and less than or equal to 3600 (one hour). If not set, use the default values: - For scale up: 0 (i.e. no stabilization is done). - For scale down: 300 (i.e. the stabilization window is 300 seconds long).", + "format": "int32", + "type": "integer" + } + }, + "type": "object", + "additionalProperties": false + }, + "io.k8s.api.autoscaling.v2beta2.HorizontalPodAutoscalerBehavior": { + "description": "HorizontalPodAutoscalerBehavior configures the scaling behavior of the target in both Up and Down directions (scaleUp and scaleDown fields respectively).", + "properties": { + "scaleDown": { + "$ref": "#/definitions/io.k8s.api.autoscaling.v2beta2.HPAScalingRules", + "description": "scaleDown is scaling policy for scaling Down. If not set, the default value is to allow to scale down to minReplicas pods, with a 300 second stabilization window (i.e., the highest recommendation for the last 300sec is used)." + }, + "scaleUp": { + "$ref": "#/definitions/io.k8s.api.autoscaling.v2beta2.HPAScalingRules", + "description": "scaleUp is scaling policy for scaling Up. If not set, the default value is the higher of:\n * increase no more than 4 pods per 60 seconds\n * double the number of pods per 60 seconds\nNo stabilization is used." + } + }, + "type": "object", + "additionalProperties": false + }, "io.k8s.api.core.v1.AWSElasticBlockStoreVolumeSource": { "description": "Represents a Persistent Disk resource in AWS.\n\nAn AWS EBS disk must exist before mounting to a container. The disk must also be in the same AWS zone as the kubelet. An AWS EBS disk can only be mounted as read/write once. AWS EBS volumes support ownership management and SELinux relabeling.", "properties": { diff --git a/chart/values.yaml b/chart/values.yaml index 6b1ca62970e04..9300abd090276 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -472,6 +472,17 @@ workers: # Maximum number of workers created by keda maxReplicaCount: 10 + # Specify HPA related options + advanced: {} + # horizontalPodAutoscalerConfig: + # behavior: + # scaleDown: + # stabilizationWindowSeconds: 300 + # policies: + # - type: Percent + # value: 100 + # periodSeconds: 15 + persistence: # Enable persistent volumes enabled: true diff --git a/tests/charts/test_keda.py b/tests/charts/test_keda.py index 43acd3996d89d..823681a5af42b 100644 --- a/tests/charts/test_keda.py +++ b/tests/charts/test_keda.py @@ -52,6 +52,50 @@ def test_keda_enabled(self, executor, is_created): else: assert docs == [] + @parameterized.expand( + [ + ('CeleryExecutor'), + ('CeleryKubernetesExecutor'), + ] + ) + def test_keda_advanced(self, executor): + """ + Verify keda advanced config. + """ + expected_advanced = { + "horizontalPodAutoscalerConfig": { + "behavior": { + "scaleDown": { + "stabilizationWindowSeconds": 300, + "policies": [{"type": "Percent", "value": 100, "periodSeconds": 15}], + } + } + } + } + docs = render_chart( + values={ + "workers": { + "keda": { + "enabled": True, + "advanced": { + "horizontalPodAutoscalerConfig": { + "behavior": { + "scaleDown": { + "stabilizationWindowSeconds": 300, + "policies": [{"type": "Percent", "value": 100, "periodSeconds": 15}], + } + } + } + }, + }, + "persistence": {"enabled": False}, + }, + 'executor': executor, + }, + show_only=["templates/workers/worker-kedaautoscaler.yaml"], + ) + assert jmespath.search("spec.advanced", docs[0]) == expected_advanced + @staticmethod def build_query(executor, concurrency=16, queue=None): """Builds the query used by KEDA autoscaler to determine how many workers there should be""" From c80934af42909147c2d3911aab8035fd6f186fe2 Mon Sep 17 00:00:00 2001 From: Swalloow Date: Thu, 30 Jun 2022 18:32:47 +0900 Subject: [PATCH 2/2] fix: apply keda test suggestion --- tests/charts/test_keda.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tests/charts/test_keda.py b/tests/charts/test_keda.py index 823681a5af42b..e897378a9770a 100644 --- a/tests/charts/test_keda.py +++ b/tests/charts/test_keda.py @@ -77,20 +77,10 @@ def test_keda_advanced(self, executor): "workers": { "keda": { "enabled": True, - "advanced": { - "horizontalPodAutoscalerConfig": { - "behavior": { - "scaleDown": { - "stabilizationWindowSeconds": 300, - "policies": [{"type": "Percent", "value": 100, "periodSeconds": 15}], - } - } - } - }, + "advanced": expected_advanced, }, - "persistence": {"enabled": False}, }, - 'executor': executor, + "executor": executor, }, show_only=["templates/workers/worker-kedaautoscaler.yaml"], )