Skip to content

Instantly share code, notes, and snippets.

@tongphe
Created October 15, 2020 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tongphe/45a30c8ffcc55880031a8589ff946126 to your computer and use it in GitHub Desktop.
Save tongphe/45a30c8ffcc55880031a8589ff946126 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
IFS=$'\n' clusters=($(kubectl config get-contexts --no-headers | awk '{print $2}'))
active_cluster=$(kubectl config current-context)
for i in ${!clusters[@]};
do
:
if [ "${clusters[$i]}" == $active_cluster ]; then
echo -e "\033[31m[*] \033[36m"${clusters[$i]}
else
echo -e "\033[31m[$((i+1))] \033[36m"${clusters[$i]}
fi
done
read -p "Select cluster: " cluster
if [[ -z "$cluster" ]]; then
echo -e "\033[31mNothing selected"
exit 1
fi
if (( cluster - 1 < ${#clusters[@]} )) &&
(( cluster > 0 )) &&
[[ $cluster = *[[:digit:]]* ]]; then
if [ "${clusters[$cluster - 1]}" == $active_cluster ]; then
echo -e "\033[34mCluster ${clusters[$cluster - 1]} is already active"
exit 0
fi
echo -e "\033[34mSelected cluster: ${clusters[$cluster - 1]}"
kubectl config use-context ${clusters[$cluster - 1]}
else
echo -e "\033[31mOops, $cluster is not there"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment