Skip to content

Instantly share code, notes, and snippets.

@mrballcb
Created November 4, 2021 18:18
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 mrballcb/4b5931c9a79139173f190dcf161bf0cf to your computer and use it in GitHub Desktop.
Save mrballcb/4b5931c9a79139173f190dcf161bf0cf to your computer and use it in GitHub Desktop.
Show pods in "transition" mode
#!/usr/bin/env bash
# Expected input can be one of the following types:
# CTX # Just the context - shows "transition mode" for this cluster and all kube masters/nodes
# CTX NS # The context, a space, and the namespace - shows all pods in that NS and only kube nodes
TYPE=${1-none}
CTX=${TYPE}
NS=${2-$TYPE}
# Extract instance types, AZ, and kops instance group name
SED_FILTER_LABELS="-e 's/beta.kubernetes.*instance-type=/type=/' -e 's#beta.kubernetes.io/##g' -e 's#kubernetes.io/##g' -e 's#,arch=\w*##' -e 's#,os=\w*##'"
shift
SLEEP=${SLEEP-1}
case "$CTX" in
context1|context2|context3|context4|context5|context6|context7|context8|context9)
NUMNODES=$(kubectl --context $CTX get nodes | grep -v NAME | wc -l)
if [ -z "$NODES" -a "$NUMNODES" -le 15 ]; then
NODES=1
fi
if [ "$CTX" = "$NS" ]; then
# Transition mode - Didn't request specific namespace, just show non healthy pods and all masters/nodes
watch -n $SLEEP "[ -n \"$NODES\" ] && kubectl --context $CTX get nodes --show-labels | \
sed ${SED_FILTER_LABELS} | \
sort -k 6; \
echo;\
kubectl --context $CTX get pods --all-namespaces $@ -o wide | \
grep -vE 'Completed|Error' | \
grep -vE '(1/1|2/2|3/3|4/4|5/5|6/6)\s*Running'"
else
shift
# Only show pods in the requested namespace, and only nodes (not masters)
watch -n $SLEEP "[ -n \"$NODES\" ] && kubectl --context $CTX get nodes --show-labels | \
grep -v master | \
sed ${SED_FILTER_LABELS} | \
sort -t , -k 3; \
echo; \
kubectl --context $CTX -n $NS get pods $@ -o wide"
fi
;;
*)
echo "Usage: $(basename $0) target [namespace]"
echo "Valid targets:"
echo " Specify the 'context', shows nodes and pods with errors:"
echo "    context1 context2 context3 context4 context5 context6 context7 context8 context9"
echo " Specify 'context namespace', shows nodes and pods in that NS"
echo " Best to use k9s for that, unless also want node output"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment