Files
aevolution/tools/ops/cleanup-environment.sh
dingoandClaude Opus 5.5 df0c4f4efb Add M8.2 environment isolation and World0 protection
Per-environment platform layers (dev, stage, world0) with restricted pod
security, quotas, limit ranges, default-deny networking and token-less
service accounts, applied by an administrator rather than Argo. A native
ValidatingAdmissionPolicy denies deleting World0 data, identity and Argo
objects to every requester without the break-glass group. Stage and
World0 Argo projects are scoped to one namespace, World0 blocks
automated sync, and a cleanup tool refuses World0. An offline policy
check runs as a new CI suite; a live verification script uses only
can-i, reads and server dry runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 12:43:58 -04:00

43 lines
2.0 KiB
Bash

#!/usr/bin/env bash
# M8.2 environment cleanup for disposable AEvolution environments only.
#
# tools/ops/cleanup-environment.sh <dev|stage> [--execute] [--context <name>]
#
# Removes AEvolution workload objects (Deployments, StatefulSets, Jobs, Services,
# ConfigMaps and PersistentVolumeClaims labelled app.kubernetes.io/part-of=aevolution)
# from evolution-<env>. 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 <dev|stage> [--execute] [--context <name>]" >&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"