Skip to content

Instantly share code, notes, and snippets.

@kongo2002
Last active November 7, 2021 22:13
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 kongo2002/1c6a6e2f7f1766cba16c9113ae8d7e38 to your computer and use it in GitHub Desktop.
Save kongo2002/1c6a6e2f7f1766cba16c9113ae8d7e38 to your computer and use it in GitHub Desktop.
zsh keybinding for k8s resources
# k8s.zsh
#
# Registers key-binding for CTRL-K to insert a kubernetes
# pod, deployment or service name into the command.
#
# The name selection is piped through FZF.
# check of FZF exists
if [[ ! -x $(which fzf) ]]; then
return 0
fi
# check if kubernetes CLI exists
if [[ ! -x $(which kubectl) ]]; then
return 0
fi
# CTRL-K - select kubernetes pod name
__kpodsel() {
local entity
case "$LBUFFER" in
*pod*)
entity="pod"
;;
*service*)
entity="service"
;;
*deployment*)
entity="deployment"
;;
*)
entity="pod"
;;
esac
local cmd="kubectl get ${entity} -o custom-columns=NAME:.metadata.name --no-headers"
setopt localoptions pipefail no_aliases 2> /dev/null
local item
eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS" fzf | while read item; do
echo -n "${(q)item} "
done
local ret=$?
echo
return $ret
}
fzf-kpod-widget() {
LBUFFER="${LBUFFER}$(__kpodsel)"
local ret=$?
zle reset-prompt
return $ret
}
zle -N fzf-kpod-widget
bindkey '^K' fzf-kpod-widget
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment