simplify and automate Postgres version upgrades

Aperta
#11 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
helm, postgresql
Ambito
databases

Direzione di ricerca

Start with templates/pg_cluster.yaml and the chart README, then inspect the release values and the charts that create the keycloak and metadig clusters. The issue recommends first confirming the image details, making the image explicit in values, and validating the rendered chart; read the remaining §4 steps to identify all affected files. Done means releases render with an explicit PostgreSQL image and the README accurately describes upgrade behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

Our CNPG-managed PostgreSQL clusters do not receive minor-version upgrades, despite what our README says. Every cluster created by this chart is frozen at the default image of the operator version that was running when the cluster was created. On both dev-k8s and prod-k8s, every chart-managed cluster is on PG 17.5. The current PG 17 minor release is 17.12, so we are seven minor releases behind on security and bug fixes.

This issue does three things:

  1. Documents how the chart behaves today.
  2. Proposes two ways to fix it:
    • (Pathway A) pin and bump image.name explicitly per release.
    • (Pathway B) manage versions centrally with a ClusterImageCatalog.
  3. Lays out the implementation steps in the chart and in the operator installation.

Upgrading the CNPG operator itself (1.27 → 1.30) is tracked separately in DataONEorg/k8s-cluster #90 because it first needs a Kubernetes upgrade. This issue does not depend on that work. Everything here runs on the operator we already have (CNPG 1.27). The Prep Step below must be finished before the operator upgrade, otherwise, after the operator upgrade, any newly created, rebuilt or restored cluster without an explicit image would silently come up on PG 18.


1. Current behavior

How the image gets chosen
  • templates/pg_cluster.yaml only renders spec.imageName when imageOverrideSpec is set. Almost every release leaves it empty. The README recommends leaving it empty (overriding is "NOT recommended") so that the operator can "manage Postgres minor-version upgrades automatically." That doesn't actually happen:

  • When a Cluster is created with no imageName and no imageCatalogRef, CNPG's defaulting webhook writes the operator's current default image into the spec and saves it on the resource. From api/v1/cluster_defaults.go (identical in release-1.27 and main):

    // Defaulting the image name if not specified
    if r.Spec.ImageName == "" && r.Spec.ImageCatalogRef == nil {
        r.Spec.ImageName = configuration.Current.PostgresImageName
    }
    
  • On later helm upgrades, imageName is absent from both the previous and the new rendered manifest. Helm's three-way merge therefore leaves the operator-written field untouched, and the cluster stays on its original image indefinitely.

  • Upgrading the operator restarts every cluster to update the instance manager inside each pod. That restart does not change the PostgreSQL image of existing clusters.

Net effect: nothing in our current setup ever performs a PG minor upgrade. The README's "Note: If you leave this value empty, cnpg will default to the initial operator install/setup" is accurate. The recommendation above it is not.

Current inventory (2026-11-19)
Cluster On 17.5 Other
dev-k8s arctic, brooke, keycloak, notify, ogdc, vegbank-dev, vegbank (7) metadig/metadig-pg on 15.6
prod-k8s arctic, demo, esa, goa, keycloak, knb, mn-ucsb-1, mn-unm-1, ogdc, opc, sdr, vegbank (12) metadig/metadig-pg on 15.6

Every cluster above except metadig-pg reports spec.imageName: ghcr.io/cloudnative-pg/postgresql:17.5. That is the legacy unsuffixed tag of the system image. I believe it is Debian bookworm based; we verify this in the Prep Step.

The metadig and keycloak charts create their CNPG Cluster resources themselves, not through dataone-cnpg. Migrating them onto this chart is out of scope and can happen later. However, they have the same unpinned-image hazard, so as part of this issue they need an explicit spec.imageName in their own charts. The 15.6 → current 15.x upgrade for metadig is a follow-up.

Latent hazards in the current design

These get worse once the operator is upgraded (DataONEorg/k8s-cluster #90), because the CNPG 1.30 default image is PG 18 on Debian trixie:

  1. New or rebuilt clusters silently get a new major version. Any cluster created with imageOverrideSpec empty after the operator upgrade gets PG 18. This includes the restore paths init.method: recovery and snapshot_recovery. Restoring a PG 17 snapshot onto a PG 18 image will fail.
  2. Clearing a previously set imageOverrideSpec triggers a major upgrade. Helm removes imageName, the webhook fills it in again with the current default, and the operator then attempts an in-place 17 → 18 pg_upgrade across a Debian release change.
  3. Drift. Clusters created at different times run different versions, and nothing in Git records which.
Image variant and OS considerations
  • system images (which bundle Barman Cloud) are deprecated upstream and will be removed along with in-core Barman Cloud support. This chart only uses volumeSnapshot backups, so the standard image variant is the right target.
  • Our databases are created with localeProvider: icu. Changing Debian release (bookworm → trixie) changes the glibc and ICU library versions. That can change collation order and require a REINDEX of text indexes. CNPG's in-place pg_upgrade also requires the same OS distribution on both sides.
    • Decision: stay on bookworm for the 17.x line (17.12-standard-bookworm).
    • Handle the move to trixie together with a future PG 18 migration, using the chart's dump/restore import path, which rebuilds indexes anyway.

2. Proposed pathways

Pathway A: explicit image.name per release

Every release pins its image explicitly in values (imageOverrideSpec, to be renamed image.name). The chart templates change to require an image value: the chart refuses to render without one. That way we no longer rely on the operator's default and the risks that come with it. A minor upgrade is a values change plus helm upgrade, release by release.

Pros

  • Simple. No new cluster-wide resources, and it works with our current operator.
  • Each release's exact version is visible in its values file in Git and is reviewable in a PR.
  • Each upgrade is limited to one release. Rollout order is entirely under our control.
  • Rollback is a one-line revert.

Cons

  • Nineteen clusters × every minor release means a lot of manual edits and deploys, which won't happen reliably. This is how we ended up seven minors behind.
  • Versions will drift between releases over time.
  • Pinning by digest gets noisy to maintain by hand.
Pathway B: ClusterImageCatalog

A small number of cluster-scoped ClusterImageCatalog resources, owned by the operator installation, map major → image. Each catalog therefore decides which minor version is installed for a given PostgreSQL major version. Each release references a catalog and a major version (image.catalog). A minor upgrade is one edit to a catalog entry, and CNPG rolls it out to every cluster that references that entry. If an application needs a different image (e.g. metadig), it can create its own namespaced ImageCatalog with the versions it wants and reference it with kind: ImageCatalog.

Pros

  • One change upgrades the fleet, so keeping current becomes routine. PostgreSQL minor upgrades are binary-compatible drop-in replacements and have historically been painless for us, so we expect this to be smooth. We should still read each minor release's notes for post-upgrade actions (see A.1).
  • The major version is still explicit per release. Restores and new clusters land on the intended major version, never on the operator default.
  • Catalogs can be split into tiers (dev/stable) for dev-first rollouts.
  • Upstream publishes official catalogs pinned by digest, and CNPG's own docs say imageCatalogRef will eventually replace imageName as the default method.
  • The same mechanism is how we'll later move a release to PG 18 (major: 18).

Cons

  • A catalog edit affects every referencing cluster across namespaces. Problems spread, so we need tiering and rollout delays.
  • Adds a cluster-scoped resource with its own ownership and change process, which belongs in the operator/infra repo, not this chart.
  • The exact image each release runs is no longer visible in its values file. It's in the catalog plus cluster status.
  • Migrating existing releases needs care (see B.2): CNPG rejects a Cluster that has both imageName and imageCatalogRef set.
Recommendation

Do the Prep Step now, then upgrade to 17.12 using Pathway A. This gets every cluster onto the current minor release as quickly as possible with the least new machinery.

Pathway B likely needs more testing than A, so we follow up by trialing it systematically on dev-k8s. If the trial goes well, we move releases onto the catalogs. If anything goes awry, the clusters stay on Pathway A pinning.

The Prep Step removes the hazards above whichever pathway we choose, and B depends on it anyway.


3. Implementation steps

Prep Step: shared prerequisites (do now, on the current CNPG 1.27 operator)
  1. Confirm image details.
    • Confirm the Debian release behind the running postgresql:17.5 image.
    • Confirm the exact target tag and digest, e.g. ghcr.io/cloudnative-pg/postgresql:17.12-standard-bookworm@sha256:….
  2. Ship the chart changes in §4.1: explicit image value, validation, and the README fix. Release a new chart version. Catalog support can ship in the same release or later with the Pathway B trial.
  3. Pin every release to what it runs today.
    • Set the image value to ghcr.io/cloudnative-pg/postgresql:17.5 in each release's values. That may be a standalone release or a subchart key in the metacat/etc. charts.
    • For keycloak and metadig, set spec.imageName in their own charts.
    • Run helm diff first to confirm the only change is the addition of spec.imageName, then helm upgrade each one.
    • Expected result: no pod restarts, because the spec value does not change, but Helm now owns spec.imageName.
  4. Back up each cluster immediately before any image change, with a one-off Backup using method: volumeSnapshot. Confirm the backup's phase is completed.
Pathway A steps
  1. Read the PostgreSQL release notes for 17.6 through 17.12.
    • Note any "post-upgrade" actions, such as reindexing specific index types affected by a fixed bug, and add them to the runbook.
  2. On one dev cluster (e.g. vegbank-dev/vegbankdb2-cnpg), set the image to 17.12-standard-bookworm@sha256:… and helm upgrade.
    • CNPG replaces replicas one by one, then switches over to or restarts the primary.
    • Don't change PostgreSQL parameters in the same release. With primaryUpdateMethod: switchover, CNPG rejects simultaneous image and parameter changes.
  3. Verify:
    • kubectl cnpg status <rel>-cnpg
    • SELECT version();
    • application smoke tests
    • check the logs for collation-version warnings; none are expected, since the OS doesn't change
  4. Roll out the rest.
    • Upgrade the remaining dev releases.
    • Wait for an agreed soak period (e.g. one week).
    • Upgrade prod one release at a time. Start with lower-traffic member nodes, and do knb, arctic and keycloak last.
  5. Repeat for each future minor release. Optionally automate the PRs with Renovate against the image value.
Pathway B steps
  1. Create catalogs in the operator installation (see §4.2): dataone-pg-bookworm-dev and dataone-pg-bookworm-stable.
    • Each initially maps major: 17 to the exact image clusters run at that point (17.12 if Pathway A has completed).
  2. Switch releases from image.name to image.catalog.
    • Set image.catalog: {kind: ClusterImageCatalog, name: dataone-pg-bookworm-<tier>, major: 17} and clear image.name.
    • Assign dev releases to dev and prod releases to stable.
    • The Prep Step made spec.imageName Helm-owned, so the three-way merge removes it in the same patch that adds spec.imageCatalogRef.
    • The resolved image is unchanged, so this should not trigger a rollout. Verify on one dev cluster first.
  3. Upgrade the dev catalog entry to the next minor release's digest. All dev clusters roll out, spaced by CLUSTERS_ROLLOUT_DELAY. Verify as in A.3.
  4. After the soak period, upgrade the stable catalog to the same digest and watch the prod rollouts.
  5. Ongoing:
    • Each new PG minor release is a PR against the catalog manifests: dev first, then stable.
    • A future major upgrade for a release is a values change to major: 18. That needs an 18 entry on the same OS, or a dump/restore into a new release if we are also moving to trixie.

4. Required changes

4.1 dataone-cnpg Helm chart
  • Values: replace imageOverrideSpec with an image block, and keep the old key working for one release cycle:

    image:
      ## Option A: explicit image (tag + digest recommended)
      name: ""        # e.g. ghcr.io/cloudnative-pg/postgresql:17.12-standard-bookworm@sha256:...
      ## Option B: image catalog reference (mutually exclusive with image.name)
      catalog: {}     # e.g. {kind: ClusterImageCatalog, name: dataone-pg-bookworm-stable, major: 17}
    
  • Template (pg_cluster.yaml):

    • Resolve image.name, falling back to imageOverrideSpec. If set, render spec.imageName.

    • If image.catalog is set, render:

      imageCatalogRef:
        apiGroup: postgresql.cnpg.io
        kind: {{ .kind | default "ClusterImageCatalog" }}
        name: {{ .name }}
        major: {{ .major }}
      
    • fail if both are set.

    • fail if neither is set. This is what removes the "silently gets the operator default" hazard.

  • Optional: expose primaryUpdateStrategy / primaryUpdateMethod, so releases can choose switchover or restart, and supervised or unsupervised, for the primary.

  • Versioning: requiring an image value breaks anyone who relies on the default, so this needs a major chart version bump (2.0.0).

  • Docs and examples:

    • Rewrite the README "Postgres Image Version" section: there are no automatic upgrades; explain how to pin, how catalogs work, and the bookworm/trixie collation caveat.
    • Add an "Upgrading PostgreSQL" section covering minor upgrades, and major upgrades via in-place pg_upgrade vs. dump/restore import.
    • Update every file in examples/ to set an image explicitly.
    • Call out the restore hazard in the recovery sections: the restore image's major version must match the backup.
  • Testing: helm template cases covering name only, catalog only, both (must fail), neither (must fail), and legacy imageOverrideSpec.

4.2 CNPG operator installation (k8s-cluster repo / operator deployment)

This section only covers what the PostgreSQL image work needs. Operator version and configuration changes for the upgrade are in DataONEorg/k8s-cluster #90.

  • Image catalogs (Pathway B):
    • Add ClusterImageCatalog manifests dataone-pg-bookworm-dev and dataone-pg-bookworm-stable next to the operator install. Give them entries for major: 17 (and 18 when needed), pinned by digest.
    • Alternatively, vendor the official catalog-standard-bookworm.yaml from cloudnative-pg/artifacts at a pinned commit and derive the two tiers from it.
    • Catalogs must not live in the per-release chart. They are cluster-scoped and would conflict across Helm releases.
  • Rollout pacing (Pathway B): set CLUSTERS_ROLLOUT_DELAY (and optionally INSTANCES_ROLLOUT_DELAY) in the operator config (cnpg-controller-manager-config). A catalog edit then rolls clusters out one after another, not all at once.
  • Ownership: name an owner and a cadence for reviewing new PG minor releases. PostgreSQL ships quarterly releases plus out-of-cycle security releases. Consider Renovate or Dependabot on the catalog manifests.

Open questions for review

  1. Is the dev/stable split enough, or do we want a per-namespace ImageCatalog for releases that need to hold back?
    • My initial thought is that the cluster-wide catalogs are enough.
  2. Which releases use this chart as a subchart (metacat, …)? Each needs its values updated in the Prep Step.
    • Some charts set up CNPG themselves (metadig, keycloak). They should be migrated to dataone-cnpg later, but they still get an explicit spec.imageName now.
  3. What soak period between dev and prod? What order for prod, and in which maintenance windows?

References

Lingua principale
Shell
Stelle
0
Fork
0
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di DataONEorg/dataone-cnpg

Tutte le issue di DataONEorg/dataone-cnpg

Issue simili

Altre issue su Shell/Bash

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.