From b658dffe7c310db3b425457bdf898c0b7c3b7de6 Mon Sep 17 00:00:00 2001 From: Mattia Eleuteri Date: Sat, 24 Oct 2026 17:05:20 +0200 Subject: [PATCH] fix(postgres): render the declared databases in a stable order keys returns a map's keys in Go's randomized iteration order, so a release with several databases rendered DEFINED_DBS in a different order on each upgrade. The init-script Secret, and the checksum the init Job carries, then changed on upgrades that left the databases untouched, which makes a no-op upgrade look like a real change in the release history. Assisted-by: LLM Signed-off-by: Mattia Eleuteri (cherry picked from commit dc94660a44d2fa09165223723f5f9a5bd794f896) Signed-off-by: Myasnikov Daniil --- .../apps/postgres/templates/init-script.yaml | 2 +- .../tests/init_script_deterministic_test.yaml | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 packages/apps/postgres/tests/init_script_deterministic_test.yaml diff --git a/packages/apps/postgres/templates/init-script.yaml b/packages/apps/postgres/templates/init-script.yaml index e78279a2d..395267ff0 100644 --- a/packages/apps/postgres/templates/init-script.yaml +++ b/packages/apps/postgres/templates/init-script.yaml @@ -337,7 +337,7 @@ stringData: RESTORE_MARK={{ with .Values.bootstrap.newServerName }}$(echo {{ b64enc . }} | base64 -d){{ end }} ADOPTED_MARK=$(psql -v ON_ERROR_STOP=1 -t -A --dbname postgres -c "SELECT current_setting('cozystack.adopted_restore', true)") MANAGED_DBS=$(psql -v ON_ERROR_STOP=1 -t -A -c "SELECT datname FROM pg_database d JOIN pg_shdescription s ON d.oid = s.objoid WHERE s.description = 'database managed by helm'") - DEFINED_DBS="{{ join " " (keys .Values.databases) }}" + DEFINED_DBS="{{ join " " (keys .Values.databases | sortAlpha) }}" DELETE_DBS=$(for db in $MANAGED_DBS; do case " $DEFINED_DBS " in *" $db "*) :;; *) echo $db;; esac; done) if [ -n "$RESTORE_MARK" ] && [ "$ADOPTED_MARK" != "$RESTORE_MARK" ]; then diff --git a/packages/apps/postgres/tests/init_script_deterministic_test.yaml b/packages/apps/postgres/tests/init_script_deterministic_test.yaml new file mode 100644 index 000000000..25d0facdd --- /dev/null +++ b/packages/apps/postgres/tests/init_script_deterministic_test.yaml @@ -0,0 +1,37 @@ +suite: init-script renders the same for the same values + +# keys returns a map's keys in Go's randomized iteration order, so a release with +# several databases rendered DEFINED_DBS in a different order on each upgrade. The +# init-script Secret then changed on upgrades that changed nothing in the +# databases, and so did the checksum the init Job carries. + +templates: + - templates/init-script.yaml + +release: + name: pg-test + namespace: tenant-test + +set: + _cluster: + scheduling: {} + +tests: + - it: lists the declared databases in a stable order + set: + databases: + hotel: {} + alpha: {} + golf: {} + charlie: {} + foxtrot: {} + bravo: {} + echo: {} + delta: {} + documentSelector: + path: metadata.name + value: pg-test-init-script + asserts: + - matchRegex: + path: stringData["init.sh"] + pattern: 'DEFINED_DBS="alpha bravo charlie delta echo foxtrot golf hotel"'