diff --git a/platform/cluster/flux/apps/data/postgres/agents-rename-job.yaml b/platform/cluster/flux/apps/data/postgres/agents-rename-job.yaml new file mode 100644 index 00000000..39f9bd49 --- /dev/null +++ b/platform/cluster/flux/apps/data/postgres/agents-rename-job.yaml @@ -0,0 +1,100 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: postgres-agents-rename + namespace: data-system + # The assistant subsystem was renamed to agents. The init script only + # runs on a fresh PGDATA, so the live volume still carries the old + # assistant_db / assistant_user objects. This one-shot reconcile renames + # them in place (preserving all data) so agents-api can reach agents_db + # as agents_user. Idempotent: re-running after the rename is a no-op, and + # on a fresh cluster (where init already created agents_db) it does + # nothing. The force annotation makes Flux recreate the Job whenever the + # spec hash drifts. + annotations: + kustomize.toolkit.fluxcd.io/force: enabled +spec: + backoffLimit: 3 + template: + metadata: + labels: + app.kubernetes.io/name: postgres-agents-rename + annotations: + vault.hashicorp.com/agent-inject: 'true' + vault.hashicorp.com/agent-inject-token: 'true' + vault.hashicorp.com/agent-pre-populate-only: 'true' + vault.hashicorp.com/role: postgres + vault.hashicorp.com/agent-inject-secret-postgres.env: secret/data/platform/postgres + vault.hashicorp.com/agent-inject-template-postgres.env: | + POSTGRES_USER={{ with secret "secret/data/platform/postgres" }}{{ index .Data.data "postgres.user" }}{{ end }} + POSTGRES_PASSWORD={{ with secret "secret/data/platform/postgres" }}{{ index .Data.data "postgres.password" }}{{ end }} + AGENTS_DB_USER={{ with secret "secret/data/platform/postgres" }}{{ index .Data.data "agents.user" }}{{ end }} + AGENTS_DB_PASSWORD={{ with secret "secret/data/platform/postgres" }}{{ index .Data.data "agents.password" }}{{ end }} + spec: + restartPolicy: OnFailure + serviceAccountName: postgres + nodeSelector: + personal-stack/site: frankfurt + containers: + - name: reconcile + image: postgres:17-alpine + imagePullPolicy: IfNotPresent + command: + - /bin/sh + - -ec + - | + set -a + . /vault/secrets/postgres.env + set +a + + export PGPASSWORD="${POSTGRES_PASSWORD}" + + # Role + database live at the cluster level, so connect to the + # default postgres database. ALTER DATABASE ... RENAME cannot run + # against the current database or with open connections to the + # target, neither of which applies here (the renamer is on + # postgres and the assistant stack is gone). + psql -v ON_ERROR_STOP=1 \ + --host=postgres.data-system.svc.cluster.local \ + --username="${POSTGRES_USER}" \ + --dbname=postgres < agents_db / ${AGENTS_DB_USER} reconciled." diff --git a/platform/cluster/flux/apps/data/postgres/kustomization.yaml b/platform/cluster/flux/apps/data/postgres/kustomization.yaml index 00877714..6f4173bf 100644 --- a/platform/cluster/flux/apps/data/postgres/kustomization.yaml +++ b/platform/cluster/flux/apps/data/postgres/kustomization.yaml @@ -6,4 +6,5 @@ resources: - config-configmap.yaml - deployment.yaml - wolfmanager-reconcile-job.yaml + - agents-rename-job.yaml - servicemonitor.yaml