Created
October 15, 2020 05:05
-
-
Save tongphe/45a30c8ffcc55880031a8589ff946126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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