kubernetes.utils.quantity module

kubernetes.utils.quantity.format_quantity(quantity_value, suffix, quantize=None) str

Takes a decimal and produces a string value in kubernetes’ canonical quantity form, like “200Mi”.Users can specify an additional decimal number to quantize the output.

Example - Relatively increase pod memory limits:

# retrieve my_pod current_memory: Decimal = parse_quantity(my_pod.spec.containers[0].resources.limits.memory) desired_memory = current_memory * 1.2 desired_memory_str = format_quantity(desired_memory, suffix=”Gi”, quantize=Decimal(1)) # patch pod with desired_memory_str

‘quantize=Decimal(1)’ ensures that the result does not contain any fractional digits.

Supported SI suffixes: base1024: Ki | Mi | Gi | Ti | Pi | Ei base1000: n | u | m | “” | k | M | G | T | P | E

See https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go

Input: quantity: Decimal. Quantity as a number which is supposed to converted to a string

with SI suffix.

suffix: string. The desired suffix/unit-of-measure of the output string quantize: Decimal. Can be used to round/quantize the value before the string

is returned. Defaults to None.

Returns: string. Canonical Kubernetes quantity string containing the SI suffix.

Raises: ValueError if the SI suffix is not supported.

kubernetes.utils.quantity.parse_quantity(quantity)

Parse kubernetes canonical form quantity like 200Mi to a decimal number. Supported SI suffixes: base1024: Ki | Mi | Gi | Ti | Pi | Ei base1000: n | u | m | “” | k | M | G | T | P | E

See https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go

Input: quantity: string. kubernetes canonical form quantity

Returns: Decimal

Raises: ValueError on invalid or unknown input