Skip to content

Support Kubernetes-style quantity suffixes #2698

@kwohlfahrt

Description

@kwohlfahrt

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions