Skip to content

Instantly share code, notes, and snippets.

@nyrahul
Last active May 16, 2025 16:14
Show Gist options
  • Save nyrahul/7ff0b9742bb44e105480491eb73cae94 to your computer and use it in GitHub Desktop.
Save nyrahul/7ff0b9742bb44e105480491eb73cae94 to your computer and use it in GitHub Desktop.
Sum up CPU/Memory Limits/Requests for a given k8s namespace
js=$(kubectl get pods -n agents -o json) && \
cpur=$(echo $js | jq '.items[].spec.containers[]?.resources.requests.cpu' | sed -r 's/([0-9]*)m/\1/' | sed -e 's/"//g' -e 's/,//g' | paste -sd+ - | bc) && \
cpul=$(echo $js | jq '.items[].spec.containers[]?.resources.limits.cpu' | sed -r 's/([0-9]*)m/\1/' | sed -e 's/"//g' -e 's/,//g' | paste -sd+ - | bc) && \
memr=$(echo $js | jq '.items[].spec.containers[]?.resources.requests.memory' | sed -r 's/([0-9]*)Mi/\1/' | sed -e 's/"//g' -e 's/,//g' | paste -sd+ - | bc) && \
meml=$(echo $js | jq '.items[].spec.containers[]?.resources.limits.memory' | sed -r 's/([0-9]*)Mi/\1/' | sed -e 's/"//g' -e 's/,//g' | paste -sd+ - | bc) && \
echo -en "CPU Requests=${cpur}m, CPU Limits=${cpul}m\nMem Requests=${memr}Mi, Mem Limits=${meml}Mi\n"
@nyrahul
Copy link
Author

nyrahul commented May 16, 2025

Sample Output:

CPU Requests=280m, CPU Limits=680m
Mem Requests=300Mi, Mem Limits=1000Mi

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