mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-02 21:30:46 +09:00
Refs #5459. Part of bringing #5405 (standard-trixie images) back to
release-1.7, which #5461 reverted. This PR covers the `C.UTF-8` item of
fixed separately: the repair racing the rollout and the Job budget, the
repair skipping `C.UTF-8`, the double REINDEX, and the postgis extension
update.
Clusters created before #5405 run a bare tag, the system variant on
Debian bullseye. Moving them to `standard-trixie` changes glibc and ICU
under the data directory. The collation repair pass from #5405 cannot
see every case. A libc `C.*` locale such as `C.UTF-8` carries no
collation version in any PostgreSQL major, yet Debian bullseye's
`C.UTF-8` and the one in glibc 2.35+ sort differently. Until the indexes
are rebuilt, replicas return wrong results through `-ro` and unique
indexes accept duplicates. PostgreSQL ([Locale data
changes](https://wiki.postgresql.org/wiki/Locale_data_changes)) and
CloudNativePG
([postgres-containers](https://github.com/cloudnative-pg/postgres-containers):
switching the Debian base "is a manual operation because of PostgreSQL
locale-data implications") both treat this as a deliberate, owner-driven
step.
A cluster on one of the bare bullseye tags the chart shipped (13.22,
14.20, 15.15, 16.11, 17.7, 18.1) stays on its image until a check finds
it clean. The check fails closed: a cluster not checked yet, one the
check could not finish on, and one it found affected all stay.
- **The check.** A `pre-upgrade` hook (`locale-check-<release>-<hash>`)
runs before every upgrade of such a cluster, from the cluster's current
image.
- It looks in every database for columns, indexes, composite or range
types, and `jsonb` indexes that depend on a collation other than C,
POSIX or builtin, and for a database default (collate or ctype) outside
those.
- It records `clean`, `affected` or `unknown` in the
`postgres.cozystack.io/locale-data` annotation of the Cluster, and the
first objects found in `postgres.cozystack.io/locale-data-objects`.
- Its Role can only `get`/`patch` that one Cluster.
- **What the chart renders.** The chart reads the recorded answer
through `lookup`. Helm renders before hooks run, so the hook fails an
attempt only when the new answer contradicts that render, before the
Cluster is patched. The HelmRelease retry then renders according to the
new answer:
- A cluster not checked before, or found `affected` or `unknown`, is
rendered on its current image. The upgrade passes, and every other value
of the app still applies.
- A cluster found `clean` fails that attempt once, and the retry moves
it to `standard-trixie` with the existing repair.
- **Opt-in.** `allowCollationChange: true` moves a held cluster on the
next upgrade, in a window the owner picks.
- **Version changes.** A held cluster refuses a change of `version` with
an explicit render error, since new majors are built only on the new
images.
- **Restore.** The CNPG backup driver copies the source Cluster's
`spec.imageName` into the backup snapshot verbatim, passes it to the
restore as `bootstrap.sourceImageName`, and clears it with
`bootstrap.enabled`. The core controller knows nothing about the chart's
hold rules.
- The chart decides: a backup taken on one of the bullseye tags restores
onto that image, so the data keeps the library that built it.
- The next upgrade then checks the restored data like any other bullseye
cluster.
- Any other value is ignored, so a tenant cannot choose an image through
it.
- The driver also reads the major from that image tag when the backup
recorded none, and refuses a mismatch with `spec.version` before the
purge.
- It keeps running its bare bullseye tag. Upstream builds new PostgreSQL
minors only on bookworm and trixie, so a held cluster gets **no further
minors and none of their security fixes; CVE-2026-116331 stays open on
it** for as long as it is held.
- The tenant sees only that the image did not change. The answer and the
objects found are on the cnpg.io Cluster, which only an administrator
can read.
- There is no deadline. The hold lasts until the owner takes one of the
ways out below.
1. **Remove the dependency.** Move the listed columns and indexes to
`C`, or to the builtin `pg_c_utf8` on PostgreSQL 17 and later, or drop
them. The next upgrade checks again, finds the cluster `clean` and moves
it.
2. **Move explicitly.** In a maintenance window, set
`allowCollationChange: true`. The next upgrade moves the cluster and the
init Job's repair rebuilds indexes on versioned collations (ICU, libc
locales other than `C.*`). Until the separate `C.UTF-8` repair fix
lands, rebuild indexes on `C.UTF-8` or another `C.*` locale by hand with
`REINDEX INDEX CONCURRENTLY`. Range partitions, CHECK constraints and
materialized views on such text are not fixed by a rebuild and need a
logical move (pg_dump/restore or logical replication). During the move,
replicas restart on the new image first, so reads through `-ro` can be
wrong and unique indexes can accept duplicates until the primary's
rebuild is done.
- `helm unittest packages/apps/postgres`: 276 passed, including
`tests/locale_guard_test.yaml`. It covers:
- holding on `affected`, `unknown` and a missing result, and moving on
`clean`;
- the opt-in and the version refusal;
- restores from bullseye and trixie backups, hand-set recoveries, and
leftover values;
- ignoring foreign images, postgis, hook gating, naming and the Role
scope.
- `go test ./internal/backupcontroller/...` passes, including
`cnpg_source_image_test.go` and the restore reconcile cases for passing
the image on and refusing the held major before the purge.
- The check script was run in Docker against `postgres:13-bullseye` and
`postgres:18-bookworm`:
- It flags `C.UTF-8`/`C.utf8` columns and defaults, ICU indexes, `en_US`
defaults with text, a `C`-collate / `en_US`-ctype database with a
`lower()` index, composite and range types over text, and `jsonb`
indexes under a non-C default.
- It reports `clean` for `C`, `POSIX`, `pg_c_utf8` and `ucs_basic`, and
when another database is `default_transaction_read_only`.
- It reports `unknown` on an authentication failure or when a database
accepts no connections.
- The record step was run with a stub `kubectl` for each render (held or
moving) and answer:
- held + `clean` fails the attempt; held + `affected`/`unknown` passes;
- moving + `clean` passes; moving + `affected`/`unknown` fails the
attempt;
- every answer is recorded.
- Dev cluster: apps installed from the v1.7.1 chart (`postgresql:17.7`,
bullseye) and upgraded to this branch (results in the PR thread).
The trigger map hits `cozystack/terraform-provider-cozystack`, because
this adds `allowCollationChange` and `bootstrap.sourceImageName` to the
postgres `values.schema.json`. The follow-up is
cozystack/terraform-provider-cozystack#91.
- [ ] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [x]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up: cozystack/terraform-provider-cozystack#91
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:
```release-note
[postgres] A cluster still on a Debian bullseye image now stays on that image until a pre-upgrade check finds nothing in it that depends on a collation other than C, POSIX or builtin (an ICU or libc locale, or C.UTF-8), or until `allowCollationChange` is set. A held cluster gets no further PostgreSQL minors or their security fixes. The `postgres.cozystack.io/locale-data` annotation of the cnpg.io Cluster shows the result of the check.
```
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
- **New Features**
- PostgreSQL upgrades from older Debian Bullseye images now check for
collation-dependent data and hold the current image when results are
affected or unknown.
- Added an opt-in setting to allow the image change despite the
collation check. Review collation and index-rebuild considerations
before enabling it.
- PostgreSQL restores retain source-image information to support image
selection and compatibility checks.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
On release-1.7 the CNPG backup driver predates the backup destination
and artifact-size changes this builds on on main, so only the image
handling is ported: the live Cluster's spec.imageName goes into the
backup snapshot, the restore passes it to the chart as
bootstrap.sourceImageName and clears it with bootstrap.enabled, and the
backup's major is read from that image when the backup recorded none.
The tests for the destination and artifact size are not part of this
line.
(cherry picked from commit 9cf93b8535)
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
162 lines
9.2 KiB
Smarty
162 lines
9.2 KiB
Smarty
{{- define "postgres.versionMap" }}
|
|
{{- $versionMap := .Files.Get "files/versions.yaml" | fromYaml }}
|
|
{{- if not (hasKey $versionMap .Values.version) }}
|
|
{{- printf `PostgreSQL version %s is not supported, allowed versions are %s` $.Values.version (keys $versionMap) | fail }}
|
|
{{- end }}
|
|
{{- index $versionMap .Values.version }}
|
|
{{- end }}
|
|
|
|
{{- define "postgres.postgisVersionMap" }}
|
|
{{- $versionMap := .Files.Get "files/postgis-versions.yaml" | fromYaml }}
|
|
{{- if not (hasKey $versionMap .Values.version) }}
|
|
{{- printf `PostgreSQL version %s is not supported by the postgis flavor, allowed versions are %s` $.Values.version (keys $versionMap | sortAlpha) | fail }}
|
|
{{- end }}
|
|
{{- index $versionMap .Values.version }}
|
|
{{- end }}
|
|
|
|
{{/*
|
|
Single source for the operand image: the init Job runs psql against the
|
|
Cluster, so both must resolve the same image, and the Job compares its own
|
|
PostgreSQL build with the server's to tell when a new image has rolled out.
|
|
|
|
standard-trixie, not the bare tag: a bare tag is the system variant on
|
|
Debian bullseye, which upstream stopped building. The standard variant,
|
|
because backups go through the barman-cloud plugin and need none of the
|
|
barman binaries the system variant adds. trixie, because the postgis
|
|
flavor is on it already, so both image families share one Debian release.
|
|
*/}}
|
|
{{- define "postgres.imageName" -}}
|
|
{{- include "postgres.heldImageName" . | default (include "postgres.targetImageName" .) -}}
|
|
{{- end -}}
|
|
|
|
{{- define "postgres.targetImageName" -}}
|
|
{{- if eq (default "postgresql" .Values.flavor) "postgis" -}}
|
|
ghcr.io/cloudnative-pg/postgis:{{ include "postgres.postgisVersionMap" . | trim }}
|
|
{{- else -}}
|
|
ghcr.io/cloudnative-pg/postgresql:{{ include "postgres.versionMap" . | trim | trimPrefix "v" }}-standard-trixie
|
|
{{- end -}}
|
|
{{- end -}}
|
|
|
|
{{/*
|
|
The image a cluster on Debian bullseye keeps, or nothing. Clusters created
|
|
before the move to trixie run one of the bare tags postgres.bullseyeImage
|
|
lists, and moving them changes glibc and ICU under an initialised data
|
|
directory. Such a cluster stays on its image until the pre-upgrade locale
|
|
check has recorded "clean" on its Cluster, or until allowCollationChange is
|
|
set: a cluster not checked yet, one the check could not finish on and one
|
|
it found affected all stay. New majors are built only on the new images, so
|
|
a held cluster refuses a change of version instead of dropping it.
|
|
*/}}
|
|
{{- define "postgres.heldImageName" -}}
|
|
{{- if and (ne (default "postgresql" .Values.flavor) "postgis") (not .Values.allowCollationChange) -}}
|
|
{{- $hold := include "postgres.localeHold" . | fromJson -}}
|
|
{{- if and $hold.image (ne $hold.verdict "clean") -}}
|
|
{{- $heldMajor := regexFind "^[0-9]+" (last (splitList ":" $hold.image)) -}}
|
|
{{- $wantMajor := trimPrefix "v" .Values.version -}}
|
|
{{- if ne $heldMajor $wantMajor -}}
|
|
{{- fail (printf "postgres: version %s cannot be applied: the cluster stays on %s until the pre-upgrade check finds nothing in it that depends on a collation sorting differently on the images PostgreSQL %s is built on (the postgres.cozystack.io/locale-data annotation of the cnpg.io Cluster). Keep version v%s, or set allowCollationChange: true and rebuild the affected indexes in a maintenance window." .Values.version $hold.image $wantMajor $heldMajor) -}}
|
|
{{- end -}}
|
|
{{- $hold.image -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
|
|
{{/*
|
|
The bullseye image that applies to this cluster and its check result, as
|
|
JSON; the image is empty when the cluster runs any other one. The result is
|
|
the postgres.cozystack.io/locale-data annotation the pre-upgrade check
|
|
writes on the live Cluster. A physical recovery with no live Cluster yet
|
|
(the CNPG backup driver deletes it before the recovery renders) restores
|
|
onto the image the backup was taken on, so that the data keeps the library
|
|
that built it: bootstrap.sourceImageName, which the driver copies from the
|
|
source Cluster, or for a recovery set up by hand the bootstrap.oldName
|
|
Cluster while it exists. A recovery has no result of its own and is held
|
|
until the first upgrade after it checks the restored data.
|
|
|
|
bootstrap.sourceImageName is in the application spec, so anyone who may
|
|
edit the application may set it; it only ever selects one of the images
|
|
postgres.bullseyeImage lists.
|
|
*/}}
|
|
{{- define "postgres.localeHold" -}}
|
|
{{- $live := lookup "postgresql.cnpg.io/v1" "Cluster" .Release.Namespace .Release.Name -}}
|
|
{{- $image := dig "spec" "imageName" "" $live -}}
|
|
{{- $verdict := dig "metadata" "annotations" "postgres.cozystack.io/locale-data" "" $live -}}
|
|
{{- if and .Values.bootstrap.enabled (not $image) -}}
|
|
{{- $image = .Values.bootstrap.sourceImageName | default (dig "spec" "imageName" "" (include "postgres.recoverySource" . | fromJson)) -}}
|
|
{{- $verdict = "" -}}
|
|
{{- end -}}
|
|
{{- if ne (include "postgres.bullseyeImage" $image) "true" -}}
|
|
{{- $image = "" -}}
|
|
{{- end -}}
|
|
{{- dict "image" $image "verdict" $verdict | toJson -}}
|
|
{{- end -}}
|
|
|
|
{{- define "postgres.recoverySource" -}}
|
|
{{- if and .Values.bootstrap.enabled .Values.bootstrap.oldName (not .Values.bootstrap.newServerName) -}}
|
|
{{- lookup "postgresql.cnpg.io/v1" "Cluster" .Release.Namespace .Values.bootstrap.oldName | toJson -}}
|
|
{{- else -}}
|
|
{}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
|
|
{{/*
|
|
The images this chart ran clusters on before the move to trixie: the bare
|
|
version tags of the CloudNativePG image, which are its system variant on
|
|
Debian bullseye, as files/versions.yaml listed them in every release before
|
|
standard-trixie. Exact on purpose: a held image reaches spec.imageName.
|
|
*/}}
|
|
{{- define "postgres.bullseyeImage" -}}
|
|
{{- has . (list "ghcr.io/cloudnative-pg/postgresql:13.22" "ghcr.io/cloudnative-pg/postgresql:14.20" "ghcr.io/cloudnative-pg/postgresql:15.15" "ghcr.io/cloudnative-pg/postgresql:16.11" "ghcr.io/cloudnative-pg/postgresql:17.7" "ghcr.io/cloudnative-pg/postgresql:18.1") -}}
|
|
{{- end -}}
|
|
|
|
{{/*
|
|
The live image of a cluster still on bullseye whose owner has not allowed
|
|
the collation change, or nothing. The pre-upgrade locale check runs only
|
|
then, from that image, against the server it is checking.
|
|
*/}}
|
|
{{- define "postgres.localeCheckImage" -}}
|
|
{{- if and (ne (default "postgresql" .Values.flavor) "postgis") (not .Values.allowCollationChange) (not .Values.bootstrap.enabled) -}}
|
|
{{- $live := lookup "postgresql.cnpg.io/v1" "Cluster" .Release.Namespace .Release.Name -}}
|
|
{{- if and $live $live.spec $live.spec.imageName (eq (include "postgres.bullseyeImage" $live.spec.imageName) "true") -}}
|
|
{{- $live.spec.imageName -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
|
|
{{/*
|
|
The flavor is fixed at bootstrap. Only the postgis image carries the shared
|
|
libraries behind PostGIS objects, so the postgis-to-postgresql direction
|
|
leaves every one of them without its code. The two families are also built
|
|
and released separately, and nothing keeps their Debian release in step, so
|
|
either direction can swap glibc and ICU under an initialised data directory
|
|
together with the flavor. CNPG would roll either change out as an ordinary
|
|
image update, so refuse it here, before the Cluster is patched. Only the
|
|
image name is compared: a minor bump, a move to another variant of the same
|
|
family, or a registry mirror rewriting the host must keep passing.
|
|
|
|
A physical recovery carries the same data directory across, so it is held
|
|
to the same rule. With no live Cluster of its own yet, the release is
|
|
checked against its recovery source while that Cluster still exists; once
|
|
the source is gone the chart has nothing to compare against, and the
|
|
backup-controller's RestoreJob, which records the flavor on every Backup,
|
|
is the path that enforces it.
|
|
*/}}
|
|
{{- define "postgres.flavorGuard" -}}
|
|
{{- $wantName := regexReplaceAll "[:@].*$" (last (splitList "/" (include "postgres.imageName" .))) "" -}}
|
|
{{- $live := lookup "postgresql.cnpg.io/v1" "Cluster" .Release.Namespace .Release.Name -}}
|
|
{{- if and $live $live.spec $live.spec.imageName -}}
|
|
{{- $liveName := regexReplaceAll "[:@].*$" (last (splitList "/" $live.spec.imageName)) "" -}}
|
|
{{- if ne $liveName $wantName -}}
|
|
{{- fail (printf "postgres: flavor cannot change on an existing cluster (running %s, requested %s). Only the postgis image carries the PostGIS libraries, and the two image families can differ in glibc and ICU, which corrupts text indexes. Create a new Postgres with the wanted flavor and move the data with a logical dump (pg_dump / pg_restore); restoring a backup keeps the flavor it was taken from." $liveName $wantName) -}}
|
|
{{- end -}}
|
|
{{- else if and .Values.bootstrap.enabled .Values.bootstrap.oldName -}}
|
|
{{- $source := lookup "postgresql.cnpg.io/v1" "Cluster" .Release.Namespace .Values.bootstrap.oldName -}}
|
|
{{- if and $source $source.spec $source.spec.imageName -}}
|
|
{{- $sourceName := regexReplaceAll "[:@].*$" (last (splitList "/" $source.spec.imageName)) "" -}}
|
|
{{- if ne $sourceName $wantName -}}
|
|
{{- fail (printf "postgres: cannot recover a %s backup of %s into the %s flavor. A physical restore carries over a data directory built with the source's libraries: PostGIS objects need the postgis image, and a glibc or ICU difference corrupts text indexes. Set flavor to match the source, or create the new Postgres empty and move the data with a logical dump (pg_dump / pg_restore)." $sourceName .Values.bootstrap.oldName $wantName) -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- end -}}
|