Skip to content

Commit

Permalink
feat(runc-shim): add a version flag
Browse files Browse the repository at this point in the history
Signed-off-by: jiaxiao zhou <[email protected]>
  • Loading branch information
Mossaka committed Aug 10, 2023
1 parent ad83804 commit 60dc92b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
19 changes: 19 additions & 0 deletions crates/runc-shim/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::process::Command;

fn main() {
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.arg("--abbrev=9")
.output()
{
Ok(output) if output.status.success() => output,
_ => return,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace();
let mut next = || parts.next().unwrap();
println!("cargo:rustc-env=CARGO_GIT_HASH={}", next());
}
18 changes: 17 additions & 1 deletion crates/runc-shim/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
limitations under the License.
*/

use containerd_shim::asynchronous::run;
use std::env;

use containerd_shim::{asynchronous::run, parse};

mod common;
mod console;
Expand All @@ -27,7 +29,21 @@ mod task;

use service::Service;

fn parse_version() {
let os_args: Vec<_> = env::args_os().collect();
let flags = parse(&os_args[1..]).unwrap();
if flags.version {
println!("{}:", os_args[0].to_string_lossy());
println!(" Version: {}", env!("CARGO_PKG_VERSION"));
println!(" Revision: {}", env!("CARGO_GIT_HASH"));
println!();

std::process::exit(0);
}
}

#[tokio::main]
async fn main() {
parse_version();
run::<Service>("io.containerd.runc.v2-rs", None).await;
}
17 changes: 3 additions & 14 deletions crates/shim/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ pub struct Flags {
/// Shim action (start / delete).
/// See <https://github.com/containerd/containerd/blob/master/runtime/v2/shim/shim.go#L191>
pub action: String,
/// Version of the shim.
pub version: bool,
}

/// Parses command line arguments passed to the shim.
/// This func replicates https://github.com/containerd/containerd/blob/master/runtime/v2/shim/shim.go#L110
pub fn parse<S: AsRef<OsStr>>(args: &[S]) -> Result<Flags> {
let mut flags = Flags::default();

let args: Vec<String> = go_flag::parse_args(args, |f| {
f.add_flag("debug", &mut flags.debug);
f.add_flag("v", &mut flags.version);
f.add_flag("namespace", &mut flags.namespace);
f.add_flag("id", &mut flags.id);
f.add_flag("socket", &mut flags.socket);
Expand All @@ -61,12 +63,6 @@ pub fn parse<S: AsRef<OsStr>>(args: &[S]) -> Result<Flags> {
flags.action = action.into();
}

if flags.namespace.is_empty() {
return Err(Error::InvalidArgument(String::from(
"Shim namespace cannot be empty",
)));
}

Ok(flags)
}

Expand Down Expand Up @@ -125,11 +121,4 @@ mod tests {
assert_eq!(flags.action, "start");
assert_eq!(flags.id, "");
}

#[test]
fn no_namespace() {
let empty: [String; 0] = [];
let result = parse(&empty).err();
assert!(result.is_some())
}
}
2 changes: 1 addition & 1 deletion crates/shim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub use crate::synchronous::*;
pub mod error;

mod args;
pub use args::Flags;
pub use args::{parse, Flags};
#[cfg(feature = "async")]
pub mod asynchronous;
pub mod cgroup;
Expand Down
44 changes: 44 additions & 0 deletions crates/shim/src/synchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ where
let os_args: Vec<_> = env::args_os().collect();
let flags = args::parse(&os_args[1..])?;

if flags.namespace.is_empty() {
return Err(Error::InvalidArgument(String::from(
"Shim namespace cannot be empty",
)));
}

let ttrpc_address = env::var(TTRPC_ADDRESS)?;

// Create shim instance
Expand Down Expand Up @@ -593,4 +599,42 @@ mod tests {
panic!("{:?}", err);
}
}

struct Nop {}

struct NopTask {}
impl Task for NopTask {}

impl Shim for Nop {
type T = NopTask;

fn new(_runtime_id: &str, _args: &Flags, _config: &mut Config) -> Self {
Nop {}
}

fn start_shim(&mut self, _opts: StartOpts) -> Result<String> {
Ok("".to_string())
}

fn delete_shim(&mut self) -> Result<DeleteResponse> {
Ok(DeleteResponse::default())
}

fn wait(&mut self) {}

fn create_task_service(&self, _publisher: RemotePublisher) -> Self::T {
NopTask {}
}
}

#[test]
fn no_namespace() {
let runtime_id = "test";
let res = bootstrap::<Nop>(runtime_id, None);
assert!(res.is_err());
assert!(res
.unwrap_err()
.to_string()
.contains("Shim namespace cannot be empty"));
}
}

0 comments on commit 60dc92b

Please sign in to comment.