Repository navigation
engine: keep cgroups inside the engine's container under Kubernetes - #15095
Merged
Merged
Conversation
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
force-pushed
the
sipsma/engine-cgroupns
branch
from
November 7, 2026 03:43
6a6ceb2 to
1783df7
Compare
…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]>
marcosnils
approved these changes
Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, besidekubepods.slice.cgroup.procsreads as0, and writing0tocgroup.procsmoves 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 builtinecho, 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/cgroupand re-runs itself underunshare --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.unsharecomes from Wolfi'sutil-linux-misc, now in the engine image. Wolfi's busybox has nounshareapplet. 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:
<container scope>/engine, and execs,gitandrgrun under the same scope. Nothing new appears at the node's cgroup root.kubectl exec, still work. runc places them in the engine's leaf cgroup.--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=1on 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:mainimage, so it can't exercise a change to the entrypoint in a PR.