mirror of
https://github.com/containers/youki
synced 2024-11-23 09:21:57 +01:00
Move dbus code to libcgroups create
This commit is contained in:
parent
a3ce77064d
commit
d018a24814
@ -189,7 +189,7 @@ pub fn create_cgroup_manager<P: Into<PathBuf>>(
|
||||
bail!("systemd cgroup flag passed, but systemd support for managing cgroups is not available");
|
||||
}
|
||||
log::info!("systemd cgroup manager will be used");
|
||||
return Ok(Box::new(v2::SystemDCGroupManager::new(
|
||||
return Ok(Box::new(crate::systemd::manager::Manager::new(
|
||||
DEFAULT_CGROUP_ROOT.into(),
|
||||
cgroup_path.into(),
|
||||
)?));
|
||||
|
@ -8,6 +8,7 @@ mod test;
|
||||
|
||||
pub mod common;
|
||||
pub mod stats;
|
||||
pub mod systemd;
|
||||
pub mod test_manager;
|
||||
pub mod v1;
|
||||
pub mod v2;
|
||||
|
39
crates/libcgroups/src/systemd/controller_type.rs
Normal file
39
crates/libcgroups/src/systemd/controller_type.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
pub enum ControllerType {
|
||||
Cpu,
|
||||
Io,
|
||||
Memory,
|
||||
Tasks,
|
||||
}
|
||||
|
||||
impl Display for ControllerType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let print = match self {
|
||||
ControllerType::Cpu => "cpu",
|
||||
ControllerType::Io => "io",
|
||||
ControllerType::Memory => "memory",
|
||||
ControllerType::Tasks => "tasks",
|
||||
};
|
||||
|
||||
write!(f, "{}", print)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for ControllerType {
|
||||
fn as_ref(&self) -> &str {
|
||||
match self {
|
||||
ControllerType::Cpu => "cpu",
|
||||
ControllerType::Io => "io",
|
||||
ControllerType::Memory => "memory",
|
||||
ControllerType::Tasks => "tasks",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const CONTROLLER_TYPES: &[ControllerType] = &[
|
||||
ControllerType::Cpu,
|
||||
ControllerType::Io,
|
||||
ControllerType::Memory,
|
||||
ControllerType::Tasks,
|
||||
];
|
@ -1,6 +1,6 @@
|
||||
use crate::dbus::systemd_manager::OrgFreedesktopSystemd1Manager;
|
||||
use crate::systemd::dbus::systemd_api::OrgFreedesktopSystemd1Manager;
|
||||
use anyhow::Result;
|
||||
use dbus::arg::{RefArg, Variant, PropMap};
|
||||
use dbus::arg::{PropMap, Variant};
|
||||
use dbus::blocking::Connection;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -25,7 +25,7 @@ impl Client {
|
||||
unit_name: &str,
|
||||
mut properties_map: PropMap,
|
||||
) -> Result<()> {
|
||||
// To view and introspect the methods under the 'org.freedesktop.systemd1' destination
|
||||
// To view and introspect the methods under the 'org.freedesktop.systemd1' destination
|
||||
// and object path under it use the following command:
|
||||
// `gdbus introspect --system --dest org.freedesktop.systemd1 --object-path /org/freedesktop/systemd1`
|
||||
let proxy = self.conn.with_proxy(
|
||||
@ -62,7 +62,7 @@ impl Client {
|
||||
|
||||
let mut properties = vec![];
|
||||
for (name, variant) in &mut properties_map.iter() {
|
||||
let s : &str = &*name;
|
||||
let s: &str = &*name;
|
||||
properties.push((s, variant.to_owned()));
|
||||
}
|
||||
//proxy.start_transient_unit(unit_name, "replace", properties, vec![])?;
|
2
crates/libcgroups/src/systemd/dbus/mod.rs
Normal file
2
crates/libcgroups/src/systemd/dbus/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub(crate) mod client;
|
||||
pub(crate) mod systemd_api;
|
0
crates/libcontainer/src/dbus/systemd_manager.rs → crates/libcgroups/src/systemd/dbus/systemd_api.rs
0
crates/libcontainer/src/dbus/systemd_manager.rs → crates/libcgroups/src/systemd/dbus/systemd_api.rs
@ -8,12 +8,9 @@ use anyhow::{anyhow, bail, Result};
|
||||
use nix::unistd::Pid;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use super::controller_type::{ControllerType, CONTROLLER_TYPES};
|
||||
#[cfg(feature = "cgroupsv2_devices")]
|
||||
use super::devices::Devices;
|
||||
use super::{
|
||||
controller::Controller, controller_type::ControllerType, cpu::Cpu, cpuset::CpuSet,
|
||||
freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, pids::Pids,
|
||||
};
|
||||
use crate::common::{self, CgroupManager, ControllerOpt, FreezerState, PathBufExt};
|
||||
use crate::stats::Stats;
|
||||
|
||||
@ -21,16 +18,8 @@ const CGROUP_PROCS: &str = "cgroup.procs";
|
||||
const CGROUP_CONTROLLERS: &str = "cgroup.controllers";
|
||||
const CGROUP_SUBTREE_CONTROL: &str = "cgroup.subtree_control";
|
||||
|
||||
// v2 systemd only supports cpu, io, memory and pids.
|
||||
const CONTROLLER_TYPES: &[ControllerType] = &[
|
||||
ControllerType::Cpu,
|
||||
ControllerType::Io,
|
||||
ControllerType::Memory,
|
||||
ControllerType::Pids,
|
||||
];
|
||||
|
||||
/// SystemDCGroupManager is a driver for managing cgroups via systemd.
|
||||
pub struct SystemDCGroupManager {
|
||||
pub struct Manager {
|
||||
root_path: PathBuf,
|
||||
cgroups_path: PathBuf,
|
||||
full_path: PathBuf,
|
||||
@ -46,14 +35,14 @@ struct CgroupsPath {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl SystemDCGroupManager {
|
||||
impl Manager {
|
||||
pub fn new(root_path: PathBuf, cgroups_path: PathBuf) -> Result<Self> {
|
||||
// TODO: create the systemd unit using a dbus client.
|
||||
let destructured_path = Self::destructure_cgroups_path(cgroups_path)?;
|
||||
let cgroups_path = Self::construct_cgroups_path(destructured_path)?;
|
||||
let full_path = root_path.join_safely(&cgroups_path)?;
|
||||
|
||||
Ok(SystemDCGroupManager {
|
||||
Ok(Manager {
|
||||
root_path,
|
||||
cgroups_path,
|
||||
full_path,
|
||||
@ -203,7 +192,7 @@ impl SystemDCGroupManager {
|
||||
"cpu" => controllers.push(ControllerType::Cpu),
|
||||
"io" => controllers.push(ControllerType::Io),
|
||||
"memory" => controllers.push(ControllerType::Memory),
|
||||
"pids" => controllers.push(ControllerType::Pids),
|
||||
"pids" => controllers.push(ControllerType::Tasks),
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
@ -220,7 +209,7 @@ impl SystemDCGroupManager {
|
||||
}
|
||||
}
|
||||
|
||||
impl CgroupManager for SystemDCGroupManager {
|
||||
impl CgroupManager for Manager {
|
||||
fn add_task(&self, pid: Pid) -> Result<()> {
|
||||
// Dont attach any pid to the cgroup if -1 is specified as a pid
|
||||
if pid.as_raw() == -1 {
|
||||
@ -234,12 +223,7 @@ impl CgroupManager for SystemDCGroupManager {
|
||||
fn apply(&self, controller_opt: &ControllerOpt) -> Result<()> {
|
||||
for controller in CONTROLLER_TYPES {
|
||||
match controller {
|
||||
ControllerType::Cpu => Cpu::apply(controller_opt, &self.full_path)?,
|
||||
ControllerType::CpuSet => CpuSet::apply(controller_opt, &self.full_path)?,
|
||||
ControllerType::HugeTlb => HugeTlb::apply(controller_opt, &self.full_path)?,
|
||||
ControllerType::Io => Io::apply(controller_opt, &self.full_path)?,
|
||||
ControllerType::Memory => Memory::apply(controller_opt, &self.full_path)?,
|
||||
ControllerType::Pids => Pids::apply(controller_opt, &self.full_path)?,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@ -253,13 +237,7 @@ impl CgroupManager for SystemDCGroupManager {
|
||||
}
|
||||
|
||||
fn freeze(&self, state: FreezerState) -> Result<()> {
|
||||
let controller_opt = ControllerOpt {
|
||||
resources: &Default::default(),
|
||||
freezer_state: Some(state),
|
||||
oom_score_adj: None,
|
||||
disable_oom_killer: false,
|
||||
};
|
||||
Freezer::apply(&controller_opt, &self.full_path)
|
||||
todo!();
|
||||
}
|
||||
|
||||
fn stats(&self) -> Result<Stats> {
|
||||
@ -278,7 +256,7 @@ mod tests {
|
||||
#[test]
|
||||
fn expand_slice_works() -> Result<()> {
|
||||
assert_eq!(
|
||||
SystemDCGroupManager::expand_slice("test-a-b.slice")?,
|
||||
Manager::expand_slice("test-a-b.slice")?,
|
||||
PathBuf::from("/test.slice/test-a.slice/test-a-b.slice"),
|
||||
);
|
||||
|
||||
@ -287,13 +265,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn get_cgroups_path_works_with_a_complex_slice() -> Result<()> {
|
||||
let cgroups_path = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from(
|
||||
"test-a-b.slice:docker:foo",
|
||||
))
|
||||
.expect("");
|
||||
let cgroups_path =
|
||||
Manager::destructure_cgroups_path(PathBuf::from("test-a-b.slice:docker:foo"))
|
||||
.expect("");
|
||||
|
||||
assert_eq!(
|
||||
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?,
|
||||
Manager::construct_cgroups_path(cgroups_path)?,
|
||||
PathBuf::from("/test.slice/test-a.slice/test-a-b.slice/docker-foo.scope"),
|
||||
);
|
||||
|
||||
@ -302,13 +279,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn get_cgroups_path_works_with_a_simple_slice() -> Result<()> {
|
||||
let cgroups_path = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from(
|
||||
"machine.slice:libpod:foo",
|
||||
))
|
||||
.expect("");
|
||||
let cgroups_path =
|
||||
Manager::destructure_cgroups_path(PathBuf::from("machine.slice:libpod:foo")).expect("");
|
||||
|
||||
assert_eq!(
|
||||
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?,
|
||||
Manager::construct_cgroups_path(cgroups_path)?,
|
||||
PathBuf::from("/machine.slice/libpod-foo.scope"),
|
||||
);
|
||||
|
||||
@ -318,10 +293,10 @@ mod tests {
|
||||
#[test]
|
||||
fn get_cgroups_path_works_with_scope() -> Result<()> {
|
||||
let cgroups_path =
|
||||
SystemDCGroupManager::destructure_cgroups_path(PathBuf::from(":docker:foo")).expect("");
|
||||
Manager::destructure_cgroups_path(PathBuf::from(":docker:foo")).expect("");
|
||||
|
||||
assert_eq!(
|
||||
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?,
|
||||
Manager::construct_cgroups_path(cgroups_path)?,
|
||||
PathBuf::from("/machine.slice/docker-foo.scope"),
|
||||
);
|
||||
|
3
crates/libcgroups/src/systemd/mod.rs
Normal file
3
crates/libcgroups/src/systemd/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod controller_type;
|
||||
mod dbus;
|
||||
pub mod manager;
|
@ -2,15 +2,13 @@ mod controller;
|
||||
pub mod controller_type;
|
||||
mod cpu;
|
||||
mod cpuset;
|
||||
#[cfg(feature = "cgroupsv2_devices")]
|
||||
pub mod devices;
|
||||
mod freezer;
|
||||
mod hugetlb;
|
||||
mod io;
|
||||
pub mod manager;
|
||||
mod memory;
|
||||
mod pids;
|
||||
pub mod systemd_manager;
|
||||
mod unified;
|
||||
pub mod util;
|
||||
pub use systemd_manager::SystemDCGroupManager;
|
||||
#[cfg(feature = "cgroupsv2_devices")]
|
||||
pub mod devices;
|
||||
|
@ -1,2 +0,0 @@
|
||||
pub mod client;
|
||||
mod systemd_manager;
|
@ -1,7 +1,6 @@
|
||||
pub mod apparmor;
|
||||
pub mod capabilities;
|
||||
pub mod container;
|
||||
pub mod dbus;
|
||||
pub mod hooks;
|
||||
pub mod namespaces;
|
||||
pub mod notify_socket;
|
||||
|
Loading…
Reference in New Issue
Block a user