mirror of
https://github.com/containers/youki
synced 2024-05-18 13:36:14 +02:00
Merge pull request #203 from Furisto/unified
Support unified resource section
This commit is contained in:
commit
e5d351f185
|
@ -212,7 +212,7 @@ where
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub trait PathBufExt {
|
||||
pub(crate) trait PathBufExt {
|
||||
fn join_safely(&self, p: &Path) -> Result<PathBuf>;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ impl Controller for Cpu {
|
|||
log::debug!("Apply Cpu cgroup config");
|
||||
|
||||
if let Some(cpu) = Self::needs_to_handle(linux_resources) {
|
||||
Self::apply(cgroup_root, cpu)?;
|
||||
Self::apply(cgroup_root, cpu).context("failed to apply cpu resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::{fs, path::Path};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use nix::unistd;
|
||||
use oci_spec::{LinuxCpu, LinuxResources};
|
||||
use unistd::Pid;
|
||||
|
@ -31,7 +31,8 @@ impl Controller for CpuSet {
|
|||
log::debug!("Apply CpuSet cgroup config");
|
||||
|
||||
if let Some(cpuset) = Self::needs_to_handle(linux_resources) {
|
||||
Self::apply(cgroup_path, cpuset)?;
|
||||
Self::apply(cgroup_path, cpuset)
|
||||
.context("failed to apply cpuset resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -26,7 +26,7 @@ impl Controller for Freezer {
|
|||
create_dir_all(&cgroup_root)?;
|
||||
|
||||
if let Some(freezer_state) = Self::needs_to_handle(linux_resources) {
|
||||
Self::apply(freezer_state, cgroup_root)?;
|
||||
Self::apply(freezer_state, cgroup_root).context("failed to appyl freezer")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::HashMap, path::Path};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use anyhow::{bail, Context, Result};
|
||||
|
||||
use crate::{
|
||||
common,
|
||||
|
@ -20,7 +20,8 @@ impl Controller for HugeTlb {
|
|||
|
||||
if let Some(hugepage_limits) = Self::needs_to_handle(linux_resources) {
|
||||
for hugetlb in hugepage_limits {
|
||||
Self::apply(cgroup_root, hugetlb)?
|
||||
Self::apply(cgroup_root, hugetlb)
|
||||
.context("failed to apply hugetlb resource restrictions")?
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use super::Controller;
|
||||
use crate::common;
|
||||
|
@ -15,7 +15,8 @@ impl Controller for NetworkClassifier {
|
|||
log::debug!("Apply NetworkClassifier cgroup config");
|
||||
|
||||
if let Some(network) = Self::needs_to_handle(linux_resources) {
|
||||
Self::apply(cgroup_root, network)?;
|
||||
Self::apply(cgroup_root, network)
|
||||
.context("failed to apply network classifier resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use super::Controller;
|
||||
use crate::common;
|
||||
|
@ -15,7 +15,8 @@ impl Controller for NetworkPriority {
|
|||
log::debug!("Apply NetworkPriority cgroup config");
|
||||
|
||||
if let Some(network) = Self::needs_to_handle(linux_resources) {
|
||||
Self::apply(cgroup_root, network)?;
|
||||
Self::apply(cgroup_root, network)
|
||||
.context("failed to apply network priority resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use super::Controller;
|
||||
use crate::{
|
||||
|
@ -21,7 +21,7 @@ impl Controller for Pids {
|
|||
log::debug!("Apply pids cgroup config");
|
||||
|
||||
if let Some(pids) = &linux_resources.pids {
|
||||
Self::apply(cgroup_root, pids)?;
|
||||
Self::apply(cgroup_root, pids).context("failed to apply pids resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{bail, Result};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{
|
||||
|
@ -22,7 +22,7 @@ pub struct Cpu {}
|
|||
impl Controller for Cpu {
|
||||
fn apply(linux_resources: &LinuxResources, path: &Path) -> Result<()> {
|
||||
if let Some(cpu) = &linux_resources.cpu {
|
||||
Self::apply(path, cpu)?;
|
||||
Self::apply(path, cpu).context("failed to apply cpu resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::common;
|
||||
|
@ -14,7 +14,8 @@ pub struct CpuSet {}
|
|||
impl Controller for CpuSet {
|
||||
fn apply(linux_resources: &LinuxResources, cgroup_path: &Path) -> Result<()> {
|
||||
if let Some(cpuset) = &linux_resources.cpu {
|
||||
Self::apply(cgroup_path, cpuset)?;
|
||||
Self::apply(cgroup_path, cpuset)
|
||||
.context("failed to apply cpuset resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{bail, Result};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use std::{
|
||||
fs::OpenOptions,
|
||||
io::{BufRead, BufReader, Read, Seek, SeekFrom, Write},
|
||||
|
@ -19,7 +19,7 @@ pub struct Freezer {}
|
|||
impl Controller for Freezer {
|
||||
fn apply(linux_resources: &LinuxResources, cgroup_path: &Path) -> Result<()> {
|
||||
if let Some(freezer_state) = linux_resources.freezer {
|
||||
Self::apply(freezer_state, cgroup_path)?;
|
||||
Self::apply(freezer_state, cgroup_path).context("failed to apply freezer")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -16,7 +16,8 @@ impl Controller for HugeTlb {
|
|||
log::debug!("Apply hugetlb cgroup v2 config");
|
||||
if let Some(hugepage_limits) = linux_resources.hugepage_limits.as_ref() {
|
||||
for hugetlb in hugepage_limits {
|
||||
Self::apply(cgroup_root, hugetlb)?
|
||||
Self::apply(cgroup_root, hugetlb)
|
||||
.context("failed to apply hugetlb resource restrictions")?
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use anyhow::{bail, Context, Result};
|
||||
|
||||
use crate::{
|
||||
common,
|
||||
|
@ -18,9 +18,9 @@ pub struct Io {}
|
|||
|
||||
impl Controller for Io {
|
||||
fn apply(linux_resource: &LinuxResources, cgroup_root: &Path) -> Result<()> {
|
||||
log::debug!("Apply io cgrup v2 config");
|
||||
log::debug!("Apply io cgroup v2 config");
|
||||
if let Some(io) = &linux_resource.block_io {
|
||||
Self::apply(cgroup_root, io)?;
|
||||
Self::apply(cgroup_root, io).context("failed to apply io resource restrictions")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ use oci_spec::{FreezerState, LinuxResources};
|
|||
|
||||
use super::{
|
||||
controller::Controller, controller_type::ControllerType, cpu::Cpu, cpuset::CpuSet,
|
||||
freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, pids::Pids,
|
||||
freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, pids::Pids, unified::Unified,
|
||||
};
|
||||
use crate::{
|
||||
common::{self, CgroupManager, PathBufExt, CGROUP_PROCS},
|
||||
|
@ -133,6 +133,11 @@ impl CgroupManager for Manager {
|
|||
}
|
||||
}
|
||||
|
||||
Unified::apply(
|
||||
linux_resources,
|
||||
&self.full_path,
|
||||
self.get_available_controllers()?,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{bail, Result};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use std::path::Path;
|
||||
|
||||
use oci_spec::{LinuxMemory, LinuxResources};
|
||||
|
@ -20,7 +20,8 @@ pub struct Memory {}
|
|||
impl Controller for Memory {
|
||||
fn apply(linux_resources: &LinuxResources, cgroup_path: &Path) -> Result<()> {
|
||||
if let Some(memory) = &linux_resources.memory {
|
||||
Self::apply(cgroup_path, memory)?;
|
||||
Self::apply(cgroup_path, memory)
|
||||
.context("failed to apply memory resource restrictions")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -9,5 +9,6 @@ pub mod manager;
|
|||
mod memory;
|
||||
mod pids;
|
||||
pub mod systemd_manager;
|
||||
mod unified;
|
||||
pub mod util;
|
||||
pub use systemd_manager::SystemDCGroupManager;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use crate::{
|
||||
common,
|
||||
|
@ -16,7 +16,7 @@ impl Controller for Pids {
|
|||
fn apply(linux_resource: &LinuxResources, cgroup_root: &std::path::Path) -> Result<()> {
|
||||
log::debug!("Apply pids cgroup v2 config");
|
||||
if let Some(pids) = &linux_resource.pids {
|
||||
Self::apply(cgroup_root, pids)?;
|
||||
Self::apply(cgroup_root, pids).context("failed to apply pids resource restrictions")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -0,0 +1,137 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use oci_spec::LinuxResources;
|
||||
|
||||
use super::controller_type::ControllerType;
|
||||
use crate::common;
|
||||
|
||||
pub struct Unified {}
|
||||
|
||||
impl Unified {
|
||||
pub fn apply(
|
||||
linux_resources: &LinuxResources,
|
||||
cgroup_path: &Path,
|
||||
controllers: Vec<ControllerType>,
|
||||
) -> Result<()> {
|
||||
if let Some(unified) = &linux_resources.unified {
|
||||
log::debug!("Apply unified cgroup config");
|
||||
for (cgroup_file, value) in unified {
|
||||
common::write_cgroup_file_str(cgroup_path.join(cgroup_file), value).map_err(
|
||||
|e| {
|
||||
let (subsystem, _) = cgroup_file
|
||||
.split_once(".")
|
||||
.with_context(|| {
|
||||
format!("failed to split {} with {}", cgroup_file, ".")
|
||||
})
|
||||
.unwrap();
|
||||
let context = if !controllers.iter().any(|c| c.to_string() == subsystem) {
|
||||
format!(
|
||||
"failed to set {} to {}: subsystem {} is not available",
|
||||
cgroup_file, value, subsystem
|
||||
)
|
||||
} else {
|
||||
format!("failed to set {} to {}: {}", cgroup_file, value, e)
|
||||
};
|
||||
|
||||
e.context(context)
|
||||
},
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::array::IntoIter;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::iter::FromIterator;
|
||||
|
||||
use crate::test::{create_temp_dir, set_fixture};
|
||||
use crate::v2::controller_type::ControllerType;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_set_unified() {
|
||||
// arrange
|
||||
let tmp = create_temp_dir("test_set_unified").unwrap();
|
||||
let hugetlb_limit_path = set_fixture(&tmp, "hugetlb.1GB.limit_in_bytes", "").unwrap();
|
||||
let cpu_weight_path = set_fixture(&tmp, "cpu.weight", "").unwrap();
|
||||
|
||||
let resources = LinuxResources {
|
||||
unified: Some(HashMap::<_, _>::from_iter(IntoIter::new([
|
||||
(
|
||||
"hugetlb.1GB.limit_in_bytes".to_owned(),
|
||||
"72348034".to_owned(),
|
||||
),
|
||||
("cpu.weight".to_owned(), "5000".to_owned()),
|
||||
]))),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// act
|
||||
Unified::apply(&resources, &tmp, vec![]).expect("apply unified");
|
||||
|
||||
// assert
|
||||
let hugetlb_limit = fs::read_to_string(hugetlb_limit_path).expect("read hugetlb limit");
|
||||
let cpu_weight = fs::read_to_string(cpu_weight_path).expect("read cpu weight");
|
||||
assert_eq!(hugetlb_limit, "72348034");
|
||||
assert_eq!(cpu_weight, "5000");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_unified_failed_to_write_subsystem_not_enabled() {
|
||||
// arrange
|
||||
let tmp =
|
||||
create_temp_dir("test_set_unified_failed_to_write_subsystem_not_enabled").unwrap();
|
||||
|
||||
let resources = LinuxResources {
|
||||
unified: Some(HashMap::<_, _>::from_iter(IntoIter::new([
|
||||
(
|
||||
"hugetlb.1GB.limit_in_bytes".to_owned(),
|
||||
"72348034".to_owned(),
|
||||
),
|
||||
("cpu.weight".to_owned(), "5000".to_owned()),
|
||||
]))),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// act
|
||||
let result = Unified::apply(&resources, &tmp, vec![]);
|
||||
|
||||
// assert
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_unified_failed_to_write_subsystem_enabled() {
|
||||
// arrange
|
||||
let tmp = create_temp_dir("test_set_unified_failed_to_write_subsystem_enabled").unwrap();
|
||||
|
||||
let resources = LinuxResources {
|
||||
unified: Some(HashMap::<_, _>::from_iter(IntoIter::new([
|
||||
(
|
||||
"hugetlb.1GB.limit_in_bytes".to_owned(),
|
||||
"72348034".to_owned(),
|
||||
),
|
||||
("cpu.weight".to_owned(), "5000".to_owned()),
|
||||
]))),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// act
|
||||
let result = Unified::apply(
|
||||
&resources,
|
||||
&tmp,
|
||||
vec![ControllerType::HugeTlb, ControllerType::Cpu],
|
||||
);
|
||||
|
||||
// assert
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue