-
Notifications
You must be signed in to change notification settings - Fork 15
/
common.sh
executable file
·62 lines (48 loc) · 2.21 KB
/
common.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
#!/usr/bin/env bash
set -uo pipefail
[[ -n "${DEBUG:-}" ]] && set -x
scale_down_karpenter_nodes() {
# Delete the nodeclaims
echo "Deleting Karpeneter NodePools"
kubectl delete nodepools.karpenter.sh --all
# 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
kubectl delete nodepools.karpenter.sh --all
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
}
# This is required for certain resources that are not managed by Terraform
force_delete_vpc() {
VPC_NAME=$1
VPCID=$(aws ec2 describe-vpcs --filters "Name=tag:Name,Values=${VPC_NAME}" --query "Vpcs[*].VpcId" --output text)
if [ -n "$VPCID" ]; then
echo "VPC ID: $VPCID"
echo "Cleaning VPC endpoints if exists..."
# Use AWS_REGION if set, otherwise default to the region from AWS CLI configuration
REGION=${AWS_REGION:-$(aws configure get region)}
vpc_endpoint_names=(
"com.amazonaws.$REGION.guardduty-data"
"com.amazonaws.$REGION.ssm"
"com.amazonaws.$REGION.ec2messages"
"com.amazonaws.$REGION.ssmmessages"
"com.amazonaws.$REGION.s3"
)
for endpoint_name in "${vpc_endpoint_names[@]}"; do
endpoint_exists=$(aws ec2 describe-vpc-endpoints --filters "Name=service-name,Values=$endpoint_name" "Name=vpc-id,Values=$VPCID" --query "VpcEndpoints[*].VpcEndpointId" --output text 2>/dev/null)
if [ -n "$endpoint_exists" ]; then
echo "Deleting VPC endpoint $endpoint_exists..."
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids "$endpoint_exists"
fi
done
# check if aws-delete-vpc is available if not install it with go install github.com/megaproaktiv/aws-delete-vpc
if ! command -v aws-delete-vpc &> /dev/null; then
echo "aws-delete-vpc could not be found, installing it..."
go install github.com/isovalent/aws-delete-vpc@latest
fi
echo "Cleaning VPC $VPCID"
aws-delete-vpc -vpc-id=$VPCID
fi
}