Created
May 12, 2024 12:20
-
-
Save caruccio/756430d7a2de75cbd026d4dd5edd13c6 to your computer and use it in GitHub Desktop.
kubectl top pods in a node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function usage() | |
{ | |
echo Usage: $0 '[--cpu/-c|--memory/-m]' nodes... | |
echo Sort by memory is the default | |
exit ${1:-0} | |
} | |
node_names=() | |
sort_field=memory | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
-c|--cpu) | |
sort_field=cpu | |
;; | |
-m|--memory) | |
sort_field=memory | |
;; | |
-h|--help) | |
usage | |
;; | |
*) | |
node_names+=($1) | |
esac | |
shift | |
done | |
if [ "${#node_names[*]}" -lt 1 ]; then | |
usage 1 | |
fi | |
_IFS="$IFS" IFS='|' | |
kubectl top node --sort-by=$sort_field | grep -E "^(NAME|${node_names[*]})" | |
IFS="$_IFS" | |
for node_name in ${node_names[@]}; do | |
echo | |
t="Topping node $node_name" | |
echo "$t" | |
printf "%-${#t}s\n" = | tr ' ' = | |
tpl='{{range $index, $pod := .items}}{{ if $index }}|{{else}}^({{end}}{{$pod.metadata.namespace}}\s+{{$pod.metadata.name}}\s{{end}}|NAME|[^a-zA-Z]).*' | |
regex=$(kubectl get pod --field-selector=spec.nodeName=$node_name -A -o template --template="$tpl") || exit 1 | |
kubectl top pod --all-namespaces --sort-by=$sort_field $@ | grep --color=no -E "$regex" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment