-
Notifications
You must be signed in to change notification settings - Fork 355
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 cgroup namespaces for cgroup v1 #349
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -187,11 +187,11 @@ pub fn container_init( | |
&& ns_type != CloneFlags::CLONE_NEWPID | ||
&& ns_type != CloneFlags::CLONE_NEWNS | ||
}) | ||
.with_context(|| "Failed to apply namespaces")?; | ||
.with_context(|| "failed to apply namespaces")?; | ||
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. 👍 |
||
if let Some(mount_namespace) = namespaces.get(LinuxNamespaceType::Mount) { | ||
namespaces | ||
.unshare_or_setns(mount_namespace) | ||
.with_context(|| format!("Failed to enter mount namespace: {:?}", mount_namespace))?; | ||
.with_context(|| format!("failed to enter mount namespace: {:?}", mount_namespace))?; | ||
} | ||
|
||
// Only set the host name if entering into a new uts namespace | ||
|
@@ -216,8 +216,13 @@ pub fn container_init( | |
} | ||
|
||
let bind_service = namespaces.get(LinuxNamespaceType::User).is_some(); | ||
rootfs::prepare_rootfs(spec, rootfs, bind_service) | ||
.with_context(|| "Failed to prepare rootfs")?; | ||
rootfs::prepare_rootfs( | ||
spec, | ||
rootfs, | ||
bind_service, | ||
namespaces.get(LinuxNamespaceType::Cgroup).is_some(), | ||
) | ||
.with_context(|| "Failed to prepare rootfs")?; | ||
|
||
// Entering into the rootfs jail. If mount namespace is specified, then | ||
// we use pivot_root, but if we are on the host mount namespace, we will | ||
|
This file contains 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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
use crate::{namespaces::Namespaces, process::channel, process::fork}; | ||
use anyhow::{Context, Result}; | ||
use nix::unistd::{Gid, Uid}; | ||
use oci_spec::runtime::LinuxNamespaceType; | ||
use anyhow::{Context, Error, Result}; | ||
use cgroups::common::CgroupManager; | ||
use nix::unistd::{Gid, Pid, Uid}; | ||
use oci_spec::runtime::{LinuxNamespaceType, LinuxResources}; | ||
use procfs::process::Process; | ||
use std::convert::From; | ||
|
||
use super::args::ContainerArgs; | ||
use super::init::container_init; | ||
|
@@ -23,7 +26,7 @@ pub fn container_intermediate( | |
if let Some(user_namespace) = namespaces.get(LinuxNamespaceType::User) { | ||
namespaces | ||
.unshare_or_setns(user_namespace) | ||
.with_context(|| format!("Failed to enter pid namespace: {:?}", user_namespace))?; | ||
.with_context(|| format!("Failed to enter user namespace: {:?}", user_namespace))?; | ||
if user_namespace.path().is_none() { | ||
log::debug!("creating new user namespace"); | ||
// child needs to be dumpable, otherwise the non root parent is not | ||
|
@@ -60,6 +63,17 @@ pub fn container_intermediate( | |
.with_context(|| format!("Failed to enter pid namespace: {:?}", pid_namespace))?; | ||
} | ||
|
||
// this needs to be done before we create the init process, so that the init | ||
// process will already be captured by the cgroup | ||
if args.rootless.is_none() { | ||
apply_cgroups( | ||
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. Can I ask you to update docs/.drawio.svg using vscode-drawing. |
||
args.cgroup_manager.as_ref(), | ||
linux.resources().as_ref(), | ||
args.init, | ||
) | ||
.context("failed to apply cgroups")? | ||
} | ||
|
||
// We only need for init process to send us the ChildReady. | ||
let (sender_to_intermediate, receiver_from_init) = &mut channel::init_to_intermediate()?; | ||
|
||
|
@@ -90,3 +104,94 @@ pub fn container_intermediate( | |
|
||
Ok(()) | ||
} | ||
|
||
fn apply_cgroups<C: CgroupManager + ?Sized>( | ||
cmanager: &C, | ||
resources: Option<&LinuxResources>, | ||
init: bool, | ||
) -> Result<(), Error> { | ||
let pid = Pid::from_raw(Process::myself()?.pid()); | ||
cmanager | ||
.add_task(pid) | ||
.with_context(|| format!("failed to add task {} to cgroup manager", pid))?; | ||
|
||
if let Some(resources) = resources { | ||
if init { | ||
let controller_opt = cgroups::common::ControllerOpt { | ||
resources, | ||
freezer_state: None, | ||
oom_score_adj: None, | ||
disable_oom_killer: false, | ||
}; | ||
|
||
cmanager | ||
.apply(&controller_opt) | ||
.context("failed to apply resource limits to cgroup")?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::apply_cgroups; | ||
use anyhow::Result; | ||
use cgroups::test_manager::TestManager; | ||
use nix::unistd::Pid; | ||
use oci_spec::runtime::LinuxResources; | ||
use procfs::process::Process; | ||
|
||
#[test] | ||
fn apply_cgroup_init() -> Result<()> { | ||
// arrange | ||
let cmanager = TestManager::default(); | ||
let resources = LinuxResources::default(); | ||
|
||
// act | ||
apply_cgroups(&cmanager, Some(&resources), true)?; | ||
|
||
// assert | ||
assert!(cmanager.get_add_task_args().len() == 1); | ||
assert_eq!( | ||
cmanager.get_add_task_args()[0], | ||
Pid::from_raw(Process::myself()?.pid()) | ||
); | ||
assert!(cmanager.apply_called()); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn apply_cgroup_tenant() -> Result<()> { | ||
// arrange | ||
let cmanager = TestManager::default(); | ||
let resources = LinuxResources::default(); | ||
|
||
// act | ||
apply_cgroups(&cmanager, Some(&resources), false)?; | ||
|
||
// assert | ||
assert_eq!( | ||
cmanager.get_add_task_args()[0], | ||
Pid::from_raw(Process::myself()?.pid()) | ||
); | ||
assert!(!cmanager.apply_called()); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn apply_cgroup_no_resources() -> Result<()> { | ||
// arrange | ||
let cmanager = TestManager::default(); | ||
|
||
// act | ||
apply_cgroups(&cmanager, None, true)?; | ||
// assert | ||
assert_eq!( | ||
cmanager.get_add_task_args()[0], | ||
Pid::from_raw(Process::myself()?.pid()) | ||
); | ||
assert!(!cmanager.apply_called()); | ||
Ok(()) | ||
} | ||
} |
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.
👍