Skip to content

Fix/cluster reseed cnpg - #438

Merged
sabinem merged 3 commits into
mainfrom
fix/cluster-reseed-cnpg
Nov 19, 2026
Merged

sabinem merged 3 commits into
mainfrom
fix/cluster-reseed-cnpg

Conversation

@sabinem

@sabinem sabinem commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Operators can back up, wipe and reseed dev and staging again

Before: just cluster::reseed dev stopped with secrets "hackagon-backend-db" not found. The cluster recipes assumed the chart's bundled postgres.

Change: Dev and staging run on CloudNativePG, with a different Secret, different service names and no superuser. Now the recipes ask the running backend where its database is and how it logs in, then do everything as that same user through one port-forward.

  • Operators: just cluster::backup, wipe and reseed work on dev without kubectl exec or superuser access
  • backup dumps the app database only. Keycloak's database is no longer included, and these recipes never touch it.
  • wipe empties the app database in one transaction instead of dropping it
  • Refuses before anything changes when the database host points at another namespace, the app user doesn't own something it would drop, or the tunnel has closed
  • A run stays on the kubeconfig context it started with, even if you switch contexts in another terminal. Set HACKAGON_KUBE_CONTEXT to pick a context without switching to it.
  • The recipes run inside the Nix shell, which now includes yq

Mike Farah's yq (nixpkgs yq-go, binary `yq`), so recipes can read YAML
with a query instead of a hand-written parser. cluster.just is the first
to need it: it reads the backend's database settings out of its
ConfigMap.
cluster::backup, wipe and reseed assumed the chart's bundled postgres:
a StatefulSet to exec into, a superuser, and a Secret named
hackagon-backend-db. Dev runs CloudNativePG instead, so reseed stopped
on the missing Secret before touching anything.

The recipes now read where the database is and how to log in from the
backend itself: host, dbname and user from its ConfigMap, the password
from the Secret its Deployment references. Everything runs as that user
through a port-forward, so neither exec nor a superuser is needed:

- backup is a pg_dump of the app database. Keycloak's database is no
  longer in it; none of these recipes touches Keycloak.
- wipe drops everything in the backend's schema in one transaction
  instead of DROP DATABASE, which needs a superuser and may fight an
  operator that manages the database.
- Both refuse before changing anything if the user does not own every
  relation it would have to drop, or if the tunnel has closed.
- A database host naming another namespace is refused: the port-forward
  stays in the guarded one, so it would reach the wrong database.
- pg_dump and psql connect without TLS, as the backend does. Over TLS,
  postgres resets each connection as it closes, and kubectl drops the
  whole port-forward over one reset.
- The tunnel is ready when kubectl prints "Forwarding from", rather than
  when a probe connection gets through.

The public recipes run inside the Nix shell, which brings pg_dump and
psql.
@sabinem
sabinem requested a review from marftn November 18, 2026 10:28

@marftn marftn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good but there is a small potential race condition with the kubeconfig: if you switch the kubeconfig context from dev to prod in another terminal after the guard has passed (e.g., during build time), it will try to run on prod. This is unlikely to have an impact with the current setup because the namespace names also differ from dev to prod.

It can be fixed easily by resolving the context once at the start and passing it with --context to every kubectl call. The threat is quite low, so I'm also ok with leaving this out to avoid making the file more complicated. Let me know what you prefer :)

Every kubectl call used the kubeconfig's current context at the moment it
ran, so switching contexts in another terminal after the guard had passed
(during the seed build, say) sent the rest of the run to the new one.

The first recipe of a run now reads the current context once and exports
it as HACKAGON_KUBE_CONTEXT; the recipes it calls inherit it, and every
kubectl call passes it as --context. Setting HACKAGON_KUBE_CONTEXT up
front picks a context without switching to it.

@marftn marftn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now! Thanks 🙂 you can merge.

@sabinem
sabinem merged commit 70e47f9 into main Nov 19, 2026
4 checks passed
@sabinem
sabinem deleted the fix/cluster-reseed-cnpg branch November 19, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants