Skip to content

Instantly share code, notes, and snippets.

@idcrook
Last active August 17, 2018 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save idcrook/52d63f33d038090bd2c3465e19f05223 to your computer and use it in GitHub Desktop.
Save idcrook/52d63f33d038090bd2c3465e19f05223 to your computer and use it in GitHub Desktop.
Shell command line expansion from kubernetes aliases; even complete in system namespace if bash alias uses that
# source kubectl bash completions
if hash kubectl 2>/dev/null; then
source <(kubectl completion bash)
fi
# the magic via https://github.com/cykerway/complete-alias
# seems to require BASH 4; install bash from Homebrew and make its BASH 4 the default user shell in macOS
# mkdir ~/.bash_completion.d
# curl -o ~/.bash_completion.d/bash_complete-alias.sh https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
if [ -f ~/.bash_completion.d/bash_complete-alias.sh ] ; then
. ~/.bash_completion.d/bash_complete-alias.sh
fi
# in ~/.bash_aliases (that gets sourced by ~/.bashrc)
# kubernetes
alias kbk="kubectl"
alias kbn="kubectl --namespace=kube-system"
# _complete_alias requires https://github.com/cykerway/complete-alias to be loaded
complete -F _complete_alias kbk
complete -F _complete_alias kbn
@idcrook
Copy link
Author

idcrook commented Jun 13, 2018

To see this in action, you can use the kubectl completions on an alias that specifies kube-system namespace.

kbn logs <TAB>
# completions shows pods in '--namespace=kube-system' as "kbn" alias has that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment