Skip to content

Instantly share code, notes, and snippets.

@epcim
Last active April 29, 2019 17:19
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 epcim/6c34f0c7f9d2c24a252b7f02faed7032 to your computer and use it in GitHub Desktop.
Save epcim/6c34f0c7f9d2c24a252b7f02faed7032 to your computer and use it in GitHub Desktop.
kubectl kubernetes alias bash cli
# you can't live without 'k' and 'p'
# kubectl alias
alias k='kubectl'
# kubectl get pods alias
## store Kubernetes pod name & namespace to env variable
## expect servername has number behind last -
[[ -z $ZSH_NAME ]] || setopt SH_WORD_SPLIT
function get-pods() {
# cleanup previously set variables
unset $(set | egrep "[a-z0-9]+[0-9]=" | awk -F= '{print $1}') &> /dev/null || true
# print and eval variables based on the pod name ($2, without dynamic part), with server index as suffix ($8)
eval $(kubectl get po --all-namespaces -L app -o wide | tee /dev/stderr| grep -v 'NAMESPACE' | awk ' {pod_name=$2;gsub(/-[a-z0-9]{9,10}-[a-z0-9]{5}$|-[a-z0-9]{5}$/,"",$2);gsub(/-/,"_",$2);gsub(/.*-/,"",$8);printf "%s%s=\x27-n %s %s\x27\n", $2,$8, $1,pod_name}'|grep -v "^=" | egrep -v "^[0-9]*=" );
}
# to be able grep output
alias p="get-pods 2>&1"
p | grep kube-system
k logs $fluentbit1
k describe po $fluentd2
## ASSUMPTIONS / KNOWN ISSUES
# - eval fails to eval exp. of pods with just number in name, rason for "| egrep -v "^[0-9]*=""
# - node name has index numbner behind last "-"
# - app names are different across namespaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment