Created
September 29, 2023 22:56
-
-
Save tmacam/199022494adf698c4d8db3a8fe405699 to your computer and use it in GitHub Desktop.
Another approach to change kubectl context
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 | |
set -e | |
function listContexts() { | |
kubectl config get-contexts | sed -e 's/^ /off/' -e 's/^\*/on/' -e 1d | awk '{printf("%i\n%s\n", (NR-1),$2)}' | |
} | |
# https://stackoverflow.com/questions/11426529/reading-output-of-a-command-into-an-array-in-bash | |
IFS=$'\n' read -r -d '' -a contexts < <( listContexts && printf '\0' ) | |
selected=$( | |
dialog --menu "Choose cluster" 0 0 0 "${contexts[@]}" 2>&1 > /dev/tty | |
) | |
if [ "x${selected}" != "x" ]; then | |
selectedContextPos=$[ 2 * selected + 1 ] | |
selectedContext=${contexts[$selectedContextPos]} | |
echo Changing to context ${selected} : ${selectedContext} | |
kubectl config use-context ${selectedContext} | |
else | |
echo "Nothing selected, not changing context" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment