Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KEDA task count query should ignore k8s queue #17433

Merged
merged 3 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion chart/templates/workers/worker-kedaautoscaler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ spec:
query: >-
SELECT ceil(COUNT(*)::decimal / {{ .Values.config.celery.worker_concurrency }})
FROM task_instance
WHERE state='running' OR state='queued'
WHERE (state='running' OR state='queued')
{{- if eq .Values.executor "CeleryKubernetesExecutor" }}
AND queue != '{{ .Values.config.celery_kubernetes_executor.kubernetes_queue }}'
{{- end }}
{{- end }}
50 changes: 44 additions & 6 deletions chart/tests/test_keda.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import jmespath
import pytest
from parameterized import parameterized

from tests.helm_template_generator import render_chart
Expand Down Expand Up @@ -51,17 +52,29 @@ def test_keda_enabled(self, executor, is_created):
else:
assert docs == []

@parameterized.expand(
@staticmethod
def build_query(executor, concurrency=16, queue=None):
"""Builds the query used by KEDA autoscaler to determine how many workers there should be"""
query = (
f"SELECT ceil(COUNT(*)::decimal / {concurrency}) "
"FROM task_instance WHERE (state='running' OR state='queued')"
)
if executor == 'CeleryKubernetesExecutor':
query += f" AND queue != '{queue or 'kubernetes'}'"
return query

@pytest.mark.parametrize(
'executor,concurrency',
[
("CeleryExecutor", 8),
("CeleryExecutor", 16),
("CeleryKubernetesExecutor", 8),
("CeleryKubernetesExecutor", 16),
]
],
)
def test_keda_concurrency(self, executor, concurrency):
"""
ScaledObject should only be created when set to enabled and executor is Celery or CeleryKubernetes
Verify keda sql query uses configured concurrency
"""
docs = render_chart(
values={
Expand All @@ -71,10 +84,35 @@ def test_keda_concurrency(self, executor, concurrency):
},
show_only=["templates/workers/worker-kedaautoscaler.yaml"],
)
expected_query = (
f"SELECT ceil(COUNT(*)::decimal / {concurrency}) "
"FROM task_instance WHERE state='running' OR state='queued'"
expected_query = self.build_query(executor=executor, concurrency=concurrency)
assert jmespath.search("spec.triggers[0].metadata.query", docs[0]) == expected_query

@pytest.mark.parametrize(
'executor,queue,should_filter',
[
("CeleryExecutor", None, False),
("CeleryExecutor", 'my_queue', False),
("CeleryKubernetesExecutor", None, True),
("CeleryKubernetesExecutor", 'my_queue', True),
],
)
def test_keda_query_kubernetes_queue(self, executor, queue, should_filter):
"""
Verify keda sql query ignores kubernetes queue when CKE is used.
Sometimes a user might want to use a different queue name for k8s executor tasks,
and we also verify here that we use the configured queue name in that case.
"""
values = {
"workers": {"keda": {"enabled": True}, "persistence": {"enabled": False}},
"executor": executor,
}
if queue:
values.update({'config': {'celery_kubernetes_executor': {'kubernetes_queue': queue}}})
docs = render_chart(
values=values,
show_only=["templates/workers/worker-kedaautoscaler.yaml"],
)
expected_query = self.build_query(executor=executor, queue=queue)
assert jmespath.search("spec.triggers[0].metadata.query", docs[0]) == expected_query

@parameterized.expand(
Expand Down
2 changes: 2 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,8 @@ config:
reinit_frequency: '{{ .Values.kerberos.reinitFrequency }}'
principal: '{{ .Values.kerberos.principal }}'
ccache: '{{ .Values.kerberos.ccacheMountPath }}/{{ .Values.kerberos.ccacheFileName }}'
celery_kubernetes_executor:
kubernetes_queue: 'kubernetes'
kubernetes:
namespace: '{{ .Release.Namespace }}'
airflow_configmap: '{{ include "airflow_config" . }}'
Expand Down