Skip to content

engine: keep cgroups inside the engine's container under Kubernetes - #15095

Merged
sipsma merged 2 commits into
mainfrom
sipsma/engine-cgroupns
Nov 9, 2026
Merged

sipsma merged 2 commits into
mainfrom
sipsma/engine-cgroupns

Conversation

@sipsma

@sipsma sipsma commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Under Kubernetes, the engine's pod CPU and memory limits didn't apply to anything the engine runs. With this change, every exec, and the engine itself, stays inside the engine container's cgroup and under its limits.

Reported on Discord against v0.21.11, and earlier in #10813.

What was happening

On cgroup v2, containerd's Kubernetes runtime gives a container its own cgroup namespace only when it isn't privileged. The Helm chart runs the engine privileged, so the engine sees the node's whole cgroup tree at /sys/fs/cgroup. The entrypoint's cgroup setup and every exec's cgroup (/exec/<id>, /buildkit/<id> on 0.21) are counted from that root, so they landed at the node's root, beside kubepods.slice.

  • Execs ran unthrottled. On k3s with the published v0.21.11 chart and a pod limited to 1 CPU, one exec used 3.97 cores and was never throttled, while the pod's own cgroup showed 0.01 cores.
  • Since v1.0.0-beta.24, the engine itself left the pod. From inside the pod, every PID in the node's root cgroup.procs reads as 0, and writing 0 to cgroup.procs moves the writer. The entrypoint's move loop, which since engine: a batch of performance changes: cache hits, allocations, Go 1.27, faster engine start, image import and services #15007 writes with the shell's builtin echo, therefore moved the entrypoint shell into the node's /sys/fs/cgroup/engine, and tini, the engine and dnsmasq followed it. With beta.25, the pod's limits constrained nothing Dagger ran.

Under Docker, a container gets its own cgroup namespace by default, so all of this already nested correctly there.

The fix

The first commit: when the entrypoint's own cgroup isn't the root of its cgroup namespace, it binds its own cgroup over /sys/fs/cgroup and re-runs itself under unshare --cgroup. Everything after that sees the container's cgroup as the root, as under Docker, and the existing setup runs unchanged inside it. When the engine already starts at its namespace root, as under Docker by default, nothing changes: the script runs once, as before.

unshare comes from Wolfi's util-linux-misc, now in the engine image. Wolfi's busybox has no unshare applet. The package adds about 22 MB to a roughly 1 GB image.

Verification

I verified this by hand, using engine images built from this branch and from its backport to 0.21, on k3s v1.37.2 (cgroup v2, systemd driver) with the Helm chart and a pod limit of 1 CPU and 2 GiB:

  • The engine runs in <container scope>/engine, and execs, git and rg run under the same scope. Nothing new appears at the node's cgroup root.
  • A 4-thread busy-loop exec is held to the pod's 1 CPU (0.92 cores for the exec, 1.00 for the pod), with throttling counted on the pod.
  • The readiness and liveness probes, and kubectl exec, still work. runc places them in the engine's leaf cgroup.
  • Under Docker, with the default private cgroup namespace, nothing changes. With --cgroupns=host, execs now nest under the container instead of escaping.

Memory limits. An exec that exceeds the pod's memory limit now meets that limit. Kubelet sets memory.oom.group=1 on each container's cgroup, which would make the kernel kill every process in the container, the engine included. The second commit clears that flag on the engine's own container cgroup when the entrypoint takes over its cgroup, so an OOM kill takes only what the kernel picks, as under Docker. A 3 GB exec in a 2 GiB pod then fails with exit 137 inside the exec, and the engine keeps running without a restart. Without the second commit, the whole engine container restarted and reset its cache.

CI doesn't cover this path: the Helm e2e test installs the published registry.dagger.io/engine:main image, so it can't exercise a change to the entrypoint in a PR.

A privileged container on cgroup v2 under Kubernetes' containerd shares
the host's cgroup namespace, so /sys/fs/cgroup is the node's whole tree.
The entrypoint's cgroup setup and every exec's cgroup then landed at the
node's root, beside kubepods.slice, where the pod's CPU and memory
limits don't apply: execs ran unthrottled, and since v1.0.0-beta.24 the
engine process itself also moved there, because every PID in the node's
root cgroup reads as 0 from inside the pod and writing 0 to cgroup.procs
moves the writer.

When the entrypoint's own cgroup isn't its namespace root, bind its own
cgroup over /sys/fs/cgroup and re-run itself in a new cgroup namespace
rooted there. Everything below then sees the container's cgroup as the
root, as it does under Docker's private cgroup namespace, and the
existing setup runs unchanged inside it.

unshare comes from Wolfi's util-linux-misc, now installed in the
engine image; Wolfi's busybox has no unshare applet.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/engine-cgroupns branch from 6a6ceb2 to 1783df7 Compare November 7, 2026 03:43
…y limit

With the engine's cgroups inside its container, an exec now meets the
container's memory limit. Kubelet sets memory.oom.group=1 on every
container's cgroup, so the kernel then killed every process in the
container: the engine restarted, dropping every session, and reset its
cache after the unclean shutdown.

Clear memory.oom.group on the engine's own container cgroup when the
entrypoint takes over its cgroup, so the OOM killer takes only what it
picks, as under Docker: the exec fails and the engine keeps running.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit e18cc07 into main Nov 9, 2026
95 checks passed
@sipsma
sipsma deleted the sipsma/engine-cgroupns branch November 9, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants