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 e1c1bad51a
5 changed files with 114 additions and 36 deletions

View file

@ -102,9 +102,12 @@ credential directory.
(systemd's `K`, `M`, `G` and `T` are Kubernetes' `Ki`, `Mi`, `Gi` and `Ti`; a
byte count passes unchanged; `infinity` sets none) and its `CPUQuota`, a
percent of one core, becomes the cpu limit in millicores (`400%` is
`4000m`). The renderer sets no `requests`, so Kubernetes reserves the limits
on the node. A value in another form fails evaluation. `TasksMax` has no pod
field, so the renderer drops it. The unit's sandbox settings
`4000m`). Both are caps, not reservations, so the renderer sets the requests
too: Kubernetes would otherwise set each to its limit and reserve the whole
cap on the node. The memory request is the unit's `MemoryMin`, else its
`MemoryLow`, else `0`, and the cpu request is `0`, so the pod is Burstable. A
value in another form fails evaluation. `TasksMax` has no pod field, so the
renderer drops it. The unit's sandbox settings
(`MemoryDenyWriteExecute`, `SystemCallFilter`, `PrivateUsers` and the rest)
stay on the host: the pod has the security shape above and no other.
- **Probes:** a service with an opt-in health listener gets its variable set to

View file

@ -93,9 +93,9 @@ store path the container reads, and the volume doesn't.
The pod takes the unit's `MemoryMax` and `CPUQuota` as its container's limits
([manifests](manifests.md#pods)), so `swarm-stt` keeps its 3 GiB and four-core
ceilings. The pod has no `requests`, so Kubernetes reserves those limits on the
node: the node needs that memory and those cores free to schedule the pod.
`TasksMax` has no pod counterpart, so the renderer drops it. The systemd sandbox
ceilings. The pod requests nothing (memory `0`, cpu `0`, as the unit sets no
`MemoryMin` or `MemoryLow`), so the node reserves nothing for it and the pod is
Burstable. `TasksMax` has no pod counterpart, so the renderer drops it. The systemd sandbox
(`MemoryDenyWriteExecute`, `SystemCallFilter`, `PrivateUsers`, the read-only
`ProtectSystem` and the rest) stays on the host. The pod is a non-root user with
a read-only root filesystem, every capability dropped, the runtime's default
@ -114,4 +114,4 @@ a queue. It switches both from the host to the cluster and back, checks each
step for exactly one copy of each, and asserts that the router's pod logs in,
routes an event, and exits 0 when deleted. For `swarm-stt` it asserts that the
pod is Ready, logged in to the store with its ServiceAccount token, carries the
unit's limits, and has its cache volume bound.
unit's limits, requests nothing and is Burstable, and has its cache volume bound.

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"]