- Download kexec.sh to directory you want to place it
- Set alias
kexec
to pointingkexec.sh
- Update aliases. (like
source ~/.zshrc
) $ kexec
or$ kexec grep_keyword
Last active
April 1, 2019 15:40
-
-
Save jeyraof/8216dc8763988ecaa7b97b88e189d470 to your computer and use it in GitHub Desktop.
kube shortcut
This file contains 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
# set path to kexec.sh | |
alias kexec='~/shortcuts/kexec.sh' |
This file contains 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 | |
# interactive kubectl exec | |
# https://unix.stackexchange.com/a/415155 | |
function select_option { | |
ESC=$( printf "\033") | |
cursor_blink_on() { printf "$ESC[?25h"; } | |
cursor_blink_off() { printf "$ESC[?25l"; } | |
cursor_to() { printf "$ESC[$1;${2:-1}H"; } | |
print_option() { printf " $1 "; } | |
print_selected() { printf " $ESC[7m $1 $ESC[27m"; } | |
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; } | |
key_input() { read -s -n3 key 2>/dev/null >&2 | |
if [[ $key = $ESC[A ]]; then echo up; fi | |
if [[ $key = $ESC[B ]]; then echo down; fi | |
if [[ $key = "" ]]; then echo enter; fi; } | |
for opt; do printf "\n"; done | |
local lastrow=`get_cursor_row` | |
local startrow=$(($lastrow - $#)) | |
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2 | |
cursor_blink_off | |
local selected=0 | |
while true; do | |
local idx=0 | |
for opt; do | |
cursor_to $(($startrow + $idx)) | |
if [ $idx -eq $selected ]; then | |
print_selected "$opt" | |
else | |
print_option "$opt" | |
fi | |
((idx++)) | |
done | |
case `key_input` in | |
enter) break;; | |
up) ((selected--)); | |
if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi;; | |
down) ((selected++)); | |
if [ $selected -ge $# ]; then selected=0; fi;; | |
esac | |
done | |
cursor_to $lastrow | |
printf "\n" | |
cursor_blink_on | |
return $selected | |
} | |
if ! [ -x "$(command -v kube)" ]; then | |
echo "No command for operating kube." | |
exit 1 | |
fi | |
cmd="kube get pod --no-headers=true -o custom-columns=\":metadata.name\" --field-selector=status.phase=Running" | |
if [ "$#" -eq "0" ]; then | |
pods=$(eval $cmd) | |
else | |
pods=$(eval $cmd | grep $1) | |
fi | |
pods=($pods) | |
echo | |
echo "Select pod to connect via /bin/bash:" | |
echo | |
select_option "${pods[@]}" | |
choice=$? | |
echo "Connect to ${pods[$choice]}..." | |
kube exec -it ${pods[$choice]} /bin/bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment