Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add info for get runtimeinfo #370

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/runc-shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ serde.workspace = true
serde_json.workspace = true
time.workspace = true
uuid.workspace = true

tempfile.workspace = true
# Async dependencies
async-trait.workspace = true
tokio = { workspace = true, features = ["full"] }
Expand Down
33 changes: 31 additions & 2 deletions crates/runc-shim/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
limitations under the License.
*/

use std::env;
use std::{env, io::Write};

use containerd_shim::{asynchronous::run, parse};
use containerd_shim::{
asynchronous::run,
parse,
protos::protobuf::{well_known_types::any::Any, Message},
run_info,
};

mod cgroup_memory;
mod common;
Expand Down Expand Up @@ -47,6 +52,30 @@ fn parse_version() {

std::process::exit(0);
}
if flags.info {
let r = run_info();
match r {
Ok(rinfo) => {
let mut info = Any::new();
info.type_url = "io.containerd.runc.v2.Info".to_string();
info.value = match rinfo.write_to_bytes() {
Ok(bytes) => bytes,
Err(e) => {
eprintln!("Failed to write runtime info to bytes: {}", e);
std::process::exit(1);
}
};
std::io::stdout()
.write_all(info.write_to_bytes().unwrap().as_slice())
.expect("Failed to write to stdout");
}
Err(_) => {
eprintln!("Failed to get runtime info");
std::process::exit(1);
}
}
std::process::exit(0);
}
}

#[tokio::main]
Expand Down
1 change: 1 addition & 0 deletions crates/shim-protos/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn main() {
"vendor/github.com/containerd/containerd/protobuf/plugin/fieldpath.proto",
"vendor/github.com/containerd/containerd/api/types/mount.proto",
"vendor/github.com/containerd/containerd/api/types/task/task.proto",
"vendor/github.com/containerd/containerd/api/types/introspection.proto",
#[cfg(feature = "sandbox")]
"vendor/github.com/containerd/containerd/api/types/platform.proto",
],
Expand Down
3 changes: 3 additions & 0 deletions crates/shim-protos/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub mod fieldpath {
include!(concat!(env!("OUT_DIR"), "/types/fieldpath.rs"));
}

pub mod introspection {
include!(concat!(env!("OUT_DIR"), "/types/introspection.rs"));
}
#[cfg(feature = "sandbox")]
pub mod platform {
include!(concat!(env!("OUT_DIR"), "/types/platform.rs"));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ name = "windows-log-reader"
path = "examples/windows_log_reader.rs"

[dependencies]
which = "7.0.1"
containerd-shim-protos = { path = "../shim-protos", version = "0.7.2" }
go-flag = "0.1.0"
lazy_static = "1.4.0"
Expand Down
3 changes: 3 additions & 0 deletions crates/shim/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct Flags {
pub action: String,
/// Version of the shim.
pub version: bool,
/// get the option protobuf from stdin, print the shim info protobuf to stdout, and exit
pub info: bool,
}

/// Parses command line arguments passed to the shim.
Expand All @@ -57,6 +59,7 @@ pub fn parse<S: AsRef<OsStr>>(args: &[S]) -> Result<Flags> {
f.add_flag("bundle", &mut flags.bundle);
f.add_flag("address", &mut flags.address);
f.add_flag("publish-binary", &mut flags.publish_binary);
f.add_flag("info", &mut flags.info);
})
.map_err(|e| Error::InvalidArgument(e.to_string()))?;

Expand Down
54 changes: 51 additions & 3 deletions crates/shim/src/asynchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
use std::{
convert::TryFrom,
env,
io::Read,
os::unix::{fs::FileTypeExt, net::UnixListener},
path::Path,
process,
process::{Command, Stdio},
process::{self, Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
Expand All @@ -31,9 +31,11 @@ use async_trait::async_trait;
use command_fds::{CommandFdExt, FdMapping};
use containerd_shim_protos::{
api::DeleteResponse,
protobuf::Message,
protobuf::{well_known_types::any::Any, Message, MessageField},
shim::oci::Options,
shim_async::{create_task, Client, Task},
ttrpc::r#async::Server,
types::introspection::{self, RuntimeInfo},
};
use futures::StreamExt;
use libc::{SIGCHLD, SIGINT, SIGPIPE, SIGTERM};
Expand All @@ -46,8 +48,12 @@ use nix::{
},
unistd::Pid,
};
use oci_spec::runtime::Features;
use signal_hook_tokio::Signals;
use tokio::{io::AsyncWriteExt, sync::Notify};
use which::which;

const DEFAULT_BINARY_NAME: &str = "runc";

use crate::{
args,
Expand Down Expand Up @@ -109,6 +115,48 @@ where
process::exit(1);
}
}
/// get runtime info
pub fn run_info() -> Result<RuntimeInfo> {
let mut info = introspection::RuntimeInfo {
name: "containerd-shim-runc-v2-rs".to_string(),
version: MessageField::some(introspection::RuntimeVersion {
version: env!("CARGO_PKG_VERSION").to_string(),
revision: String::default(),
..Default::default()
}),
..Default::default()
};
let mut binary_name = DEFAULT_BINARY_NAME.to_string();
let mut data: Vec<u8> = Vec::new();
std::io::stdin()
.read_to_end(&mut data)
.map_err(io_error!(e, "read stdin"))?;
// get BinaryName from stdin
if !data.is_empty() {
let opts =
Any::parse_from_bytes(&data).and_then(|any| Options::parse_from_bytes(&any.value))?;
if !opts.binary_name().is_empty() {
binary_name = opts.binary_name().to_string();
}
}
let binary_path = which(binary_name).unwrap();

// get features
let output = Command::new(binary_path).arg("features").output().unwrap();

let features: Features = serde_json::from_str(&String::from_utf8_lossy(&output.stdout))?;

// set features
let features_any = Any {
type_url: "types.containerd.io/opencontainers/runtime-spec/1/features/Features".to_string(),
// features to json
value: serde_json::to_vec(&features)?,
..Default::default()
};
info.features = MessageField::some(features_any);

Ok(info)
}

#[cfg_attr(feature = "tracing", tracing::instrument(level = "info"))]
async fn bootstrap<T>(runtime_id: &str, opts: Option<Config>) -> Result<()>
Expand Down
Loading