|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Delete Gateway API resources BEFORE uninstalling the controller |
| 6 | +# so the LBC can clean up the ALB and target groups |
| 7 | +if kubectl api-resources --api-group=gateway.networking.k8s.io -o name 2>/dev/null | grep -q httproute; then |
| 8 | + kubectl delete httproute --all -A --ignore-not-found |
| 9 | + kubectl delete gateway --all -A --ignore-not-found --wait=false |
| 10 | + kubectl delete gatewayclass --all --ignore-not-found --wait=false |
| 11 | +fi |
| 12 | + |
| 13 | +# Delete AWS LBC Gateway CRDs resources |
| 14 | +if kubectl api-resources --api-group=gateway.k8s.aws -o name 2>/dev/null | grep -q targetgroupconfiguration; then |
| 15 | + kubectl delete targetgroupconfiguration --all -A --ignore-not-found |
| 16 | + kubectl delete loadbalancerconfiguration --all -A --ignore-not-found |
| 17 | + kubectl delete listenerruleconfiguration --all -A --ignore-not-found |
| 18 | +fi |
| 19 | + |
| 20 | +# Wait for Gateways to be fully removed (finalizers cleared by LBC) |
| 21 | +echo "Waiting for Gateway resources to be cleaned up..." |
| 22 | +timeout 120 bash -c 'while kubectl get gateway -A 2>/dev/null | grep -q .; do sleep 5; done' || true |
| 23 | + |
| 24 | +# Force-remove finalizers if still stuck |
| 25 | +for gw in $(kubectl get gateway -A -o jsonpath='{range .items[*]}{.metadata.namespace}/{.metadata.name}{"\n"}{end}' 2>/dev/null); do |
| 26 | + ns=$(echo $gw | cut -d/ -f1) |
| 27 | + name=$(echo $gw | cut -d/ -f2) |
| 28 | + kubectl patch gateway $name -n $ns --type=merge -p '{"metadata":{"finalizers":[]}}' 2>/dev/null || true |
| 29 | +done |
| 30 | +kubectl delete gateway --all -A --ignore-not-found 2>/dev/null || true |
| 31 | +kubectl delete gatewayclass --all --ignore-not-found 2>/dev/null || true |
| 32 | + |
| 33 | +# Now uninstall the controllers |
| 34 | +uninstall-helm-chart aws-load-balancer-controller kube-system |
| 35 | + |
| 36 | +# Clean up CRDs |
| 37 | +kubectl delete -f https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/refs/heads/main/config/crd/gateway/gateway-crds.yaml --ignore-not-found 2>/dev/null || true |
| 38 | +kubectl delete -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.2.0/standard-install.yaml --ignore-not-found 2>/dev/null || true |
0 commit comments