Skip to content

Commit 85fdd4f

Browse files
committed
adding wait for network rsr deletion
Signed-off-by: vivekr-splunk <[email protected]>
1 parent 396c5cf commit 85fdd4f

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

test/deploy-eks-cluster.sh

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,61 @@ function deleteCluster() {
2424
echo "Cleanup role, security-group, open-id ${TEST_CLUSTER_NAME}"
2525
account_id=$(aws sts get-caller-identity --query "Account" --output text)
2626
rolename=$(echo ${TEST_CLUSTER_NAME} | awk -F- '{print "EBS_" $(NF-1) "_" $(NF)}')
27+
28+
# Detach role policies
2729
role_attached_policies=$(aws iam list-attached-role-policies --role-name $rolename --query 'AttachedPolicies[*].PolicyArn' --output text)
28-
for policy_arn in ${role_attached_policies};
29-
do
30+
for policy_arn in ${role_attached_policies}; do
3031
aws iam detach-role-policy --role-name ${rolename} --policy-arn ${policy_arn}
3132
done
3233

34+
# Delete IAM role
3335
aws iam delete-role --role-name ${rolename}
36+
37+
# Delete OIDC provider
3438
oidc_id=$(aws eks describe-cluster --name ${TEST_CLUSTER_NAME} --query "cluster.identity.oidc.issuer" --output text | cut -d '/' -f 5)
3539
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
3642
security_group_id=$(aws eks describe-cluster --name ${TEST_CLUSTER_NAME} --query "cluster.resourcesVpcConfig.securityGroupIds[0]" --output text)
37-
echo "Cleanup remaining PVC on the EKS Cluster ${TEST_CLUSTER_NAME}"
3843

44+
# Cleanup remaining PVCs on the EKS Cluster
3945
echo "Cleanup remaining PVC on the EKS Cluster ${TEST_CLUSTER_NAME}"
4046
tools/cleanup.sh
47+
48+
# Get node group
4149
NODE_GROUP=$(eksctl get nodegroup --cluster=${TEST_CLUSTER_NAME} | sed -n 4p | awk '{ print $2 }')
42-
if [[ ! -z "${NODE_GROUP}" ]]; then
43-
eksctl delete nodegroup --cluster=${TEST_CLUSTER_NAME} --name=${NODE_GROUP}
44-
if [ $? -ne 0 ]; then
45-
echo "Unable to delete Nodegroup ${NODE_GROUP}. For Cluster - ${TEST_CLUSTER_NAME}"
46-
fi
47-
fi
48-
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+
4959
if [ $? -ne 0 ]; then
5060
echo "Unable to delete cluster - ${TEST_CLUSTER_NAME}"
5161
return 1
5262
fi
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
73+
done
74+
75+
# Delete security group
5376
aws ec2 delete-security-group --group-id ${security_group_id}
5477

5578
return 0
5679
}
5780

81+
5882
function createCluster() {
5983
# Deploy eksctl cluster if not deploy
6084
rc=$(which eksctl)

0 commit comments

Comments
 (0)