@@ -21,32 +21,64 @@ if [[ -z "${EKS_CLUSTER_K8_VERSION}" ]]; then
2121fi
2222
2323function deleteCluster() {
24+ echo " Cleanup role, security-group, open-id ${TEST_CLUSTER_NAME} "
25+ account_id=$( aws sts get-caller-identity --query " Account" --output text)
26+ rolename=$( echo ${TEST_CLUSTER_NAME} | awk -F- ' {print "EBS_" $(NF-1) "_" $(NF)}' )
27+
28+ # Detach role policies
29+ role_attached_policies=$( aws iam list-attached-role-policies --role-name $rolename --query ' AttachedPolicies[*].PolicyArn' --output text)
30+ for policy_arn in ${role_attached_policies} ; do
31+ aws iam detach-role-policy --role-name ${rolename} --policy-arn ${policy_arn}
32+ done
33+
34+ # Delete IAM role
35+ aws iam delete-role --role-name ${rolename}
36+
37+ # Delete OIDC provider
38+ oidc_id=$( aws eks describe-cluster --name ${TEST_CLUSTER_NAME} --query " cluster.identity.oidc.issuer" --output text | cut -d ' /' -f 5)
39+ aws iam delete-open-id-connect-provider --open-id-connect-provider-arn arn:aws:iam::${account_id} :oidc-provider/oidc.eks.us-west-2.amazonaws.com/id/${oidc_id}
40+
41+ # Get security group ID
42+ security_group_id=$( aws eks describe-cluster --name ${TEST_CLUSTER_NAME} --query " cluster.resourcesVpcConfig.securityGroupIds[0]" --output text)
43+
44+ # Cleanup remaining PVCs on the EKS Cluster
2445 echo " Cleanup remaining PVC on the EKS Cluster ${TEST_CLUSTER_NAME} "
2546 tools/cleanup.sh
47+
48+ # Get node group
2649 NODE_GROUP=$( eksctl get nodegroup --cluster=${TEST_CLUSTER_NAME} | sed -n 4p | awk ' { print $2 }' )
27- if [[ ! -z " ${NODE_GROUP} " ]]; then
28- eksctl delete nodegroup --cluster=${TEST_CLUSTER_NAME} --name=${NODE_GROUP}
29- if [ $? -ne 0 ]; then
30- echo " Unable to delete Nodegroup ${NODE_GROUP} . For Cluster - ${TEST_CLUSTER_NAME} "
31- fi
32- fi
33- eksctl delete cluster --name=${TEST_CLUSTER_NAME}
50+
51+ # Delete the node group to ensure no EC2 instances are using the security group
52+ echo " Deleting node group - ${NODE_GROUP} "
53+ eksctl delete nodegroup --cluster=${TEST_CLUSTER_NAME} --name=${NODE_GROUP}
54+
55+ # Delete cluster
56+ echo " Deleting cluster - ${TEST_CLUSTER_NAME} "
57+ eksctl delete cluster --name ${TEST_CLUSTER_NAME}
58+
3459 if [ $? -ne 0 ]; then
3560 echo " Unable to delete cluster - ${TEST_CLUSTER_NAME} "
3661 return 1
3762 fi
38- rolename=$( echo ${TEST_CLUSTER_NAME} | awk -F- ' {print "EBS_" $(NF-1) "_" $(NF)}' )
39- role_attached_policies=$( aws iam list-attached-role-policies --role-name $rolename --query ' AttachedPolicies[*].PolicyArn' --output text)
40- for policy_arn in ${role_attached_policies} ;
41- do
42- aws iam detach-role-policy --role-name ${rolename} --policy-arn ${policy_arn}
63+
64+ # Wait for the cluster resources to be fully released before deleting security group
65+ echo " Waiting for resources to be detached from security group - ${security_group_id} "
66+ while true ; do
67+ ENIs=$( aws ec2 describe-network-interfaces --filters " Name=group-id,Values=${security_group_id} " --query " NetworkInterfaces[*].NetworkInterfaceId" --output text)
68+ if [ -z " ${ENIs} " ]; then
69+ break
70+ fi
71+ echo " ENIs still attached to security group: ${ENIs} . Waiting for cleanup..."
72+ sleep 10
4373 done
4474
45- aws iam delete-role --role-name ${rolename}
75+ # Delete security group
76+ aws ec2 delete-security-group --group-id ${security_group_id}
4677
4778 return 0
4879}
4980
81+
5082function createCluster() {
5183 # Deploy eksctl cluster if not deploy
5284 rc=$( which eksctl)
0 commit comments