-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable HPA for Airflow Webserver (#41955)
- Loading branch information
1 parent
2ebd8b5
commit 39164a3
Showing
6 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{{/* | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
*/}} | ||
|
||
################################ | ||
## Airflow Webserver HPA | ||
################################# | ||
{{- if and (.Values.webserver.enabled) (.Values.webserver.hpa.enabled) }} | ||
apiVersion: autoscaling/v2 | ||
kind: HorizontalPodAutoscaler | ||
metadata: | ||
name: {{ include "airflow.fullname" . }}-webserver | ||
labels: | ||
tier: airflow | ||
component: webserver-horizontalpodautoscaler | ||
release: {{ .Release.Name }} | ||
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" | ||
heritage: {{ .Release.Service }} | ||
deploymentName: {{ .Release.Name }}-webserver | ||
{{- if or (.Values.labels) (.Values.webserver.labels) }} | ||
{{- mustMerge .Values.webserver.labels .Values.labels | toYaml | nindent 4 }} | ||
{{- end }} | ||
spec: | ||
scaleTargetRef: | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
name: {{ include "airflow.fullname" . }}-webserver | ||
minReplicas: {{ .Values.webserver.hpa.minReplicaCount }} | ||
maxReplicas: {{ .Values.webserver.hpa.maxReplicaCount }} | ||
metrics: {{- toYaml .Values.webserver.hpa.metrics | nindent 4 }} | ||
{{- with .Values.webserver.hpa.behavior }} | ||
behavior: {{- toYaml . | nindent 4 }} | ||
{{- end }} | ||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from __future__ import annotations | ||
|
||
import jmespath | ||
import pytest | ||
|
||
from tests.charts.helm_template_generator import render_chart | ||
|
||
|
||
class TestWebserverHPA: | ||
"""Tests HPA.""" | ||
|
||
def test_hpa_disabled_by_default(self): | ||
"""Disabled by default.""" | ||
docs = render_chart( | ||
values={}, | ||
show_only=["templates/webserver/webserver-hpa.yaml"], | ||
) | ||
assert docs == [] | ||
|
||
def test_should_add_component_specific_labels(self): | ||
docs = render_chart( | ||
values={ | ||
"webserver": { | ||
"hpa": {"enabled": True}, | ||
"labels": {"test_label": "test_label_value"}, | ||
}, | ||
}, | ||
show_only=["templates/webserver/webserver-hpa.yaml"], | ||
) | ||
|
||
assert "test_label" in jmespath.search("metadata.labels", docs[0]) | ||
assert jmespath.search("metadata.labels", docs[0])["test_label"] == "test_label_value" | ||
|
||
@pytest.mark.parametrize( | ||
"min_replicas, max_replicas", | ||
[ | ||
(None, None), | ||
(2, 8), | ||
], | ||
) | ||
def test_min_max_replicas(self, min_replicas, max_replicas): | ||
"""Verify minimum and maximum replicas.""" | ||
docs = render_chart( | ||
values={ | ||
"webserver": { | ||
"hpa": { | ||
"enabled": True, | ||
**({"minReplicaCount": min_replicas} if min_replicas else {}), | ||
**({"maxReplicaCount": max_replicas} if max_replicas else {}), | ||
} | ||
}, | ||
}, | ||
show_only=["templates/webserver/webserver-hpa.yaml"], | ||
) | ||
assert jmespath.search("spec.minReplicas", docs[0]) == 1 if min_replicas is None else min_replicas | ||
assert jmespath.search("spec.maxReplicas", docs[0]) == 5 if max_replicas is None else max_replicas | ||
|
||
def test_hpa_behavior(self): | ||
"""Verify HPA behavior.""" | ||
expected_behavior = { | ||
"scaleDown": { | ||
"stabilizationWindowSeconds": 300, | ||
"policies": [{"type": "Percent", "value": 100, "periodSeconds": 15}], | ||
} | ||
} | ||
docs = render_chart( | ||
values={ | ||
"webserver": { | ||
"hpa": { | ||
"enabled": True, | ||
"behavior": expected_behavior, | ||
}, | ||
}, | ||
}, | ||
show_only=["templates/webserver/webserver-hpa.yaml"], | ||
) | ||
assert jmespath.search("spec.behavior", docs[0]) == expected_behavior | ||
|
||
@pytest.mark.parametrize( | ||
"metrics, expected_metrics", | ||
[ | ||
# default metrics | ||
( | ||
None, | ||
{ | ||
"type": "Resource", | ||
"resource": {"name": "cpu", "target": {"type": "Utilization", "averageUtilization": 80}}, | ||
}, | ||
), | ||
# custom metric | ||
( | ||
[ | ||
{ | ||
"type": "Pods", | ||
"pods": { | ||
"metric": {"name": "custom"}, | ||
"target": {"type": "Utilization", "averageUtilization": 80}, | ||
}, | ||
} | ||
], | ||
{ | ||
"type": "Pods", | ||
"pods": { | ||
"metric": {"name": "custom"}, | ||
"target": {"type": "Utilization", "averageUtilization": 80}, | ||
}, | ||
}, | ||
), | ||
], | ||
) | ||
def test_should_use_hpa_metrics(self, metrics, expected_metrics): | ||
docs = render_chart( | ||
values={ | ||
"webserver": { | ||
"hpa": {"enabled": True, **({"metrics": metrics} if metrics else {})}, | ||
}, | ||
}, | ||
show_only=["templates/webserver/webserver-hpa.yaml"], | ||
) | ||
assert expected_metrics == jmespath.search("spec.metrics[0]", docs[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters