This is an automated email from the ASF dual-hosted git repository.

Miretpl pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new bada0686efe Allow authenticationRef method in database connection 
(#75220)
bada0686efe is described below

commit bada0686efe7173f5220bee2787df33983a706e7
Author: Ana Loureiro <[email protected]>
AuthorDate: Mon Nov 16 21:55:56 2026 +0000

    Allow authenticationRef method in database connection (#75220)
    
    * feat: allow authenticationRef in database connection
    
    * fix: remove env for database connection when not in use
    
    * Apply suggestion from @HenkvanDyk
    
    Co-authored-by: Henk van Dyk <[email protected]>
    
    ---------
    
    Co-authored-by: Henk van Dyk <[email protected]>
---
 chart/templates/_helpers.yaml                      |  2 +-
 chart/templates/workers/worker-deployment.yaml     |  2 +-
 chart/templates/workers/worker-kedaautoscaler.yaml | 10 +++
 chart/tests/helm_tests/other/test_keda.py          | 88 ++++++++++++++++++++++
 chart/values.schema.json                           | 26 +++++++
 chart/values.yaml                                  | 11 +++
 6 files changed, 137 insertions(+), 2 deletions(-)

diff --git a/chart/templates/_helpers.yaml b/chart/templates/_helpers.yaml
index 4ddbeecafcb..7e6c1fb549e 100644
--- a/chart/templates/_helpers.yaml
+++ b/chart/templates/_helpers.yaml
@@ -210,7 +210,7 @@ A backend set in a way the chart cannot see counts as 
unset, which only leaves t
 
 {{/* KEDA scaler database connection for the worker and triggerer autoscalers 
*/}}
 {{- define "keda_airflow_environment" }}
-  {{- if or (eq .Values.data.metadataConnection.protocol "mysql") (and 
.Values.pgbouncer.enabled (not .UsePgbouncer)) }}
+  {{- if and (not .AuthenticationRef) (or (eq 
.Values.data.metadataConnection.protocol "mysql") (and 
.Values.pgbouncer.enabled (not .UsePgbouncer))) }}
   - name: KEDA_DB_CONN
     valueFrom:
       secretKeyRef:
diff --git a/chart/templates/workers/worker-deployment.yaml 
b/chart/templates/workers/worker-deployment.yaml
index 661a801862d..503f255b5d7 100644
--- a/chart/templates/workers/worker-deployment.yaml
+++ b/chart/templates/workers/worker-deployment.yaml
@@ -355,8 +355,8 @@ spec:
             {{- include "custom_airflow_environment" . | indent 10 }}
             {{- include "standard_airflow_environment" . | indent 10 }}
             {{- if $keda }}
+            {{- include "keda_airflow_environment" (merge (dict "UsePgbouncer" 
.Values.workers.celery.keda.usePgbouncer "AuthenticationRef" 
.Values.workers.celery.keda.authenticationRef) .) | indent 10 }}
             {{- include "metadata_db_environment" . | indent 10 }}
-            {{- include "keda_airflow_environment" (merge (dict "UsePgbouncer" 
.Values.workers.celery.keda.usePgbouncer) .) | indent 10 }}
             {{- else if not (include "celery_result_backend_configured" .) }}
             {{- include "metadata_db_sql_alchemy_environment" . | indent 10 }}
             {{- else }}
diff --git a/chart/templates/workers/worker-kedaautoscaler.yaml 
b/chart/templates/workers/worker-kedaautoscaler.yaml
index 50ee1c92b7d..f25e765bd93 100644
--- a/chart/templates/workers/worker-kedaautoscaler.yaml
+++ b/chart/templates/workers/worker-kedaautoscaler.yaml
@@ -60,19 +60,29 @@ spec:
   triggers:
     {{- if eq .Values.data.metadataConnection.protocol "mysql" }}
     - type: "mysql"
+      {{- with .Values.workers.celery.keda.authenticationRef }}
+      authenticationRef: {{- toYaml . | nindent 8 }}
+      {{- end }}
       metadata:
         queryValue: "1"
+        {{- if not .Values.workers.celery.keda.authenticationRef }}
         connectionStringFromEnv: KEDA_DB_CONN
+        {{- end }}
         query: {{ tpl .Values.workers.celery.keda.query . | quote }}
     {{- else }}
     - type: "postgresql"
+      {{- with .Values.workers.celery.keda.authenticationRef }}
+      authenticationRef: {{- toYaml . | nindent 8 }}
+      {{- end }}
       metadata:
         targetQueryValue: "1"
+        {{- if not .Values.workers.celery.keda.authenticationRef }}
         {{- if and .Values.pgbouncer.enabled (not 
.Values.workers.celery.keda.usePgbouncer) }}
         connectionFromEnv: KEDA_DB_CONN
         {{- else }}
         connectionFromEnv: AIRFLOW_CONN_AIRFLOW_DB
         {{- end }}
+        {{- end }}
         query: {{ tpl .Values.workers.celery.keda.query . | quote }}
     {{- end }}
 {{- end }}
diff --git a/chart/tests/helm_tests/other/test_keda.py 
b/chart/tests/helm_tests/other/test_keda.py
index ff4c3cf4940..8e5e788e455 100644
--- a/chart/tests/helm_tests/other/test_keda.py
+++ b/chart/tests/helm_tests/other/test_keda.py
@@ -344,6 +344,62 @@ class TestKeda:
         )
         assert autoscaler_connection_env_var == "KEDA_DB_CONN"
 
+    def test_keda_authentication_ref_not_set_by_default(self):
+        docs = render_chart(
+            values={"workers": {"celery": {"keda": {"enabled": True}}}},
+            show_only=["templates/workers/worker-kedaautoscaler.yaml"],
+        )
+
+        assert jmespath.search("spec.triggers[0].authenticationRef", docs[0]) 
is None
+
+    @pytest.mark.parametrize(
+        ("authentication_ref", "pgbouncer_enabled", "use_pgbouncer"),
+        [
+            ({"name": "airflow-keda-db"}, False, True),
+            ({"name": "airflow-keda-db"}, True, False),
+            ({"name": "airflow-keda-db", "kind": 
"ClusterTriggerAuthentication"}, False, True),
+        ],
+    )
+    def test_keda_authentication_ref_postgresql(self, authentication_ref, 
pgbouncer_enabled, use_pgbouncer):
+        docs = render_chart(
+            values={
+                "pgbouncer": {"enabled": pgbouncer_enabled},
+                "workers": {
+                    "celery": {
+                        "keda": {
+                            "enabled": True,
+                            "usePgbouncer": use_pgbouncer,
+                            "authenticationRef": authentication_ref,
+                        }
+                    }
+                },
+            },
+            show_only=["templates/workers/worker-kedaautoscaler.yaml"],
+        )
+
+        trigger = jmespath.search("spec.triggers[0]", docs[0])
+        assert trigger["type"] == "postgresql"
+        assert trigger["authenticationRef"] == authentication_ref
+        assert "connectionFromEnv" not in trigger["metadata"]
+        assert trigger["metadata"]["targetQueryValue"] == "1"
+
+    def test_keda_authentication_ref_mysql(self):
+        docs = render_chart(
+            values={
+                "data": {"metadataConnection": {"protocol": "mysql", "port": 
3306}},
+                "workers": {
+                    "celery": {"keda": {"enabled": True, "authenticationRef": 
{"name": "airflow-keda-db"}}}
+                },
+            },
+            show_only=["templates/workers/worker-kedaautoscaler.yaml"],
+        )
+
+        trigger = jmespath.search("spec.triggers[0]", docs[0])
+        assert trigger["type"] == "mysql"
+        assert trigger["authenticationRef"] == {"name": "airflow-keda-db"}
+        assert "connectionStringFromEnv" not in trigger["metadata"]
+        assert trigger["metadata"]["queryValue"] == "1"
+
     @pytest.mark.parametrize(
         "queue",
         [
@@ -450,3 +506,35 @@ class TestKeda:
         assert "KEDA_DB_CONN" in self._env_names(triggerer)
         assert "KEDA_DB_CONN" not in self._env_names(worker)
         assert "KEDA_DB_CONN" not in self._env_names(scheduler)
+
+    @pytest.mark.parametrize(
+        "values",
+        [
+            {"data": {"metadataConnection": {"protocol": "mysql", "port": 
3306}}},
+            {"pgbouncer": {"enabled": True}},
+        ],
+    )
+    def test_worker_keda_db_conn_not_set_with_authentication_ref(self, values):
+        docs = render_chart(
+            values={
+                **values,
+                "executor": "CeleryExecutor",
+                "workers": {
+                    "celery": {
+                        "keda": {
+                            "enabled": True,
+                            "usePgbouncer": False,
+                            "authenticationRef": {"name": "airflow-keda-db"},
+                        }
+                    }
+                },
+                "triggerer": {"keda": {"enabled": True, "usePgbouncer": 
False}},
+            },
+            show_only=[
+                "templates/workers/worker-deployment.yaml",
+                "templates/triggerer/triggerer-deployment.yaml",
+            ],
+        )
+        worker, triggerer = docs
+        assert "KEDA_DB_CONN" not in self._env_names(worker)
+        assert "KEDA_DB_CONN" in self._env_names(triggerer)
diff --git a/chart/values.schema.json b/chart/values.schema.json
index 244f11b7012..ac5fad86270 100644
--- a/chart/values.schema.json
+++ b/chart/values.schema.json
@@ -2017,6 +2017,32 @@
                                     "description": "Weather to use PGBouncer 
to connect to the database or not when it is enabled. This configuration will 
be ignored if PGBouncer is not enabled.",
                                     "type": "boolean",
                                     "default": true
+                                },
+                                "authenticationRef": {
+                                    "description": "Reference to a KEDA 
TriggerAuthentication (or ClusterTriggerAuthentication) holding the database 
connection for the scaler. When set, it is used instead of the connection 
passed through the worker environment. The TriggerAuthentication must be 
created outside of this chart.",
+                                    "type": [
+                                        "null",
+                                        "object"
+                                    ],
+                                    "default": null,
+                                    "additionalProperties": false,
+                                    "required": [
+                                        "name"
+                                    ],
+                                    "properties": {
+                                        "name": {
+                                            "description": "Name of the 
TriggerAuthentication or ClusterTriggerAuthentication.",
+                                            "type": "string"
+                                        },
+                                        "kind": {
+                                            "description": "Kind of the 
referenced object.",
+                                            "type": "string",
+                                            "enum": [
+                                                "TriggerAuthentication",
+                                                "ClusterTriggerAuthentication"
+                                            ]
+                                        }
+                                    }
                                 }
                             }
                         },
diff --git a/chart/values.yaml b/chart/values.yaml
index a551f664c29..2d483eec80d 100644
--- a/chart/values.yaml
+++ b/chart/values.yaml
@@ -780,6 +780,17 @@ workers:
       # This configuration will be ignored if PGBouncer is not enabled
       usePgbouncer: true
 
+      # Reference to a KEDA TriggerAuthentication (or 
ClusterTriggerAuthentication) holding
+      # the database connection for the scaler. When set, it is used instead 
of the connection
+      # the chart passes through the worker environment (connectionFromEnv / 
connectionStringFromEnv),
+      # and the worker no longer needs the kedaConnection key in the metadata 
Secret.
+      # The TriggerAuthentication must be created outside of this chart.
+      # For PostgreSQL use the "connection" parameter; for MySQL use 
"connectionString".
+      # authenticationRef:
+      #   name: airflow-keda-db
+      #   kind: TriggerAuthentication
+      authenticationRef: ~
+
     # Allow HPA for Airflow Celery workers (KEDA must be disabled)
     hpa:
       enabled: false

Reply via email to