Skip to content

Instantly share code, notes, and snippets.

@neofob
Last active January 28, 2024 19:33
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 neofob/bca04997e3ca40ac97547859eaccb7e8 to your computer and use it in GitHub Desktop.
Save neofob/bca04997e3ca40ac97547859eaccb7e8 to your computer and use it in GitHub Desktop.
Kubernetes

Code

kubectl get pods -o=jsonpath='{.items..resources.limits}' -A

output

{"cpu":"200m","memory":"1Gi"} {"cpu":"200m","memory":"1Gi"} {"cpu":"200m","memory":"512Mi"} {"cpu":"500m","memory":"250Mi"} {"memory":"170Mi"} {"memory":"170Mi"} {"cpu":"2","memory":"2Gi"} {"cpu":"2","memory":"2Gi"} {"cpu":"2","memory":"2Gi"} {"cpu":"1","memory":"1Gi"} {"cpu":"1","memory":"1Gi"} {"cpu":"2","memory":"2Gi"} {"cpu":"100m","memory":"128Mi"} {"cpu":"100m","memory":"128Mi"} {"cpu":"500m","memory":"600Mi"} {"cpu":"1","memory":"1Gi"} {"cpu":"100m","memory":"25Mi"} {"cpu":"100m","memory":"25Mi"}

Use plugin

kubectl resource-capacity --sort cpu.limit --util --pods

Bash script

res=$(kubectl get pods -o=jsonpath='{.items[*]..resources.limits.cpu}' -A)
let tot=0
for i in $res
do
   if [[ $i =~ "m" ]]; then
      i=$(echo $i | sed 's/[^0-9]*//g')
      tot=$(( tot + i ))
   else
      tot=$(( tot + i*1000 ))
   fi
done
echo $tot

Sum the resources

kubectl get pod --all-namespaces --sort-by='.metadata.name' -o json | \
jq -r '[.items[] | {pod_name: .metadata.name, containers: .spec.containers[] | \
[ {container_name: .name, memory_requested: .resources.requests.memory, cpu_requested: .resources.requests.cpu} ] }]' | \
jq  'sort_by(.containers[0].cpu_requested)'

References:

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