gtsystem / lightkube

Modern lightweight kubernetes module for python
https://lightkube.readthedocs.io
MIT License
96 stars 11 forks source link

Add means to compare quantities #36

Closed sed-i closed 1 year ago

sed-i commented 2 years ago

Background

K8s converts user input quantities to "canonical form":

Before serializing, Quantity will be put in "canonical form". This means that Exponent/suffix will be adjusted up or down (with a corresponding increase or decrease in Mantissa) such that: a. No precision is lost b. No fractional digits will be emitted c. The exponent (or suffix) is as large as possible. The sign will be omitted unless the number is negative.

Examples: 1.5 will be serialized as "1500m" 1.5Gi will be serialized as "1536Mi"

Additional examples:

User input K8s representation
{"memory": "0.9Gi"} {"memory": "966367641600m"}
{"cpu": "0.30000000000000004"} {"cpu": "301m"}

Use case

When patching a statefulset's resource limits, it would be handy to be able to compare a quantity's setpoint

statefulset.spec.template.spec.containers[i].resources

to the active podspec

pod.spec.containers[i].resources

Currently, two equivalent ResourceRequirements instances could differ under == comparison and require manual interconversion.

Additional thoughts

gtsystem commented 2 years ago

Yes, a function similar to parse_quantity could be a good addition. Would you like to contribute a PR?