Skip to content

Instantly share code, notes, and snippets.

@ptrstpp950
Last active October 6, 2022 08:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptrstpp950/a4babaa6b28470f7c7f0cddb48d4eadd to your computer and use it in GitHub Desktop.
Save ptrstpp950/a4babaa6b28470f7c7f0cddb48d4eadd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
ceiling_divide() {
echo $((($1+$2-1)/$2))
}
die () {
echo >&2 "$@"
exit 1
}
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
case $1 in
'requests')
;;
'limits')
;;
*) die "Only works for requests or limits"
esac
res=$(kubectl get pods -o=jsonpath='{.items[*]..resources.$1.memory}' -A)
let total=0
for i in $res
do
if [[ $i == ?(-)+([0-9]) ]]
then
multiplier=0
else
suffix=${i: -2}
case $suffix in
'Ki')
multiplier=1
;;
'Mi')
multiplier=2
;;
'Gi')
multiplier=3
;;
*)
die "bad suffix $suffix"
;;
esac
fi
value=$(echo $i | sed 's/[^0-9]*//g')
multiplier=$((1024 ** multiplier))
value=$((value*multiplier))
# echo "value=$value multiplier=$multiplier"
total=$((total+value))
done
gibibyte=$((1024 ** 3))
echo $(ceiling_divide $total $gibibyte) "GiB RAM (exactly $total bytes)"
res=$(kubectl get pods -o=jsonpath='{.items[*]..resources.$1.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 $(ceiling_divide $tot 1000) "cores (exactly $tot milicores)"
@dracorp
Copy link

dracorp commented Oct 28, 2021

Domyślny bash w macos:

 $ ./kubectl_total_resources.sh limits
./kubectl_total_resources.sh: line 28: syntax error in conditional expression: unexpected token `('
./kubectl_total_resources.sh: line 28: syntax error near `?(-'
./kubectl_total_resources.sh: line 28: `   if [[ $i == ?(-)+([0-9]) ]]'

Bash z brew nie ma tego problemu i dlatego wolę używać env w shebangu.

#!/usr/bin/env bash

@ptrstpp950
Copy link
Author

Widać, że ze mnie żaden apple fanboy 😅Słuszna uwaga, dzięki!

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