#!/usr/bin/env bash # M8.2 environment cleanup for disposable AEvolution environments only. # # tools/ops/cleanup-environment.sh [--execute] [--context ] # # Removes AEvolution workload objects (Deployments, StatefulSets, Jobs, Services, # ConfigMaps and PersistentVolumeClaims labelled app.kubernetes.io/part-of=aevolution) # from evolution-. Platform guardrails (namespace, quota, limit range, network # policies, service accounts) and Secrets are never touched. Without --execute every # delete is a server-side dry run. World0, or any namespace that is not labelled as # the named disposable environment or is labelled protected, is refused before any # change; the refusal of World0 happens before kubectl is even invoked. set -euo pipefail usage() { echo "usage: $0 [--execute] [--context ]" >&2; exit 2; } [ $# -ge 1 ] || usage env="$1"; shift case "$env" in dev|stage) ;; *) echo "refused: '$env' is not a disposable environment (only dev or stage; never world0)" >&2 exit 2 ;; esac dry_run=(--dry-run=server) k=(kubectl --request-timeout=30s) while [ $# -gt 0 ]; do case "$1" in --execute) dry_run=() ;; --context) [ $# -ge 2 ] || usage; k+=(--context "$2"); shift ;; *) usage ;; esac shift done ns="evolution-$env" labels="$("${k[@]}" get namespace "$ns" -o jsonpath='{.metadata.labels.aevolution\.io/environment}/{.metadata.labels.aevolution\.io/protected}')" if [ "$labels" != "$env/" ]; then echo "refused: namespace $ns labels are '$labels' (need aevolution.io/environment=$env and no aevolution.io/protected)" >&2 exit 2 fi selector='app.kubernetes.io/part-of=aevolution,app.kubernetes.io/managed-by!=aevolution-platform' "${k[@]}" delete -n "$ns" "${dry_run[@]}" --wait=false -l "$selector" \ deployments,statefulsets,jobs,services,configmaps,persistentvolumeclaims [ ${#dry_run[@]} -eq 0 ] && echo "deleted AEvolution workloads in $ns" || echo "dry run only (server-side); rerun with --execute to delete in $ns"