Created
May 27, 2022 10:03
-
-
Save laureen71/9df580b076064f303152bf9704b2a0e6 to your computer and use it in GitHub Desktop.
kubectl context switcher
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
#!/bin/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 "\e[33m[*] \e[36m"${clusters[$i]} | |
else | |
echo -e "\e[33m[$((i+1))] \e[36m"${clusters[$i]} | |
fi | |
done | |
read -p "Select cluster: " cluster | |
if [[ -z "$cluster" ]]; then | |
echo -e "\e[91mNothing selected\e[39m" | |
exit 1 | |
fi | |
if (( cluster - 1 < ${#clusters[@]} )) && | |
(( cluster > 0 )) && | |
[[ $cluster = *[[:digit:]]* ]]; then | |
if [ "${clusters[$cluster - 1]}" == $active_cluster ]; then | |
echo -e "\e[93mCluster ${clusters[$cluster - 1]} is already active\e[39m" | |
exit 0 | |
fi | |
echo -e "\e[32mSelected cluster: ${clusters[$cluster - 1]}\e[39m" | |
kubectl config use-context ${clusters[$cluster - 1]} | |
else | |
echo -e "\e[91mOops, $cluster is not there\e[39m" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment