54 lines
1.5 KiB
Bash
Executable File
54 lines
1.5 KiB
Bash
Executable File
set -e
|
|
|
|
declare -A account_clusters
|
|
declare -A account_profiles
|
|
|
|
for ctx in $(kubectl config get-contexts -o name); do
|
|
if [[ "${ctx:0:7}" != "arn:aws" ]]; then
|
|
continue
|
|
fi
|
|
|
|
account_id="${ctx#*:*:*:*:}"
|
|
account_id="${account_id%%:*}"
|
|
cluster_name="${ctx#*cluster/}"
|
|
|
|
if [[ "${account_id}" == "" ]] || [[ "${cluster_name}" == "" ]]; then
|
|
continue
|
|
fi
|
|
|
|
if [[ ! -v account_profiles[$account_id] ]]; then
|
|
aws_profile=$(grep "sso_account_id = $account_id" ~/.aws/config -B 5 | grep "\[profile" | tail -n 1 | tr -d '[]')
|
|
aws_profile="${aws_profile#profile }"
|
|
|
|
if [[ "${aws_profile}" == "" ]]; then
|
|
continue
|
|
fi
|
|
|
|
account_profiles[$account_id]=$aws_profile
|
|
account_clusters[$account_id]=""
|
|
fi
|
|
|
|
account_clusters[$account_id]+="$cluster_name "
|
|
done
|
|
|
|
for acc in ${!account_profiles[@]}; do
|
|
profile=${account_profiles[$acc]}
|
|
|
|
declare -A current_clusters
|
|
|
|
for remote_cluster in $(aws --profile $profile eks list-clusters --query "clusters" --output text); do
|
|
current_clusters[$remote_cluster]=""
|
|
done
|
|
|
|
for cluster in ${account_clusters[$acc]}; do
|
|
if [[ -v current_clusters[$cluster] ]]; then
|
|
# check if credentials are current
|
|
echo "$cluster cluster exists"
|
|
else
|
|
kubectl config delete-context "$(kubectl config get-contexts -o name | grep "$acc:cluster/$cluster" | head -n 1)"
|
|
fi
|
|
done
|
|
|
|
unset -v current_clusters
|
|
done
|