From c7011d3d42819b33f21868f4f9fe32b0c52f1099 Mon Sep 17 00:00:00 2001 From: Pulkit Jain Date: Fri, 25 Oct 2024 08:56:45 +0530 Subject: [PATCH] Fix context issue during cleanup of kind clusters Signed-off-by: Pulkit Jain --- ci/kind/kind-setup.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ci/kind/kind-setup.sh b/ci/kind/kind-setup.sh index e57e79616e4..3c62755e11f 100755 --- a/ci/kind/kind-setup.sh +++ b/ci/kind/kind-setup.sh @@ -517,18 +517,20 @@ function destroy_external_servers { function clean_kind { echo "=== Cleaning up stale kind clusters ===" - read -a all_kind_clusters <<< $(kind get clusters) - for kind_cluster_name in "${all_kind_clusters[@]}"; do - creationTimestamp=$(kubectl get nodes --context kind-$kind_cluster_name -o json -l node-role.kubernetes.io/control-plane | \ + for context in $(kubectl config get-contexts -o name | grep 'kind-'); do + cluster_name=$(echo $context | sed 's/^kind-//') + if docker ps --format '{{.Names}}' | grep -q "$cluster_name"; then + creationTimestamp=$(kubectl get nodes --context $context -o json -l node-role.kubernetes.io/control-plane | \ jq -r '.items[0].metadata.creationTimestamp') creation=$(printUnixTimestamp "$creationTimestamp") now=$(date -u '+%s') diff=$((now-creation)) timeout=$(($UNTIL_TIME_IN_MINS*60)) if [[ $diff -gt $timeout ]]; then - echo "=== kind ${kind_cluster_name} present from more than $UNTIL_TIME_IN_MINS minutes ===" - kind delete cluster --name $kind_cluster_name + echo "=== Cluster ${cluster_name} is present for more than $UNTIL_TIME_IN_MINS minutes ===" + kind delete cluster --name $cluster_name fi + fi done }