Implementation:ArroyoSystems Arroyo K8s Quantities
Overview
K8s Quantities provides parsing and conversion utilities for Kubernetes resource quantity strings (CPU and memory). It extends the k8s_openapi::Quantity type with methods to parse millicpu values, binary/decimal memory units, and produce canonical string representations.
Description
This module defines:
MemoryUnit: An enum representing all supported Kubernetes memory units (m, Ki, Mi, Gi, Ti, Pi, k, M, G, T, P, and base units), each with a multiplier value for conversion to a common base (millibytes).ParsedQuantity: An enum withCpu(i64)andMemory(i128)variants, supporting multiplication byi64and conversion to canonical Kubernetes quantity strings viato_canonical().QuantityParsertrait: ExtendsQuantitywithparse_cpu()(returns millicpu as i64) andparse_memory()(returns millibytes as i128) methods.
The parsing uses a regex to extract the numeric value and unit suffix, then applies the appropriate multiplier. CPU values without a unit suffix are interpreted as whole cores (multiplied by 1000 for millicpu). Memory values are parsed with both binary (Ki, Mi, Gi, Ti, Pi) and decimal (k, M, G, T, P) suffixes.
Usage
This module is used by the KubernetesScheduler to parse and scale CPU and memory resource requests when creating worker pods.
Code Reference
Source Location
crates/arroyo-controller/src/schedulers/kubernetes/quantities.rs
Signature
pub enum ParsedQuantity {
Cpu(i64),
Memory(i128),
}
impl ParsedQuantity {
pub fn value(&self) -> i128
pub fn to_canonical(&self) -> String
}
impl Mul<i64> for ParsedQuantity {
type Output = ParsedQuantity;
fn mul(self, rhs: i64) -> Self::Output
}
pub trait QuantityParser {
fn parse_cpu(&self) -> Result<ParsedQuantity>;
fn parse_memory(&self) -> Result<ParsedQuantity>;
}
impl QuantityParser for Quantity {
fn parse_cpu(&self) -> Result<ParsedQuantity>
fn parse_memory(&self) -> Result<ParsedQuantity>
}
Import
use crate::schedulers::kubernetes::quantities::QuantityParser;
I/O Contract
Inputs
| Name | Type | Description |
|---|---|---|
| quantity string | Quantity (wrapping String) |
Kubernetes resource quantity like "500m", "2", "1Gi", "100Mi" |
Outputs
| Name | Type | Description |
|---|---|---|
| ParsedQuantity::Cpu | i64 |
CPU value in millicpu (e.g., "500m" -> 500, "2" -> 2000) |
| ParsedQuantity::Memory | i128 |
Memory value in millibytes (e.g., "1Ki" -> 1024000, "500Mi" -> 524288000000) |
| canonical string | String |
Canonical Kubernetes quantity representation (e.g., "2000m" -> "2", "1024000m" -> "1Ki") |
Usage Examples
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
// Parse CPU quantities
let cpu = Quantity("500m".to_string()).parse_cpu().unwrap();
assert_eq!(cpu.value(), 500);
let cpu = Quantity("2".to_string()).parse_cpu().unwrap();
assert_eq!(cpu.value(), 2000);
// Parse memory quantities
let mem = Quantity("1Gi".to_string()).parse_memory().unwrap();
// Scale by number of slots
let scaled = cpu * 4;
assert_eq!(scaled.to_canonical(), "2");
Related Pages
- Implementation:ArroyoSystems_Arroyo_Kubernetes_Scheduler - Uses QuantityParser to scale pod resources
- Implementation:ArroyoSystems_Arroyo_Scheduler_Trait - The Scheduler trait that Kubernetes scheduler implements