agent: determine value of use_systemd_cgroup before LinuxContainer::new()

Right now LinuxContainer::new() gets passed a CreateOpts struct, but then
modifies the use_systemd_cgroup field inside that struct. Pull the cgroups path
parsing logic into do_create_container, so that CreateOpts can be immutable in
LinuxContainer::new. This is just moving things around, there should be no
functional changes.

Signed-off-by: Jeremi Piotrowski <jpiotrowski@microsoft.com>
This commit is contained in:
Jeremi Piotrowski 2023-02-24 13:43:26 +01:00
parent ad8968c8d9
commit b0691806f1
2 changed files with 10 additions and 11 deletions

View File

@ -1449,7 +1449,7 @@ impl LinuxContainer {
pub fn new<T: Into<String> + Display + Clone>( pub fn new<T: Into<String> + Display + Clone>(
id: T, id: T,
base: T, base: T,
mut config: Config, config: Config,
logger: &Logger, logger: &Logger,
) -> Result<Self> { ) -> Result<Self> {
let base = base.into(); let base = base.into();
@ -1475,21 +1475,14 @@ impl LinuxContainer {
.context(format!("Cannot change owner of container {} root", id))?; .context(format!("Cannot change owner of container {} root", id))?;
let spec = config.spec.as_ref().unwrap(); let spec = config.spec.as_ref().unwrap();
let linux = spec.linux.as_ref().unwrap(); let linux = spec.linux.as_ref().unwrap();
let cpath = if config.use_systemd_cgroup {
// determine which cgroup driver to take and then assign to config.use_systemd_cgroup
// systemd: "[slice]:[prefix]:[name]"
// fs: "/path_a/path_b"
let cpath = if SYSTEMD_CGROUP_PATH_FORMAT.is_match(linux.cgroups_path.as_str()) {
config.use_systemd_cgroup = true;
if linux.cgroups_path.len() == 2 { if linux.cgroups_path.len() == 2 {
format!("system.slice:kata_agent:{}", id.as_str()) format!("system.slice:kata_agent:{}", id.as_str())
} else { } else {
linux.cgroups_path.clone() linux.cgroups_path.clone()
} }
} else { } else {
config.use_systemd_cgroup = false;
if linux.cgroups_path.is_empty() { if linux.cgroups_path.is_empty() {
format!("/{}", id.as_str()) format!("/{}", id.as_str())
} else { } else {

View File

@ -36,7 +36,7 @@ use protocols::health::{
use protocols::types::Interface; use protocols::types::Interface;
use protocols::{agent_ttrpc_async as agent_ttrpc, health_ttrpc_async as health_ttrpc}; use protocols::{agent_ttrpc_async as agent_ttrpc, health_ttrpc_async as health_ttrpc};
use rustjail::cgroups::notifier; use rustjail::cgroups::notifier;
use rustjail::container::{BaseContainer, Container, LinuxContainer}; use rustjail::container::{BaseContainer, Container, LinuxContainer, SYSTEMD_CGROUP_PATH_FORMAT};
use rustjail::process::Process; use rustjail::process::Process;
use rustjail::specconv::CreateOpts; use rustjail::specconv::CreateOpts;
@ -210,9 +210,15 @@ impl AgentService {
// restore the cwd for kata-agent process. // restore the cwd for kata-agent process.
defer!(unistd::chdir(&olddir).unwrap()); defer!(unistd::chdir(&olddir).unwrap());
// determine which cgroup driver to take and then assign to use_systemd_cgroup
// systemd: "[slice]:[prefix]:[name]"
// fs: "/path_a/path_b"
let cgroups_path = oci.linux.as_ref().map_or("", |linux| &linux.cgroups_path);
let use_systemd_cgroup = SYSTEMD_CGROUP_PATH_FORMAT.is_match(cgroups_path);
let opts = CreateOpts { let opts = CreateOpts {
cgroup_name: "".to_string(), cgroup_name: "".to_string(),
use_systemd_cgroup: false, use_systemd_cgroup,
no_pivot_root: s.no_pivot_root, no_pivot_root: s.no_pivot_root,
no_new_keyring: false, no_new_keyring: false,
spec: Some(oci.clone()), spec: Some(oci.clone()),