From 59be27882761d81d90a7caeb433a1e29daf5710b Mon Sep 17 00:00:00 2001 From: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com> Date: Tue, 4 May 2021 10:07:56 -0600 Subject: [PATCH] Allow chart to work with custom cluster domains (#15640) K8s clusters can use a different cluster domain than the default `cluster.local`. This change will make the chart compatible with those clusters as well. --- chart/templates/_helpers.yaml | 4 +- .../secrets/metadata-connection-secret.yaml | 4 +- .../tests/test_metadata_connection_secret.py | 8 ++-- chart/tests/test_pgbouncer.py | 39 +++++++++++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/chart/templates/_helpers.yaml b/chart/templates/_helpers.yaml index 2f1641b011b9c..7e9b7df9d5ec6 100644 --- a/chart/templates/_helpers.yaml +++ b/chart/templates/_helpers.yaml @@ -300,8 +300,8 @@ If release name contains chart name it will be used as a full name. {{- end }} {{ define "pgbouncer_config" }} -{{- $pgMetadataHost := .Values.data.metadataConnection.host | default (printf "%s-%s.%s.svc.cluster.local" .Release.Name "postgresql" .Release.Namespace) }} -{{- $pgResultBackendHost := .Values.data.resultBackendConnection.host | default (printf "%s-%s.%s.svc.cluster.local" .Release.Name "postgresql" .Release.Namespace) }} +{{- $pgMetadataHost := .Values.data.metadataConnection.host | default (printf "%s-%s.%s" .Release.Name "postgresql" .Release.Namespace) }} +{{- $pgResultBackendHost := .Values.data.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 }} {{ .Release.Name }}-result-backend = host={{ $pgResultBackendHost }} dbname={{ .Values.data.resultBackendConnection.db }} port={{ .Values.data.resultBackendConnection.port }} pool_size={{ .Values.pgbouncer.resultBackendPoolSize }} diff --git a/chart/templates/secrets/metadata-connection-secret.yaml b/chart/templates/secrets/metadata-connection-secret.yaml index c4061feceadb5..cb14a9a42f0b6 100644 --- a/chart/templates/secrets/metadata-connection-secret.yaml +++ b/chart/templates/secrets/metadata-connection-secret.yaml @@ -19,8 +19,8 @@ ## Airflow Metadata Secret ################################# {{- if not .Values.data.metadataSecretName }} -{{- $metadataHost := .Values.data.metadataConnection.host | default (printf "%s-%s.%s.svc.cluster.local" .Release.Name "postgresql" .Release.Namespace) }} -{{- $pgbouncerHost := (printf "%s-%s.%s.svc.cluster.local" .Release.Name "pgbouncer" .Release.Namespace) }} +{{- $metadataHost := .Values.data.metadataConnection.host | default (printf "%s-%s.%s" .Release.Name "postgresql" .Release.Namespace) }} +{{- $pgbouncerHost := (printf "%s-%s.%s" .Release.Name "pgbouncer" .Release.Namespace) }} {{- $host := ternary $pgbouncerHost $metadataHost .Values.pgbouncer.enabled }} {{- $port := ((ternary .Values.ports.pgbouncer .Values.data.metadataConnection.port .Values.pgbouncer.enabled) | toString) }} {{- $database := (ternary (printf "%s-%s" .Release.Name "metadata") .Values.data.metadataConnection.db .Values.pgbouncer.enabled) }} diff --git a/chart/tests/test_metadata_connection_secret.py b/chart/tests/test_metadata_connection_secret.py index fe90cc0bc259c..0df01901fecc0 100644 --- a/chart/tests/test_metadata_connection_secret.py +++ b/chart/tests/test_metadata_connection_secret.py @@ -53,8 +53,8 @@ def test_default_connection(self): connection = self._get_connection({}) assert ( - "postgresql://postgres:postgres@RELEASE-NAME-postgresql.default.svc.cluster.local:5432" - "/postgres?sslmode=disable" == connection + "postgresql://postgres:postgres@RELEASE-NAME-postgresql.default:5432/postgres?sslmode=disable" + == connection ) def test_should_set_pgbouncer_overrides_when_enabled(self): @@ -63,7 +63,7 @@ def test_should_set_pgbouncer_overrides_when_enabled(self): # host, port, dbname get overridden assert ( - "postgresql://postgres:postgres@RELEASE-NAME-pgbouncer.default.svc.cluster.local:6543" + "postgresql://postgres:postgres@RELEASE-NAME-pgbouncer.default:6543" "/RELEASE-NAME-metadata?sslmode=disable" == connection ) @@ -76,7 +76,7 @@ def test_should_set_pgbouncer_overrides_with_non_chart_database_when_enabled(sel # host, port, dbname still get overridden even with an non-chart db assert ( - "postgresql://someuser:somepass@RELEASE-NAME-pgbouncer.default.svc.cluster.local:6543" + "postgresql://someuser:somepass@RELEASE-NAME-pgbouncer.default:6543" "/RELEASE-NAME-metadata?sslmode=disable" == connection ) diff --git a/chart/tests/test_pgbouncer.py b/chart/tests/test_pgbouncer.py index dbe49c54c8f5c..ff3fc1f0bfd21 100644 --- a/chart/tests/test_pgbouncer.py +++ b/chart/tests/test_pgbouncer.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +import base64 import unittest import jmespath @@ -133,3 +134,41 @@ def test_pgbouncer_resources_are_not_added_by_default(self): show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"], ) assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {} + + +class PgbouncerConfigTest(unittest.TestCase): + def test_databases_default(self): + docs = render_chart( + values={"pgbouncer": {"enabled": True}}, + show_only=["templates/secrets/pgbouncer-config-secret.yaml"], + ) + + encoded_ini = jmespath.search('data."pgbouncer.ini"', docs[0]) + ini = base64.b64decode(encoded_ini).decode() + + assert ( + "RELEASE-NAME-metadata = host=RELEASE-NAME-postgresql.default dbname=postgres port=5432" + " pool_size=10" in ini + ) + assert ( + "RELEASE-NAME-result-backend = host=RELEASE-NAME-postgresql.default dbname=postgres port=5432" + " pool_size=5" in ini + ) + + def test_hostname_override(self): + docs = render_chart( + values={ + "pgbouncer": {"enabled": True, "metadataPoolSize": 12, "resultBackendPoolSize": 7}, + "data": { + "metadataConnection": {"host": "meta_host", "db": "meta_db", "port": 1111}, + "resultBackendConnection": {"host": "rb_host", "db": "rb_db", "port": 2222}, + }, + }, + show_only=["templates/secrets/pgbouncer-config-secret.yaml"], + ) + + encoded_ini = jmespath.search('data."pgbouncer.ini"', docs[0]) + ini = base64.b64decode(encoded_ini).decode() + + assert "RELEASE-NAME-metadata = host=meta_host dbname=meta_db port=1111 pool_size=12" in ini + assert "RELEASE-NAME-result-backend = host=rb_host dbname=rb_db port=2222 pool_size=7" in ini