Watch
0
0
Fork
You've already forked hyperhive
0

k8s: a pod's requests are explicit, so its limits cap and reserve nothing

This commit is contained in:
atlas 2026-11-21 23:42:12 +01:00 • committed by maia
commit eda01b8b1d
3 changed files with 114 additions and 36 deletions

View file

@ -165,11 +165,11 @@ let
else
lib.toInt (lib.head m);
# A unit's `MemoryMax` as a container's memory limit. systemd's K, M, G and T
# are powers of 1024, as Kubernetes' Ki, Mi, Gi and Ti are. `infinity` is no
# limit.
memoryLimitOf =
name: value:
# A unit's memory setting `what` as a Kubernetes quantity. systemd's K, M, G
# and T are powers of 1024, as Kubernetes' Ki, Mi, Gi and Ti are. `infinity`
# is `null`, which a limit reads as no limit and a request refuses.
memoryOf =
what: name: value:
let
v = toString value;
m = builtins.match "([0-9]+)([KMGT]?)" v;
@ -177,7 +177,7 @@ let
if v == "infinity" then
null
else if m == null then
fail name "MemoryMax is \"${v}\"; only a byte count, optionally with the suffix K, M, G or T, translates"
fail name "${what} is \"${v}\"; only a byte count, optionally with the suffix K, M, G or T, translates"
else
lib.head m
+ {
@ -189,6 +189,24 @@ let
}
.${lib.last m};
memoryLimitOf = memoryOf "MemoryMax";
# A unit's `MemoryMin`, else its `MemoryLow`, as a container's memory
# request: the memory systemd keeps for the unit. A unit that sets neither
# reserves none, which is `0`.
memoryRequestOf =
name: sc:
let
what = if sc ? MemoryMin then "MemoryMin" else "MemoryLow";
v = memoryOf what name sc.${what};
in
if !(sc ? MemoryMin || sc ? MemoryLow) then
"0"
else if v == null then
fail name "${what} is \"infinity\"; only a byte count, optionally with the suffix K, M, G or T, translates"
else
v;
# A unit's `CPUQuota`, a percent of one core, as a container's cpu limit in
# millicores.
cpuLimitOf =
@ -439,18 +457,26 @@ let
}
];
# The unit's memory and CPU ceilings are the container's limits. The unit's
# other resource settings have no pod field and stay on the host:
# `TasksMax` has no counterpart (the node's kubelet sets the pids limit),
# and the pod keeps the one security shape every pod has, in place of the
# sandbox settings (`MemoryDenyWriteExecute`, `SystemCallFilter`,
# `PrivateUsers`, ...). No `requests` are set, so Kubernetes reserves the
# limits on the node.
# The unit's memory and CPU ceilings are the container's limits. They are
# caps, not reservations, so the requests are explicit: Kubernetes sets
# an unset request to the limit, and the pod would reserve the whole cap
# on the node. The memory request is what the unit asks systemd to keep
# (`MemoryMin`, else `MemoryLow`), and the cpu request is `0`, since
# `CPUQuota` reserves nothing. The pod is then Burstable. The unit's other
# resource settings have no pod field and stay on the host: `TasksMax`
# has no counterpart (the node's kubelet sets the pids limit), and the pod
# keeps the one security shape every pod has, in place of the sandbox
# settings (`MemoryDenyWriteExecute`, `SystemCallFilter`, `PrivateUsers`,
# ...).
limits =
lib.optionalAttrs (sc ? MemoryMax && memoryLimitOf name sc.MemoryMax != null) {
memory = memoryLimitOf name sc.MemoryMax;
}
// lib.optionalAttrs (sc ? CPUQuota) { cpu = cpuLimitOf name sc.CPUQuota; };
requests = {
memory = memoryRequestOf name sc;
cpu = "0";
};
container = {
inherit name volumeMounts;
@ -468,7 +494,12 @@ let
capabilities.drop = [ "ALL" ];
};
}
// lib.optionalAttrs (limits != { }) { resources = { inherit limits; }; }
// lib.optionalAttrs (limits != { } || sc ? MemoryMin || sc ? MemoryLow) {
resources = {
inherit requests;
}
// lib.optionalAttrs (limits != { }) { inherit limits; };
}
// lib.optionalAttrs (lib.tail execWords != [ ]) { args = lib.tail execWords; }
// lib.optionalAttrs (svc.healthEnv != null) {
ports = [

View file

@ -832,10 +832,11 @@ let
&& lib.hasInfix "swarm-stt.yaml" sttPrune.script;
}
{
# The unit's ceilings are the pod's limits, and the limits the pod has
# no field for are not rendered. The controller's pod, whose unit sets
# none, is the control that nothing is added to a pod without them.
name = "the speech-to-text pod carries its unit's memory and CPU ceilings as limits and no requests, and a pod whose unit sets none carries none";
# The unit's ceilings are the pod's limits and reserve nothing, and the
# limits the pod has no field for are not rendered. The controller's pod,
# whose unit sets none, is the control that nothing is added to a pod
# without them.
name = "the speech-to-text pod carries its unit's memory and CPU ceilings as limits and reserves nothing, and a pod whose unit sets none carries none";
ok =
let
unit = units.swarm-stt.serviceConfig;
@ -849,7 +850,11 @@ let
memory = "3Gi";
cpu = "4000m";
}
&& !((container "swarm-stt").resources ? requests)
&&
(container "swarm-stt").resources.requests == {
memory = "0";
cpu = "0";
}
&& !(lib.hasInfix "TasksMax" rendered)
&& !(lib.hasInfix "SystemCallFilter" rendered)
&& lib.all (name: !((container name) ? resources)) [
@ -898,6 +903,40 @@ let
CPUQuota = "4";
}));
}
{
# `MemoryMin` wins over `MemoryLow`, either is the memory the pod asks
# for, and the cpu request stays 0. A unit with neither is the control.
name = "a unit's MemoryMin, else its MemoryLow, is the pod's memory request, and one the renderer cannot read fails the evaluation";
ok =
let
withSc =
sc: lib.recursiveUpdate allLocal { services.hyperhive.deploy.stt.unit.serviceConfig = sc; };
resourcesOf =
sc: (lib.head (render (withSc sc))."swarm-stt/deployment".spec.template.spec.containers).resources;
in
(resourcesOf { }).requests.memory == "0"
&&
(resourcesOf { MemoryLow = "256M"; }).requests == {
memory = "256Mi";
cpu = "0";
}
&&
(resourcesOf {
MemoryMin = "1G";
MemoryLow = "256M";
}).requests.memory == "1Gi"
&&
(resourcesOf { MemoryLow = "256M"; }).limits == {
memory = "3Gi";
cpu = "4000m";
}
&& !(renders (withSc {
MemoryLow = "10%";
}))
&& !(renders (withSc {
MemoryMin = "infinity";
}));
}
{
name = "the speech-to-text pod has probes on its health listener, keeps its cache in a volume, mounts no Secret, and runs as every pod does";
ok =

View file

@ -270,11 +270,12 @@ pkgs.testers.runNixOSTest {
];
};
# The speech-to-text pod has no requests, so the node reserves its limits
# (3 GiB, four cores), and its image carries ffmpeg and whisper.
# The speech-to-text image carries ffmpeg and whisper. The pod's limits
# (3 GiB, four cores) are as large as the node's cores, so it is
# schedulable only because it requests none of them.
virtualisation = {
memorySize = 10240;
cores = 6;
memorySize = 8192;
cores = 4;
diskSize = 12288;
};
};
@ -483,12 +484,16 @@ pkgs.testers.runNixOSTest {
volumes = json.loads(kube(machine, f"get pod {stt_pod} -o json"))["spec"]["volumes"]
assert not any("secret" in v for v in volumes), volumes
with subtest("the speech-to-text pod carries its unit's limits, and no requests of its own"):
# The unit sets MemoryMax=3G and CPUQuota=400%. The API server prints
# 4000m as 4. Kubernetes fills the requests in from the limits.
spec = json.loads(kube(machine, f"get pod {stt_pod} -o json"))["spec"]["containers"][0]["resources"]
assert spec["limits"] == {"cpu": "4", "memory": "3Gi"}, spec
assert spec["requests"] == spec["limits"], spec
with subtest("the speech-to-text pod carries its unit's limits, reserves nothing, and is Burstable"):
# The unit sets MemoryMax=3G and CPUQuota=400% and no MemoryMin or
# MemoryLow. The API server prints 4000m as 4 and accepts the requests
# of 0 the renderer sets.
def pod_field(path: str) -> str:
return kube(machine, f"get pod {stt_pod} -o jsonpath='{{{path}}}'")
assert json.loads(pod_field(".spec.containers[0].resources.limits")) == {"cpu": "4", "memory": "3Gi"}
assert json.loads(pod_field(".spec.containers[0].resources.requests")) == {"cpu": "0", "memory": "0"}
assert pod_field(".status.qosClass") == "Burstable", pod_field(".status.qosClass")
# A pod whose unit sets neither is the control that the field is read
# from the unit: the router's has none.
router_resources = json.loads(kube(machine, f"get pod {router} -o json"))["spec"]["containers"][0]["resources"]