From d76322046cdbbfab4bd6f5400f3a28e4c9fc50b6 Mon Sep 17 00:00:00 2001 From: youngkermit8-coder <263626606+youngkermit8-coder@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:15:57 +0800 Subject: [PATCH] fix(helm): derive Patroni discovery labels from chart values --- .../helm_charts/templates/statefulset.yaml | 2 +- tests/test_helm_labels.py | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/test_helm_labels.py diff --git a/k8s-cluster/ha-cluster/helm_charts/templates/statefulset.yaml b/k8s-cluster/ha-cluster/helm_charts/templates/statefulset.yaml index ffa96a4..e1c38b9 100644 --- a/k8s-cluster/ha-cluster/helm_charts/templates/statefulset.yaml +++ b/k8s-cluster/ha-cluster/helm_charts/templates/statefulset.yaml @@ -85,7 +85,7 @@ spec: - name: PATRONI_KUBERNETES_USE_ENDPOINTS value: 'true' - name: PATRONI_KUBERNETES_LABELS - value: '{application: ivyhac, cluster-name: ivorysql-patroni-hac}' + value: {{ dict "application" .Values.applicationName "cluster-name" .Values.clusterName | toJson | quote }} - name: PATRONI_KUBERNETES_PORTS value: '[{"name": "ivorysql", "port": 5432}]' - name: IVORYSQL_PASSWORD diff --git a/tests/test_helm_labels.py b/tests/test_helm_labels.py new file mode 100644 index 0000000..ca01b41 --- /dev/null +++ b/tests/test_helm_labels.py @@ -0,0 +1,41 @@ +"""Render regression tests. Requires Helm 3 and PyYAML (pip install pyyaml).""" +import json +import os +from pathlib import Path +import subprocess +import unittest + +CHART = Path(__file__).resolve().parents[1] / 'k8s-cluster/ha-cluster/helm_charts' + +def render(overrides=None): + command = [os.environ.get('HELM', 'helm'), 'template', 'test', str(CHART)] + if overrides: + command += ['--values', '-'] + result = subprocess.run(command, input=json.dumps(overrides or {}), + text=True, encoding='utf-8', capture_output=True, check=True) + import yaml + return list(yaml.safe_load_all(result.stdout)) + +def statefulset(documents): + return next(doc for doc in documents if doc and doc['kind'] == 'StatefulSet') + +class LabelsTest(unittest.TestCase): + def test_patroni_matches_pod_labels(self): + for overrides in [{}, {'applicationName': 'custom-app', 'clusterName': 'custom-cluster'}]: + with self.subTest(overrides=overrides): + documents = render(overrides) + sts = statefulset(documents) + pod = sts['spec']['template'] + env = {e['name']: e.get('value') for e in pod['spec']['containers'][0]['env']} + import yaml + labels = yaml.safe_load(env['PATRONI_KUBERNETES_LABELS']) + self.assertEqual(labels, pod['metadata']['labels']) + self.assertEqual(labels['cluster-name'], env['PATRONI_SCOPE']) + for service in documents: + if service and service['kind'] == 'Service': + for key, value in service['spec'].get('selector', {}).items(): + if key != 'role': + self.assertEqual(labels[key], value) + +if __name__ == '__main__': + unittest.main()