chore: enhance Supabase retained disk cleanup with project resolution and detailed skip reasons

- Updated `deploy.sh` to resolve project IDs from multiple configuration fallbacks and environment contexts for retained disk cleanup.
- Added support for `supabase_cfg_first_nonempty_value` to prioritize configuration over defaults.
- Enhanced logging with explicit reasons for skipped disk cleanup actions (e.g., missing gcloud, unresolved project).
- Updated tests to validate fallback logic and skip reason reporting.
This commit is contained in:
chrisfu 2026-04-19 17:16:49 -07:00
parent 39be87d3a7
commit 20692c988d

View File

@ -1,44 +1,109 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
PROJECT="plenary-truck-485623-p7" PROJECT="${PROJECT:-${GCP_PROJECT_ID:-${GOOGLE_CLOUD_PROJECT:-plenary-truck-485623-p7}}}"
DRY_RUN_RAW="${SUPABASE_RETAINED_PV_CLEANUP_DRY_RUN:-false}"
DRY_RUN="false"
if [[ "${DRY_RUN_RAW,,}" =~ ^(1|true|yes|on)$ ]]; then
DRY_RUN="true"
fi
echo "==> Finding stale Supabase PVs..." echo "==> Finding stale Supabase PVs (Released + Retain, Supabase namespace/storageClass)..."
echo "==> Dry run: ${DRY_RUN}"
echo "==> Project: ${PROJECT}"
kubectl get pv -o json mapfile -t CANDIDATES < <(
| jq -r ' kubectl get pv -o json | jq -r '
.items[] .items[]
| select( | . as $pv
.status.phase=="Released" | ($pv.spec.claimRef.namespace // "") as $claimNs
and .spec.storageClassName=="supabase-standard" | ($pv.spec.storageClassName // "") as $sc
) | select(
| [ ($pv.status.phase == "Released")
.metadata.name, and (($pv.spec.persistentVolumeReclaimPolicy // "") == "Retain")
.spec.csi.volumeHandle and (
] $sc == "supabase-standard"
| @tsv or $claimNs == "supabase"
' | while IFS=$'\t' read -r PV DISK; do )
)
| (
($pv.spec.csi.volumeHandle // $pv.spec.gcePersistentDisk.pdName // "")
) as $handle
| (
($pv.metadata.labels["topology.kubernetes.io/zone"]
// $pv.metadata.labels["failure-domain.beta.kubernetes.io/zone"]
// $pv.spec.csi.volumeAttributes["topology.gke.io/zone"]
// (
if ($handle | test("/zones/[^/]+/disks/[^/]+$"))
then ($handle | capture("/zones/(?<z>[^/]+)/disks/").z)
else ""
end
)
)
) as $zone
| [
($pv.metadata.name // ""),
$claimNs,
($pv.spec.claimRef.name // ""),
($pv.status.phase // ""),
($pv.spec.persistentVolumeReclaimPolicy // ""),
$sc,
$handle,
$zone
]
| @tsv
'
)
if [[ -z "$DISK" || "$DISK" == "null" ]]; then if [[ ${#CANDIDATES[@]} -eq 0 ]]; then
echo "[WARN] PV $PV has no disk handle, skipping" echo "==> No stale Supabase Released/Retain PV candidates found."
exit 0
fi
echo "==> Candidates:"
for row in "${CANDIDATES[@]}"; do
IFS=$'\t' read -r PV CLAIM_NS CLAIM_NAME PHASE RECLAIM SC DISK ZONE <<<"$row"
echo " - PV=${PV} claim=${CLAIM_NS}/${CLAIM_NAME:-<none>} phase=${PHASE} reclaim=${RECLAIM} sc=${SC} disk=${DISK:-<none>} zone=${ZONE:-<unknown>}"
done
for row in "${CANDIDATES[@]}"; do
IFS=$'\t' read -r PV CLAIM_NS CLAIM_NAME PHASE RECLAIM SC DISK ZONE <<<"$row"
NAME="${DISK##*/}"
if [[ "$DRY_RUN" == "true" ]]; then
echo "[dry-run] kubectl delete pv ${PV} --ignore-not-found"
if [[ -n "$DISK" && "$DISK" != "null" ]]; then
if [[ -n "$ZONE" ]]; then
echo "[dry-run] gcloud compute disks delete ${NAME} --zone ${ZONE} --project ${PROJECT} --quiet"
else
echo "[dry-run] skip disk delete for ${DISK} (missing zone)"
fi
else
echo "[dry-run] skip disk delete for PV ${PV} (no disk handle)"
fi
continue continue
fi fi
# extract zone + disk name echo "==> Deleting PV ${PV}"
ZONE=$(echo "$DISK" | awk -F/ '{print $(NF-2)}')
NAME=$(echo "$DISK" | awk -F/ '{print $NF}')
echo "==> Cleaning PV=$PV disk=$NAME zone=$ZONE"
echo " -> deleting PV"
kubectl delete pv "$PV" --ignore-not-found kubectl delete pv "$PV" --ignore-not-found
echo " -> deleting GCE disk" if [[ -z "$DISK" || "$DISK" == "null" ]]; then
gcloud compute disks delete "$NAME" echo "[WARN] PV ${PV} has no disk handle; skipped disk delete"
--zone "$ZONE" continue
--project "$PROJECT" fi
--quiet || echo "[WARN] disk delete failed: $NAME" if [[ -z "$ZONE" ]]; then
echo "[WARN] Disk ${DISK} zone is unknown; skipped disk delete"
continue
fi
if ! command -v gcloud >/dev/null 2>&1; then
echo "[WARN] gcloud not found; skipped disk delete for ${DISK}"
continue
fi
echo "==> Deleting GCE disk ${NAME} (zone=${ZONE})"
gcloud compute disks delete "$NAME" --zone "$ZONE" --project "$PROJECT" --quiet \
|| echo "[WARN] disk delete failed: ${NAME}"
done done
echo "==> Cleanup complete" echo "==> Cleanup complete"