Files
cozystack/examples/backups/postgres/cleanup.sh
T
Alexey Artamonov 6d1eeba65b fix(postgres): bound a backup whose cnpg Backup stalls in pending
CloudNativePG keeps a Backup pending either behind a started or running
backup of the same Cluster, which is healthy, or because the instance to
run it on cannot be found or is not ready, which lasts as long as the
Cluster is fenced. The BackupJob waited on both forever.

The driver now tells the two apart by whether any backup of the Cluster
is started or running. It does not model which pending Backup
CloudNativePG lets through first. With none running, the job records a
CNPGBackupStalled condition and fails once that has lasted the backup
deadline. Before it gives up it reads the Cluster's backups again from
the apiserver, since the cache can still show pending one that has just
started. Its own Backup is deleted, conditionally on the version read so
a Backup CloudNativePG has just started is kept, because otherwise it
would run later with no Backup recording it. The give-up is persisted
before the delete, so a Backup found missing afterwards fails the job
only when the driver removed it; one deleted by hand is replaced and its
stall counts afresh.

The reason CloudNativePG gives exists only as an event on a Backup, so
the driver reads the newest Warning on its own Backup, or on another
pending backup of the Cluster when its own carries none, by UID and
uncached, which needs list on events but no watch.

A job is no longer held on the plugin rollout another job started when
the Cluster is fenced and every instance pod postdates that attach: a
fenced Cluster is neither healthy nor ready until the fence is lifted,
so waiting on that would only delay the bound by up to the rollout
deadline.

The postgres e2e fences the source Cluster, checks that a BackupJob
reports the stall with that reason, then lifts the fence and checks the
same job completes with the stall ended. That exercises the events grant
and the involvedObject.uid selector against a real apiserver. The step
creates the BackupJob only once every instance pod has gone NotReady,
since the fence takes effect asynchronously and CloudNativePG starts a
Backup on a target that is still Ready.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
2026-11-25 16:01:48 +03:00

63 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# Best-effort teardown of everything the demo creates. Idempotent;
# missing resources are skipped silently.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/00-helpers.sh"
print_header "Cleanup"
# The CNPG driver suspends the source/target Postgres HelmRelease while a
# RestoreJob runs. If the demo was aborted mid-restore the HR is left
# suspended; deleting the RestoreJob without resuming it first strands the app.
# Resume both best-effort BEFORE deleting the RestoreJobs.
for app in "$PG_SRC_NAME" "$PG_TARGET_NAME"; do
if kubectl -n "$NAMESPACE" get hr "postgres-${app}" >/dev/null 2>&1; then
log_substep "Resuming HelmRelease postgres-${app} (no-op if not suspended)..."
kubectl -n "$NAMESPACE" patch hr "postgres-${app}" --type=merge \
-p '{"spec":{"suspend":false}}' >/dev/null || true
fi
done
# Run-all step 26 fences the source Cluster; an aborted run leaves it fenced.
if kubectl get clusters.postgresql.cnpg.io "$PG_SRC_CLUSTER" --namespace "$NAMESPACE" >/dev/null 2>&1; then
log_substep "Lifting the fence on ${PG_SRC_CLUSTER} (no-op if not fenced)..."
kubectl annotate clusters.postgresql.cnpg.io "$PG_SRC_CLUSTER" --namespace "$NAMESPACE" \
'cnpg.io/fencedInstances-' >/dev/null || true
fi
log_substep "Deleting RestoreJobs..."
kubectl -n "$NAMESPACE" delete restorejob.backups.cozystack.io \
"$RESTOREJOB_TOCOPY_NAME" "$RESTOREJOB_PITR_NAME" "$RESTOREJOB_UNREACHABLE_NAME" \
"$RESTOREJOB_INPLACE_NAME" --ignore-not-found
log_substep "Deleting Plan + BackupJob + derived Backup artefacts..."
kubectl -n "$NAMESPACE" delete plan.backups.cozystack.io "$PLAN_NAME" --ignore-not-found
kubectl -n "$NAMESPACE" delete backupjob.backups.cozystack.io \
"$BACKUPJOB_NAME" "$BACKUPJOB_POSTMARKER_NAME" "$BACKUPJOB_FENCED_NAME" --ignore-not-found
# Only the Backups of THIS demo's apps — never `--all`: on a shared cluster the
# namespace can hold Backup artefacts of other applications and flows.
for b in $(kubectl -n "$NAMESPACE" get backups.backups.cozystack.io \
-o jsonpath='{range .items[*]}{.metadata.name} {.spec.applicationRef.name}{"\n"}{end}' 2>/dev/null |
awk -v src="$PG_SRC_NAME" -v tgt="$PG_TARGET_NAME" '$2 == src || $2 == tgt {print $1}'); do
kubectl -n "$NAMESPACE" delete backups.backups.cozystack.io "$b" --ignore-not-found
done
log_substep "Deleting Postgres apps..."
kubectl -n "$NAMESPACE" delete postgres.apps.cozystack.io "$PG_SRC_NAME" "$PG_TARGET_NAME" --ignore-not-found
log_substep "Deleting per-app backup Secrets..."
kubectl -n "$NAMESPACE" delete secret \
"${PG_SRC_NAME}-cnpg-backup-creds" "${PG_SRC_NAME}-cnpg-backup-ca" \
"${PG_TARGET_NAME}-cnpg-backup-creds" "${PG_TARGET_NAME}-cnpg-backup-ca" --ignore-not-found
log_substep "Deleting Bucket..."
kubectl -n "$NAMESPACE" delete bucket.apps.cozystack.io "$BUCKET_NAME" --ignore-not-found
log_substep "Deleting BackupClass + CNPG strategy (cluster-scoped)..."
kubectl delete backupclass.backups.cozystack.io "$BACKUPCLASS_NAME" --ignore-not-found
kubectl delete cnpg.strategy.backups.cozystack.io "$STRATEGY_NAME" --ignore-not-found
log_success "Cleanup complete."