Skip to content

Commit

Permalink
Allow chart to work with custom cluster domains (apache#15640)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jedcunningham authored May 4, 2021
1 parent 0f97a39 commit 59be278
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
4 changes: 2 additions & 2 deletions chart/templates/_helpers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 2 additions & 2 deletions chart/templates/secrets/metadata-connection-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand Down
8 changes: 4 additions & 4 deletions chart/tests/test_metadata_connection_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_default_connection(self):
connection = self._get_connection({})

assert (
"postgresql://postgres:[email protected].svc.cluster.local:5432"
"/postgres?sslmode=disable" == connection
"postgresql://postgres:[email protected]:5432/postgres?sslmode=disable"
== connection
)

def test_should_set_pgbouncer_overrides_when_enabled(self):
Expand All @@ -63,7 +63,7 @@ def test_should_set_pgbouncer_overrides_when_enabled(self):

# host, port, dbname get overridden
assert (
"postgresql://postgres:[email protected].svc.cluster.local:6543"
"postgresql://postgres:[email protected]:6543"
"/RELEASE-NAME-metadata?sslmode=disable" == connection
)

Expand All @@ -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:[email protected].svc.cluster.local:6543"
"postgresql://someuser:[email protected]:6543"
"/RELEASE-NAME-metadata?sslmode=disable" == connection
)

Expand Down
39 changes: 39 additions & 0 deletions chart/tests/test_pgbouncer.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 base64
import unittest

import jmespath
Expand Down Expand Up @@ -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

0 comments on commit 59be278

Please sign in to comment.