Skip to content

Commit 545b0e7

Browse files
jokemanfiremxpv
authored andcommitted
Add info for get runtimeinfo
This interface is already in goshim. Use containerd-shim-runc-v2 --info. Signed-off-by: jokemanfire <[email protected]>
1 parent 1f1e38d commit 545b0e7

File tree

8 files changed

+137
-6
lines changed

8 files changed

+137
-6
lines changed

crates/runc-shim/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ serde.workspace = true
3636
serde_json.workspace = true
3737
time.workspace = true
3838
uuid.workspace = true
39-
39+
tempfile.workspace = true
4040
# Async dependencies
4141
async-trait.workspace = true
4242
tokio = { workspace = true, features = ["full"] }

crates/runc-shim/src/main.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
limitations under the License.
1515
*/
1616

17-
use std::env;
17+
use std::{env, io::Write};
1818

19-
use containerd_shim::{asynchronous::run, parse};
19+
use containerd_shim::{
20+
asynchronous::run,
21+
parse,
22+
protos::protobuf::{well_known_types::any::Any, Message},
23+
run_info,
24+
};
2025

2126
mod cgroup_memory;
2227
mod common;
@@ -47,6 +52,30 @@ fn parse_version() {
4752

4853
std::process::exit(0);
4954
}
55+
if flags.info {
56+
let r = run_info();
57+
match r {
58+
Ok(rinfo) => {
59+
let mut info = Any::new();
60+
info.type_url = "io.containerd.runc.v2.Info".to_string();
61+
info.value = match rinfo.write_to_bytes() {
62+
Ok(bytes) => bytes,
63+
Err(e) => {
64+
eprintln!("Failed to write runtime info to bytes: {}", e);
65+
std::process::exit(1);
66+
}
67+
};
68+
std::io::stdout()
69+
.write_all(info.write_to_bytes().unwrap().as_slice())
70+
.expect("Failed to write to stdout");
71+
}
72+
Err(_) => {
73+
eprintln!("Failed to get runtime info");
74+
std::process::exit(1);
75+
}
76+
}
77+
std::process::exit(0);
78+
}
5079
}
5180

5281
#[tokio::main]

crates/shim-protos/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn main() {
3232
"vendor/github.com/containerd/containerd/protobuf/plugin/fieldpath.proto",
3333
"vendor/github.com/containerd/containerd/api/types/mount.proto",
3434
"vendor/github.com/containerd/containerd/api/types/task/task.proto",
35+
"vendor/github.com/containerd/containerd/api/types/introspection.proto",
3536
#[cfg(feature = "sandbox")]
3637
"vendor/github.com/containerd/containerd/api/types/platform.proto",
3738
],

crates/shim-protos/src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub mod fieldpath {
3434
include!(concat!(env!("OUT_DIR"), "/types/fieldpath.rs"));
3535
}
3636

37+
pub mod introspection {
38+
include!(concat!(env!("OUT_DIR"), "/types/introspection.rs"));
39+
}
3740
#[cfg(feature = "sandbox")]
3841
pub mod platform {
3942
include!(concat!(env!("OUT_DIR"), "/types/platform.rs"));

crates/shim-protos/vendor/github.com/containerd/containerd/api/types/introspection.proto

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/shim/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ name = "windows-log-reader"
3434
path = "examples/windows_log_reader.rs"
3535

3636
[dependencies]
37+
which = "7.0.1"
3738
containerd-shim-protos = { path = "../shim-protos", version = "0.7.2" }
3839
go-flag = "0.1.0"
3940
lazy_static = "1.4.0"

crates/shim/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct Flags {
4141
pub action: String,
4242
/// Version of the shim.
4343
pub version: bool,
44+
/// get the option protobuf from stdin, print the shim info protobuf to stdout, and exit
45+
pub info: bool,
4446
}
4547

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

crates/shim/src/asynchronous/mod.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
use std::{
1818
convert::TryFrom,
1919
env,
20+
io::Read,
2021
os::unix::{fs::FileTypeExt, net::UnixListener},
2122
path::Path,
22-
process,
23-
process::{Command, Stdio},
23+
process::{self, Command, Stdio},
2424
sync::{
2525
atomic::{AtomicBool, Ordering},
2626
Arc,
@@ -31,9 +31,11 @@ use async_trait::async_trait;
3131
use command_fds::{CommandFdExt, FdMapping};
3232
use containerd_shim_protos::{
3333
api::DeleteResponse,
34-
protobuf::Message,
34+
protobuf::{well_known_types::any::Any, Message, MessageField},
35+
shim::oci::Options,
3536
shim_async::{create_task, Client, Task},
3637
ttrpc::r#async::Server,
38+
types::introspection::{self, RuntimeInfo},
3739
};
3840
use futures::StreamExt;
3941
use libc::{SIGCHLD, SIGINT, SIGPIPE, SIGTERM};
@@ -46,8 +48,12 @@ use nix::{
4648
},
4749
unistd::Pid,
4850
};
51+
use oci_spec::runtime::Features;
4952
use signal_hook_tokio::Signals;
5053
use tokio::{io::AsyncWriteExt, sync::Notify};
54+
use which::which;
55+
56+
const DEFAULT_BINARY_NAME: &str = "runc";
5157

5258
use crate::{
5359
args,
@@ -109,6 +115,48 @@ where
109115
process::exit(1);
110116
}
111117
}
118+
/// get runtime info
119+
pub fn run_info() -> Result<RuntimeInfo> {
120+
let mut info = introspection::RuntimeInfo {
121+
name: "containerd-shim-runc-v2-rs".to_string(),
122+
version: MessageField::some(introspection::RuntimeVersion {
123+
version: env!("CARGO_PKG_VERSION").to_string(),
124+
revision: String::default(),
125+
..Default::default()
126+
}),
127+
..Default::default()
128+
};
129+
let mut binary_name = DEFAULT_BINARY_NAME.to_string();
130+
let mut data: Vec<u8> = Vec::new();
131+
std::io::stdin()
132+
.read_to_end(&mut data)
133+
.map_err(io_error!(e, "read stdin"))?;
134+
// get BinaryName from stdin
135+
if !data.is_empty() {
136+
let opts =
137+
Any::parse_from_bytes(&data).and_then(|any| Options::parse_from_bytes(&any.value))?;
138+
if !opts.binary_name().is_empty() {
139+
binary_name = opts.binary_name().to_string();
140+
}
141+
}
142+
let binary_path = which(binary_name).unwrap();
143+
144+
// get features
145+
let output = Command::new(binary_path).arg("features").output().unwrap();
146+
147+
let features: Features = serde_json::from_str(&String::from_utf8_lossy(&output.stdout))?;
148+
149+
// set features
150+
let features_any = Any {
151+
type_url: "types.containerd.io/opencontainers/runtime-spec/1/features/Features".to_string(),
152+
// features to json
153+
value: serde_json::to_vec(&features)?,
154+
..Default::default()
155+
};
156+
info.features = MessageField::some(features_any);
157+
158+
Ok(info)
159+
}
112160

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

0 commit comments

Comments
 (0)