Skip to content

Commit

Permalink
feat: add --resource option to update subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>
  • Loading branch information
knight42 committed Dec 18, 2021
1 parent 7d63d2a commit 21eaf4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions crates/liboci-cli/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use clap::Parser;
use std::path::PathBuf;

/// Update running container resource constraints
#[derive(Parser, Debug)]
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>,
Expand Down
22 changes: 16 additions & 6 deletions crates/youki/src/commands/update.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 21eaf4e

Please sign in to comment.