shlomitubul commented on code in PR #64763: URL: https://github.com/apache/airflow/pull/64763#discussion_r3051252793
########## helm-tests/tests/helm_tests/security/test_tpl_rendering.py: ########## @@ -0,0 +1,152 @@ +# 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 base64 + +import jmespath +import pytest +from chart_utils.helm_template_generator import render_chart + + +class TestServiceAccountAnnotationsTplRendering: Review Comment: Done — moved all SA annotation tpl tests into `test_annotations.py` and deleted `test_tpl_rendering.py`. ########## helm-tests/tests/helm_tests/security/test_tpl_rendering.py: ########## @@ -0,0 +1,152 @@ +# 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 base64 + +import jmespath +import pytest +from chart_utils.helm_template_generator import render_chart + + +class TestServiceAccountAnnotationsTplRendering: + """Tests that ServiceAccount annotations support tpl rendering for all components.""" + + @pytest.mark.parametrize( + "component, template, values_key", + [ + ("scheduler", "templates/scheduler/scheduler-serviceaccount.yaml", "scheduler"), + ("pgbouncer", "templates/pgbouncer/pgbouncer-serviceaccount.yaml", "pgbouncer"), + ("webserver", "templates/webserver/webserver-serviceaccount.yaml", "webserver"), + ("triggerer", "templates/triggerer/triggerer-serviceaccount.yaml", "triggerer"), + ("dag-processor", "templates/dag-processor/dag-processor-serviceaccount.yaml", "dagProcessor"), + ("api-server", "templates/api-server/api-server-serviceaccount.yaml", "apiServer"), + ], + ) + def test_tpl_rendered_annotation(self, component, template, values_key): Review Comment: Done — split into separate parametrized test methods: `test_tpl_rendered_annotations_airflow_2` (scheduler, pgbouncer, triggerer) and `test_tpl_rendered_annotations_airflow_3` (dag-processor, api-server). No conditional logic in any test. ########## helm-tests/tests/helm_tests/security/test_tpl_rendering.py: ########## @@ -0,0 +1,152 @@ +# 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 base64 + +import jmespath +import pytest +from chart_utils.helm_template_generator import render_chart + + +class TestServiceAccountAnnotationsTplRendering: + """Tests that ServiceAccount annotations support tpl rendering for all components.""" + + @pytest.mark.parametrize( + "component, template, values_key", + [ + ("scheduler", "templates/scheduler/scheduler-serviceaccount.yaml", "scheduler"), + ("pgbouncer", "templates/pgbouncer/pgbouncer-serviceaccount.yaml", "pgbouncer"), + ("webserver", "templates/webserver/webserver-serviceaccount.yaml", "webserver"), + ("triggerer", "templates/triggerer/triggerer-serviceaccount.yaml", "triggerer"), + ("dag-processor", "templates/dag-processor/dag-processor-serviceaccount.yaml", "dagProcessor"), + ("api-server", "templates/api-server/api-server-serviceaccount.yaml", "apiServer"), + ], + ) + def test_tpl_rendered_annotation(self, component, template, values_key): + airflow_version = "3.0.0" if component in ("dag-processor", "api-server") else "2.11.2" + component_values = { + "serviceAccount": { + "annotations": { + "test-annotation": "{{ .Release.Name }}-value", + }, + }, + } + if component == "pgbouncer": + component_values["enabled"] = True + docs = render_chart( + values={ + "airflowVersion": airflow_version, + values_key: component_values, + }, + show_only=[template], + ) + annotations = jmespath.search("metadata.annotations", docs[0]) + assert annotations["test-annotation"] == "release-name-value" + + def test_worker_tpl_rendered_annotation(self): + docs = render_chart( + values={ + "workers": { + "serviceAccount": { + "annotations": { + "test-annotation": "{{ .Release.Name }}-worker", + }, + }, + }, + }, + show_only=["templates/workers/worker-serviceaccount.yaml"], + ) + annotations = jmespath.search("metadata.annotations", docs[0]) + assert annotations["test-annotation"] == "release-name-worker" + + @pytest.mark.parametrize( + "component, template, values_key", + [ + ("pgbouncer", "templates/pgbouncer/pgbouncer-serviceaccount.yaml", "pgbouncer"), + ("webserver", "templates/webserver/webserver-serviceaccount.yaml", "webserver"), + ], + ) + def test_plain_annotation_still_works(self, component, template, values_key): Review Comment: Correct — removed the duplicate. The new tests are added alongside the existing ones in `test_annotations.py`. ########## helm-tests/tests/helm_tests/security/test_tpl_rendering.py: ########## @@ -0,0 +1,152 @@ +# 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 base64 + +import jmespath +import pytest +from chart_utils.helm_template_generator import render_chart + + +class TestServiceAccountAnnotationsTplRendering: + """Tests that ServiceAccount annotations support tpl rendering for all components.""" + + @pytest.mark.parametrize( + "component, template, values_key", + [ + ("scheduler", "templates/scheduler/scheduler-serviceaccount.yaml", "scheduler"), + ("pgbouncer", "templates/pgbouncer/pgbouncer-serviceaccount.yaml", "pgbouncer"), + ("webserver", "templates/webserver/webserver-serviceaccount.yaml", "webserver"), + ("triggerer", "templates/triggerer/triggerer-serviceaccount.yaml", "triggerer"), + ("dag-processor", "templates/dag-processor/dag-processor-serviceaccount.yaml", "dagProcessor"), + ("api-server", "templates/api-server/api-server-serviceaccount.yaml", "apiServer"), + ], + ) + def test_tpl_rendered_annotation(self, component, template, values_key): + airflow_version = "3.0.0" if component in ("dag-processor", "api-server") else "2.11.2" + component_values = { + "serviceAccount": { + "annotations": { + "test-annotation": "{{ .Release.Name }}-value", + }, + }, + } + if component == "pgbouncer": + component_values["enabled"] = True + docs = render_chart( + values={ + "airflowVersion": airflow_version, + values_key: component_values, + }, + show_only=[template], + ) + annotations = jmespath.search("metadata.annotations", docs[0]) + assert annotations["test-annotation"] == "release-name-value" + + def test_worker_tpl_rendered_annotation(self): + docs = render_chart( + values={ + "workers": { + "serviceAccount": { + "annotations": { + "test-annotation": "{{ .Release.Name }}-worker", + }, + }, + }, + }, + show_only=["templates/workers/worker-serviceaccount.yaml"], + ) + annotations = jmespath.search("metadata.annotations", docs[0]) + assert annotations["test-annotation"] == "release-name-worker" + + @pytest.mark.parametrize( + "component, template, values_key", + [ + ("pgbouncer", "templates/pgbouncer/pgbouncer-serviceaccount.yaml", "pgbouncer"), + ("webserver", "templates/webserver/webserver-serviceaccount.yaml", "webserver"), + ], + ) + def test_plain_annotation_still_works(self, component, template, values_key): + component_values = { + "serviceAccount": { + "annotations": { + "test-annotation": "plain-value", + }, + }, + } + if component == "pgbouncer": + component_values["enabled"] = True + docs = render_chart( + values={ + "airflowVersion": "2.11.2", + values_key: component_values, + }, + show_only=[template], + ) + annotations = jmespath.search("metadata.annotations", docs[0]) + assert annotations["test-annotation"] == "plain-value" + + +class TestMetadataConnectionTplRendering: Review Comment: Done — moved both `test_tpl_rendered_user_and_db` and `test_tpl_rendered_user_and_db_plain_values` into `TestMetadataConnectionSecret` in `test_metadata_connection_secret.py`. ########## chart/templates/_helpers.yaml: ########## @@ -497,16 +503,16 @@ If release name contains chart name it will be used as a full name. {{ $pgMetadataHost := .Values.data.metadataConnection.host | default (printf "%s-%s.%s" .Release.Name "postgresql" .Release.Namespace) }} {{ $pgResultBackendHost := $resultBackendConnection.host | default (printf "%s-%s.%s" .Release.Name "postgresql" .Release.Namespace) }} [databases] -{{ .Release.Name }}-metadata = host={{ $pgMetadataHost }} dbname={{ .Values.data.metadataConnection.db }} port={{ .Values.data.metadataConnection.port }} pool_size={{ .Values.pgbouncer.metadataPoolSize }} {{ .Values.pgbouncer.extraIniMetadata | default "" }} -{{ .Release.Name }}-result-backend = host={{ $pgResultBackendHost }} dbname={{ $resultBackendConnection.db }} port={{ $resultBackendConnection.port }} pool_size={{ .Values.pgbouncer.resultBackendPoolSize }} {{ .Values.pgbouncer.extraIniResultBackend | default "" }} +{{ .Release.Name }}-metadata = host={{ $pgMetadataHost }} dbname={{ tpl .Values.data.metadataConnection.db . }} port={{ .Values.data.metadataConnection.port }} pool_size={{ .Values.pgbouncer.metadataPoolSize }} {{ .Values.pgbouncer.extraIniMetadata | default "" }} +{{ .Release.Name }}-result-backend = host={{ $pgResultBackendHost }} dbname={{ tpl ($resultBackendConnection.db) . }} port={{ $resultBackendConnection.port }} pool_size={{ .Values.pgbouncer.resultBackendPoolSize }} {{ .Values.pgbouncer.extraIniResultBackend | default "" }} Review Comment: Done — removed the unnecessary parentheses. ########## chart/templates/_helpers.yaml: ########## @@ -50,6 +50,12 @@ If release name contains chart name it will be used as a full name. {{- end }} {{- end }} +{{- define "airflow.tplDict" -}} + {{- range $key, $value := .values }} + {{- printf "%s: %s" $key (tpl $value $.context | quote) | nindent 4 }} Review Comment: Done — removed `nindent 4` from the helper and added `| nindent 4` at each call site, making `tplDict` generic and reusable at any indent level. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
