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

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 = [