Skip to content

Instantly share code, notes, and snippets.

@epcim epcim/get-pods.sh
Last active Apr 29, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.