-
Notifications
You must be signed in to change notification settings - Fork 19
Place inner container cgroups under the pod's cgroup tree #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamestoyer
wants to merge
1
commit into
coder:main
Choose a base branch
from
jamestoyer:feat/dind-cgroup-attribution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -256,7 +256,8 @@ func dockerCmd() *cobra.Command { | |
| log.Debug(ctx, "starting dockerd", slog.F("args", args)) | ||
|
|
||
| blog.Info("Waiting for sysbox processes to startup...") | ||
| dockerd := background.New(ctx, log, "dockerd", dargs...) | ||
| wrapCmd, wrapArgs := wrapDockerdCmd(dargs) | ||
| dockerd := background.New(ctx, log, wrapCmd, wrapArgs...) | ||
| err = dockerd.Start() | ||
| if err != nil { | ||
| return xerrors.Errorf("start dockerd: %w", err) | ||
|
|
@@ -289,7 +290,8 @@ func dockerCmd() *cobra.Command { | |
| log.Fatal(ctx, "dockerd exited, failed getting args for restart", slog.Error(err)) | ||
| } | ||
|
|
||
| err = dockerd.Restart(ctx, "dockerd", args...) | ||
| wrapCmd, wrapArgs := wrapDockerdCmd(args) | ||
| err = dockerd.Restart(ctx, wrapCmd, wrapArgs...) | ||
| if err != nil { | ||
| blog.Info("Failed to create Container-based Virtual Machine: " + err.Error()) | ||
| //nolint | ||
|
|
@@ -357,7 +359,8 @@ func dockerCmd() *cobra.Command { | |
|
|
||
| log.Debug(ctx, "restarting dockerd", slog.F("args", args)) | ||
|
|
||
| err = dockerd.Restart(ctx, "dockerd", args...) | ||
| wrapCmd, wrapArgs := wrapDockerdCmd(args) | ||
| err = dockerd.Restart(ctx, wrapCmd, wrapArgs...) | ||
| if err != nil { | ||
| return xerrors.Errorf("restart dockerd: %w", err) | ||
| } | ||
|
|
@@ -881,6 +884,55 @@ func dockerdArgs(link, cidr string, isNoSpace bool) ([]string, error) { | |
| return args, nil | ||
| } | ||
|
|
||
| // wrapDockerdCmd wraps the dockerd invocation with `unshare --cgroup` + | ||
| // cgroup2 remount + cgroupv2 delegation setup. This creates a new cgroup | ||
| // namespace for dockerd and moves existing processes into a sibling `/init` | ||
| // cgroup so the root cgroup can enable subtree_control. Without this, cgroupv2 | ||
| // "no internal processes" rule prevents Docker from creating child cgroups | ||
| // with processes under a cgroup that already has processes. | ||
| // | ||
| // Note: the `umount /sys/fs/cgroup && mount -t cgroup2 ...` leaks back into | ||
| // envbox's mount namespace because we don't use `--mount`. Using `--mount` | ||
| // would break sysbox-fs propagation (its per-container mounts under | ||
| // /var/lib/sysboxfs/ stop being visible to sysbox-runc in the dockerd NS). | ||
| // The side effect of the leak is a non-fatal "Unable to read CPU quota" | ||
| // warning when envbox later tries to read /sys/fs/cgroup/<host-path>/cpu.max | ||
| // — that path no longer exists under the remounted view. The inner | ||
| // container's CPU limits still flow through the parent pod's cgroup hierarchy, | ||
| // but cgroup-aware tools inside the workspace may see the host CPU count. | ||
| // | ||
| // The cgroup delegation logic mirrors moby's `hack/dind` wrapper: | ||
| // https://github.com/moby/moby/blob/master/hack/dind | ||
| // | ||
| // After this setup, inner container cgroups are placed under the envbox | ||
| // container's cgroup on the host. Tetragon's cgtracker can then associate | ||
| // these child cgroups with the parent pod for attribution. | ||
| // | ||
| // See also: https://github.com/moby/moby/issues/45378#issuecomment-2886261231 | ||
| func wrapDockerdCmd(dargs []string) (string, []string) { | ||
| shellCmd := `set -e | ||
| umount /sys/fs/cgroup | ||
| mount -t cgroup2 cgroup /sys/fs/cgroup | ||
|
Comment on lines
+914
to
+915
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer if we didn't do this on a |
||
| if [ -f /sys/fs/cgroup/cgroup.controllers ]; then | ||
| mkdir -p /sys/fs/cgroup/init | ||
| while ! { | ||
| xargs -rn1 < /sys/fs/cgroup/cgroup.procs > /sys/fs/cgroup/init/cgroup.procs || : | ||
| sed -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllers > /sys/fs/cgroup/cgroup.subtree_control | ||
| }; do :; done | ||
| fi | ||
| exec "$0" "$@" | ||
| ` | ||
| wrapperArgs := []string{ | ||
| "--cgroup", | ||
| "/bin/sh", | ||
| "-c", | ||
| shellCmd, | ||
| "dockerd", | ||
| } | ||
| wrapperArgs = append(wrapperArgs, dargs...) | ||
| return "unshare", wrapperArgs | ||
| } | ||
|
|
||
| // TODO This is bad code. | ||
| func filterElements(ss []string, filters ...string) []string { | ||
| filtered := make([]string, 0, len(ss)) | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we permalink directly to the relevant portion https://github.com/moby/moby/blob/ee3e21b70457f90d537640613d59340db1f0178c/hack/dind#L61-L79
I'd prefer if we kept the verbatim comments from upstream, if possible