1
0
Fork 0
mirror of https://github.com/containers/youki synced 2024-06-10 00:36:16 +02:00

Merge pull request #546 from knight42/feat/update-resources

feat: add --resource option to update subcommand
This commit is contained in:
utam0k 2021-12-19 10:44:51 +09:00 committed by GitHub
commit 468e9fbd6b
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -1,4 +1,5 @@
use clap::Parser;
use std::path::PathBuf;
/// Update running container resource constraints
#[derive(Parser, Debug)]
@ -6,6 +7,11 @@ pub struct Update {
#[clap(forbid_empty_values = true, required = true)]
pub container_id: String,
/// Read the new resource limits from the given json file. Use - to read from stdin.
/// If this option is used, all other options are ignored.
#[clap(short, long)]
pub resources: Option<PathBuf>,
/// Set the maximum number of processes allowed in the container
#[clap(long)]
pub pids_limit: Option<i64>,

View File

@ -1,19 +1,29 @@
use std::fs;
use std::io;
use std::path::PathBuf;
use crate::commands::create_cgroup_manager;
use anyhow::Result;
use libcgroups::{self, common::ControllerOpt};
use liboci_cli::Update;
use oci_spec::runtime::{LinuxPids, LinuxResources};
use oci_spec::runtime::{LinuxPidsBuilder, LinuxResources, LinuxResourcesBuilder};
pub fn update(args: Update, root_path: PathBuf) -> Result<()> {
let cmanager = create_cgroup_manager(root_path, &args.container_id)?;
let mut linux_res = LinuxResources::default();
if let Some(new_pids_limit) = args.pids_limit {
let mut pids = LinuxPids::default();
pids.set_limit(new_pids_limit);
linux_res.set_pids(Some(pids));
let linux_res: LinuxResources;
if let Some(resources_path) = args.resources {
linux_res = if resources_path.to_string_lossy() == "-" {
serde_json::from_reader(io::stdin())?
} else {
serde_json::from_reader(fs::File::open(resources_path)?)?
};
} else {
let mut builder = LinuxResourcesBuilder::default();
if let Some(new_pids_limit) = args.pids_limit {
builder = builder.pids(LinuxPidsBuilder::default().limit(new_pids_limit).build()?);
}
linux_res = builder.build()?;
}
cmanager.apply(&ControllerOpt {