Resource Quantity Notes

1 min read

Kubernetes CPU and memory arithmetic using standard SI (m, K, M, G) and binary (Ki, Mi, Gi) suffixes.

Reference

NoteSignatureReturns
parseQuantitystringfloat64 — error on invalid
formatQuantityfloat64string — error on invalid
sumQuantitya, b stringstring — error on invalid

parseQuantity returns CPU in cores (not millicores) — "500m"0.5, "2"2.0.
parseQuantity returns memory in bytes — "1Gi"1073741824.0.

formatQuantity expresses sub-unit fractions as milli-units: 0.1"100m", 0.5"500m".

Examples

# Give each tenant 1/4 of the declared CPU limit
resources:
  requests:
    cpu: "{{ formatQuantity (div (parseQuantity .spec.cpuLimit) 4) }}"

# Total memory across two pools
- path: totalMemory
  value: "{{ sumQuantity .spec.poolA.memory .spec.poolB.memory }}"

# Cap memory request at node size × headroom
memory: "{{ formatQuantity (min (parseQuantity .spec.memory) (mul (parseQuantity .spec.nodeMemory) 0.8)) }}"

Compose with math notes for arithmetic between parsed quantities.