Last active
October 6, 2022 08:38
-
-
Save ptrstpp950/a4babaa6b28470f7c7f0cddb48d4eadd to your computer and use it in GitHub Desktop.
This file contains 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
#!/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)" |
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
Domyślny bash w macos:
Bash z brew nie ma tego problemu i dlatego wolę używać env w shebangu.