11#! /bin/bash
22
33environment=$1
4+ cluster=${2:- all}
5+ echo " Destroying infrastructure for environment ${environment} and cluster ${cluster} "
46
57set -Eeuo pipefail
68set -u
79
810SCRIPT_DIR=$( cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " & > /dev/null && pwd )
9-
11+ export USE_CURRENT_USER=1 ;
1012source $SCRIPT_DIR /lib/common-env.sh
1113
1214cluster_exists=0
1315aws eks describe-cluster --name " ${EKS_CLUSTER_NAME} " & > /dev/null || cluster_exists=$?
1416
15- if [ $cluster_exists -eq 0 ]; then
17+ if [ $cluster_exists -eq 0 ] && [[ " $cluster " == " standard " || " $cluster " == " all " ]] ; then
1618 echo " Deleting cluster ${EKS_CLUSTER_NAME} "
1719 bash $SCRIPT_DIR /shell.sh " ${environment} " ' delete-environment' || true
1820 bash $SCRIPT_DIR /exec.sh " ${environment} " ' eksctl delete cluster --name ${EKS_CLUSTER_NAME} --region ${AWS_REGION} --wait --force --disable-nodegroup-eviction --timeout 45m' &
1921else
20- echo " Cluster ${EKS_CLUSTER_NAME} does not exist"
22+ echo " Cluster ${EKS_CLUSTER_NAME} does not exist or skipped "
2123fi
2224
2325export EKS_CLUSTER_AUTO_NAME=" ${EKS_CLUSTER_NAME} -auto"
2426auto_cluster_exists=0
2527aws eks describe-cluster --name " ${EKS_CLUSTER_AUTO_NAME} " & > /dev/null || auto_cluster_exists=$?
2628
27- if [ $auto_cluster_exists -eq 0 ]; then
29+ if [ $auto_cluster_exists -eq 0 ] && [[ " $cluster " == " auto " || " $cluster " == " all " ]] ; then
2830 echo " Deleting auto mode cluster ${EKS_CLUSTER_AUTO_NAME} "
29- bash $SCRIPT_DIR /shell.sh " ${environment} " ' delete-environment' || true # Needed ?
31+ # bash $SCRIPT_DIR/shell.sh "${environment}" 'delete-environment' || true # Needed ?
3032 bash $SCRIPT_DIR /exec.sh " ${environment} " ' eksctl delete cluster --name ${EKS_CLUSTER_AUTO_NAME} --region ${AWS_REGION} --wait --force --disable-nodegroup-eviction --timeout 45m'
3133else
32- echo " Auto mode cluster ${EKS_CLUSTER_AUTO_NAME} does not exist"
34+ echo " Auto mode cluster ${EKS_CLUSTER_AUTO_NAME} does not exist or skipped "
3335fi
3436
3537wait
3638
37- aws cloudformation delete-stack --stack-name ${EKS_CLUSTER_NAME} -ide-role || true
39+ # Only delete ide-role if all clusters are deleted
40+ if [ " $cluster " == " all" ]; then
41+ aws cloudformation delete-stack --stack-name ${EKS_CLUSTER_NAME} -ide-role || true
42+ fi
0 commit comments