-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathdestroy.sh
executable file
·78 lines (66 loc) · 3.5 KB
/
destroy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
set -uo pipefail
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOTDIR="$(cd ${SCRIPTDIR}/../..; pwd )"
[[ -n "${DEBUG:-}" ]] && set -x
scale_down_karpenter_nodes() {
# Get all nodes with the label karpenter.sh/registered=true
nodes=$(kubectl get nodes -l karpenter.sh/registered=true -o jsonpath='{.items[*].metadata.name}')
# Iterate over each node
for node in $nodes; do
# Get all pods running on the current node
pods=$(kubectl get pods --all-namespaces --field-selector spec.nodeName=$node -o jsonpath='{range .items[*]}{.metadata.namespace}{" "}{.metadata.name}{"\n"}{end}')
# Iterate over each pod
while IFS= read -r pod; do
namespace=$(echo $pod | awk '{print $1}')
pod_name=$(echo $pod | awk '{print $2}')
# Get the owner references of the pod
owner_refs=$(kubectl get pod $pod_name -n $namespace -o jsonpath='{.metadata.ownerReferences[*]}')
# Check if the owner is a ReplicaSet (which is part of a deployment) or a StatefulSet and scale down
if echo $owner_refs | grep -q "ReplicaSet"; then
replicaset_name=$(kubectl get pod $pod_name -n $namespace -o jsonpath='{.metadata.ownerReferences[?(@.kind=="ReplicaSet")].name}')
deployment_name=$(kubectl get replicaset $replicaset_name -n $namespace -o jsonpath='{.metadata.ownerReferences[?(@.kind=="Deployment")].name}')
if [[ $(kubectl get deployment $deployment_name -n $namespace -o jsonpath='{.spec.replicas}') -gt 0 ]]; then
echo kubectl scale deployment $deployment_name -n $namespace --replicas=0
kubectl scale deployment $deployment_name -n $namespace --replicas=0
fi
elif echo $owner_refs | grep -q "StatefulSet"; then
statefulset_name=$(kubectl get pod $pod_name -n $namespace -o jsonpath='{.metadata.ownerReferences[?(@.kind=="StatefulSet")].name}')
if [[ $(kubectl get statefulset $statefulset_name -n $namespace -o jsonpath='{.spec.replicas}') -gt 0 ]]; then
echo kubectl scale statefulset $statefulset_name -n $namespace --replicas=0
kubectl scale statefulset $statefulset_name -n $namespace --replicas=0
fi
fi
done <<< "$pods"
done
# Loop through each node and delete it
for node in $nodes; do
echo "Deleting node: $node"
kubectl delete node $node
done
# do a final check to make sure the nodes are gone, loop sleep 60 in between checks
nodes=$(kubectl get nodes -l karpenter.sh/registered=true -o jsonpath='{.items[*].metadata.name}')
while [[ ! -z $nodes ]]; do
echo "Waiting for nodes to be deleted: $nodes"
sleep 60
nodes=$(kubectl get nodes -l karpenter.sh/registered=true -o jsonpath='{.items[*].metadata.name}')
done
sleep 60
}
# Delete the Ingress/SVC before removing the addons
TMPFILE=$(mktemp)
terraform -chdir=$SCRIPTDIR output -raw configure_kubectl > "$TMPFILE"
# check if TMPFILE contains the string "No outputs found"
if [[ ! $(cat $TMPFILE) == *"No outputs found"* ]]; then
source "$TMPFILE"
scale_down_karpenter_nodes
kubectl delete ing -A --all
# delete all the kuberneters service of type LoadBalancer, without using jq
kubectl get svc --all-namespaces -o json | grep -E '"type": "LoadBalancer"' | awk '{print "kubectl delete svc " $1 " -n " $2}' | bash
sleep 60
fi
terraform destroy -target="module.gitops_bridge_bootstrap" -auto-approve
terraform destroy -target="module.eks_blueprints_addons" -auto-approve
terraform destroy -target="module.eks" -auto-approve
terraform destroy -target="module.vpc" -auto-approve
terraform destroy -auto-approve