Skip to content

Commit

Permalink
Initial implementation of resource-control via systemd and dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrodshn committed Jul 2, 2021
1 parent d68e49e commit 8aaee1c
Show file tree
Hide file tree
Showing 6 changed files with 2,569 additions and 23 deletions.
7 changes: 2 additions & 5 deletions src/cgroups/v1/pids.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path};
use std::path::Path;

use anyhow::Result;

Expand All @@ -10,10 +10,7 @@ pub struct Pids {}
impl Controller for Pids {
type Resource = LinuxPids;

fn apply(
linux_resources: &LinuxResources,
cgroup_root: &Path,
) -> Result<()> {
fn apply(linux_resources: &LinuxResources, cgroup_root: &Path) -> Result<()> {
log::debug!("Apply pids cgroup config");

if let Some(pids) = &linux_resources.pids {
Expand Down
2 changes: 1 addition & 1 deletion src/cgroups/v1/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, path::PathBuf};
use anyhow::{anyhow, Result};
use procfs::process::Process;

use super::{ControllerType, controller_type::CONTROLLERS};
use super::{controller_type::CONTROLLERS, ControllerType};

pub fn list_subsystem_mount_points() -> Result<HashMap<ControllerType, PathBuf>> {
let mut mount_paths = HashMap::with_capacity(CONTROLLERS.len());
Expand Down
14 changes: 8 additions & 6 deletions src/cgroups/v2/systemd_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ 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 = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from(
"test-a-b.slice:docker:foo",
))
.expect("");

assert_eq!(
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?,
Expand All @@ -275,9 +276,10 @@ 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 = SystemDCGroupManager::destructure_cgroups_path(PathBuf::from(
"machine.slice:libpod:foo",
))
.expect("");

assert_eq!(
SystemDCGroupManager::construct_cgroups_path(cgroups_path)?,
Expand Down
92 changes: 83 additions & 9 deletions src/dbus/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::dbus::systemd_manager::OrgFreedesktopSystemd1Manager;
use anyhow::Result;
use dbus::arg::{RefArg, Variant};
use dbus::blocking::Connection;
use oci_spec::LinuxResources;
use std::time::Duration;
use std::vec::Vec;

Expand All @@ -15,19 +18,90 @@ impl Client {
Ok(Client { conn })
}

/// start_unit starts a specific unit under systemd. See https://www.freedesktop.org/wiki/Software/systemd/dbus
/// start_transient_unit is a higher level API for starting a unit
/// for a specific container under systemd.
/// See https://www.freedesktop.org/wiki/Software/systemd/dbus
/// for more details.
pub fn start_unit(&self, unit_name: &str, _properties: Vec<&str>) -> Result<()> {
pub fn start_transient_unit_for_container(
&self,
container_name: &str,
unit_name: &str,
linux_resources: LinuxResources,
) -> Result<()> {
let proxy = self.conn.with_proxy(
"org.freedesktop.systemd1.Manager",
"/",
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
Duration::from_millis(5000),
);
let (_job_id,): (i32,) = proxy.method_call(
"org.freedesktop.systemd1.Manager",
"StartTransientUnit",
(unit_name, "replace"),
)?;

let mut properties = Vec::<Property>::new();
properties.push(
PropDescription::new(format!("youki container {}", container_name).as_str()).into(),
);

let slice = "machine.slice";
// if we create a slice, the parent is defined via a Wants=
// otherwise, we use Slice=
if unit_name.ends_with("slice") {
properties.push(PropWants::new(slice).into());
} else {
properties.push(PropSlice::new(slice).into());
}

proxy.start_transient_unit(unit_name, "replace", properties, vec![])?;
Ok(())
}
}

type Property = (&'static str, Variant<Box<dyn RefArg>>);

struct PropDescription(Property);

impl PropDescription {
fn new(desc: &str) -> Self {
return PropDescription((
"Description",
Variant(Box::new(desc.to_string()) as Box<dyn RefArg>),
));
}
}

impl From<PropDescription> for Property {
fn from(p: PropDescription) -> Self {
p.0
}
}

struct PropWants(Property);

impl PropWants {
fn new(unit: &str) -> Self {
return PropWants((
"Wants",
Variant(Box::new(unit.to_string()) as Box<dyn RefArg>),
));
}
}

impl From<PropWants> for Property {
fn from(p: PropWants) -> Self {
p.0
}
}

struct PropSlice(Property);

impl PropSlice {
fn new(slice: &str) -> Self {
return PropSlice((
"Slice",
Variant(Box::new(slice.to_string()) as Box<dyn RefArg>),
));
}
}

impl From<PropSlice> for Property {
fn from(p: PropSlice) -> Self {
p.0
}
}
4 changes: 2 additions & 2 deletions src/dbus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod client;
pub use client::Client;
pub mod client;
mod systemd_manager;
Loading

0 comments on commit 8aaee1c

Please sign in to comment.