Please describe your feature request.
I want to use yq to work with Kubernetes resources, where quantities are frequently expressed with a suffix. The formats are:
- Integers, possibly stringified (e.g.
"3" or 3)
"1m" for "milli", equal to 0.001
"1k" for "k", equal to 1000, as well as M, G, T, P, and E
"1Ki" for 2^10, equal to 1024, similarly Mi, Gi, etc
Note k and Ki differ in capitalization, otherwise it's just an additional i suffix.
Describe the solution you'd like
I would like a function to convert to and from the suffixed units. Integer milli quantities are probably the best solution, as it preserves the exact value.
- resources:
cpu: 200m
memory: 300Mi
- resources:
cpu: "3"
memory: 512M
And we run a command:
yq '{"cpu": (.[].resources.cpu | tomillis) as $item ireduce (0; . + $item) | frommillis, "memory": (.[].resources.memory | tomillis) as $item ireduce (0; . + $item) | frommillis}'
it could output
cpu: 3200m
memory: 807200Ki
Note memory only simplifies to the nearest Ki, so the output unit is different from the input.
Describe alternatives you've considered
We could also convert to/from float, but I'd be worried about loss of precision.
Additional context
Happy to work on implementing this.
Please describe your feature request.
I want to use
yqto work with Kubernetes resources, where quantities are frequently expressed with a suffix. The formats are:"3"or3)"1m"for "milli", equal to0.001"1k"for "k", equal to1000, as well asM,G,T,P, andE"1Ki"for 2^10, equal to1024, similarlyMi,Gi, etcNote
kandKidiffer in capitalization, otherwise it's just an additionalisuffix.Describe the solution you'd like
I would like a function to convert to and from the suffixed units. Integer milli quantities are probably the best solution, as it preserves the exact value.
And we run a command:
yq '{"cpu": (.[].resources.cpu | tomillis) as $item ireduce (0; . + $item) | frommillis, "memory": (.[].resources.memory | tomillis) as $item ireduce (0; . + $item) | frommillis}'it could output
Note
memoryonly simplifies to the nearestKi, so the output unit is different from the input.Describe alternatives you've considered
We could also convert to/from float, but I'd be worried about loss of precision.
Additional context
Happy to work on implementing this.