Last active
March 11, 2025 12:35
-
-
Save jenrik/5a47d08b52e474711ea601951b7f9052 to your computer and use it in GitHub Desktop.
Normalize Kubernetes units to plain numbers
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
def k8sunits: capture("(?<num>\\d+)(?<unit>.*)") | (.num | tonumber) * {"": 1, "Ki": 1024, "K": 1000, "Mi": pow(1024; 2), "M": pow(1000; 2), "Gi": pow(1024; 3), "G": pow(1000; 3), "Ti": pow(1024; 4), "T": pow(1000; 4), "Ei": pow(1024; 5), "E": pow(1000; 5), "Pi": pow(1024; 6), "p": pow(1000; 6), "m": 0.001}[.unit]; |
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/sh | |
set -e | |
# Convert Kubernetes storage and CPU units to plain decimals | |
grep -E "^[0-9]+(\.[0-9]+)?(e[0-9]+)?((E|P|T|G|M|K)i?|m)?$" \ | |
| sed \ | |
-e 's/Ki$/ * \(1024 \^ 1\)/' \ | |
-e 's/K$/ * \(1000 \^ 1\)/' \ | |
-e 's/Mi$/ * \(1024 \^ 2\)/' \ | |
-e 's/M$/ * \(1000 \^ 2\)/' \ | |
-e 's/Gi$/ * \(1024 \^ 3\)/' \ | |
-e 's/G$/ * \(1000 \^ 3\)/' \ | |
-e 's/Ti$/ * \(1024 \^ 4\)/' \ | |
-e 's/T$/ * \(1000 \^ 4\)/' \ | |
-e 's/Ei$/ * \(1024 \^ 5\)/' \ | |
-e 's/E$/ * \(1000 \^ 5\)/' \ | |
-e 's/Pi$/ * \(1024 \^ 6\)/' \ | |
-e 's/P$/ * \(1000 \^ 6\)/' \ | |
-e 's/m$/ \/ 1000/' \ | |
| bc --scale=3 -z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment