Skip to content
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

Support resource restrictions for rootless containers #499

Merged
merged 8 commits into from
Nov 28, 2021

Conversation

Furisto
Copy link
Collaborator

@Furisto Furisto commented Nov 24, 2021

Resource restrictions can now be applied to rootless containers (using systemd).

Systemd places units started by unprivileged users under /user.slice/user-$(id -u).slice/user@$(id -u).service. In order for resource restrictions to be applied correctly, you have to enable delegation for this service unit. This describes how to do that.

- Use systemd client to find systemd cgroup root
- Add error context
- Manager debug impl
- Comments
- Set default slice name for rootless and rootfull containers
@codecov-commenter
Copy link

codecov-commenter commented Nov 25, 2021

Codecov Report

Merging #499 (1a14c43) into main (8b9117d) will decrease coverage by 0.54%.
The diff coverage is 22.48%.

@@            Coverage Diff             @@
##             main     #499      +/-   ##
==========================================
- Coverage   61.09%   60.55%   -0.55%     
==========================================
  Files          85       86       +1     
  Lines       12417    12604     +187     
==========================================
+ Hits         7586     7632      +46     
- Misses       4831     4972     +141     

@Furisto Furisto marked this pull request as ready for review November 25, 2021 19:56
@Furisto Furisto requested a review from utam0k November 25, 2021 19:57
} else {
let parts = cgroups_path
.to_str()
.ok_or_else(|| anyhow!("failed to parse cgroupsPath field"))?
.ok_or_else(|| anyhow!("failed to parse cgroups path"))?
Copy link
Member

@utam0k utam0k Nov 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the error is raised when the cgroup path is not in the format of [slice]:[scope_prefix]:[name], right?
I want to output the error message properly in case it fails.

print_feature_status(&content, "CONFIG_USER_NS", FeatureDisplay::new("user"));

let user_display = match rootless::unprivileged_user_ns_enabled() {
Ok(false) => FeatureDisplay::with_status("user", "enabled (root only)", "disabled"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// this needs to be done before we create the init process, so that the init
// process will already be captured by the cgroup. It also needs to be done
// before we enter the user namespace because if a privileged user starts a
// rootless container on a cgroup v1 system we can still fullfill resource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// rootless container on a cgroup v1 system we can still fullfill resource
// rootless container on a cgroup v1 system we can still fulfill resource

Comment on lines 25 to 40
// this needs to be done before we create the init process, so that the init
// process will already be captured by the cgroup. It also needs to be done
// before we enter the user namespace because if a privileged user starts a
// rootless container on a cgroup v1 system we can still fullfill resource
// restrictions through the cgroup fs support (delegation through systemd is
// not supported for v1 by us). This only works if the user has not yet been
// mapped to an unprivileged user by the user namespace however.
// In addition this needs to be done before we enter the cgroup namespace as
// the cgroup of the process will form the root of the cgroup hierarchy in
// the cgroup namespace.
apply_cgroups(
args.cgroup_manager.as_ref(),
linux.resources().as_ref(),
args.init,
)
.context("failed to apply cgroups")?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I ask you to update the group part of the sequence diagram in README?

Comment on lines +36 to +49
/// Root path of the cgroup hierarchy e.g. /sys/fs/cgroup
root_path: PathBuf,
/// Path relative to the root path e.g. /system.slice/youki-569d5ce3afe1074769f67.scope for rootfull containers
/// and e.g. /user.slice/user-1000/user@1000.service/youki-569d5ce3afe1074769f67.scope for rootless containers
cgroups_path: PathBuf,
/// Combination of root path and cgroups path
full_path: PathBuf,
/// Destructured cgroups path as specified in the runtime spec e.g. system.slice:youki:569d5ce3afe1074769f67
destructured_path: CgroupsPath,
/// Name of the container e.g. 569d5ce3afe1074769f67
container_name: String,
/// Name of the systemd unit e.g. youki-569d5ce3afe1074769f67.scope
unit_name: String,
/// Client for communicating with systemd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

})
}

fn destructure_cgroups_path(cgroups_path: PathBuf) -> Result<CgroupsPath> {
log::debug!("CGROUPS PATH IS {:?}", cgroups_path);
fn destructure_cgroups_path(cgroups_path: &Path) -> Result<CgroupsPath> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be implemented impl TryFrom<Path> for CgroupPath. What do you think?

- Add cgroups path to error context
- Correct spelling mistake
- Update sequence diagram
- Implement TryFrom for CgroupsPath
@Furisto
Copy link
Collaborator Author

Furisto commented Nov 28, 2021

@utam0k PTAL

Copy link
Member

@utam0k utam0k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

@utam0k utam0k merged commit 8310ac4 into youki-dev:main Nov 28, 2021
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.

3 participants