Skip to content

Instantly share code, notes, and snippets.

@tmacam
Created September 29, 2023 22:56
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 tmacam/199022494adf698c4d8db3a8fe405699 to your computer and use it in GitHub Desktop.
Save tmacam/199022494adf698c4d8db3a8fe405699 to your computer and use it in GitHub Desktop.
Another approach to change kubectl context
#!/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