diff --git a/Cargo.lock b/Cargo.lock index eec0a7a1b..ec51c68a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -453,7 +453,7 @@ dependencies = [ [[package]] name = "containerd-shim" version = "0.3.0" -source = "git+https://github.com/containerd/rust-extensions?rev=8500b4c665e0d0911b9546c00bd0bd4670cf5533#8500b4c665e0d0911b9546c00bd0bd4670cf5533" +source = "git+https://github.com/containerd/rust-extensions?rev=7f7e3117a6ecb49e5e3b48b4f457a4914d2f2b93#7f7e3117a6ecb49e5e3b48b4f457a4914d2f2b93" dependencies = [ "cgroups-rs", "command-fds", @@ -481,7 +481,7 @@ dependencies = [ [[package]] name = "containerd-shim-protos" version = "0.3.0" -source = "git+https://github.com/containerd/rust-extensions?rev=8500b4c665e0d0911b9546c00bd0bd4670cf5533#8500b4c665e0d0911b9546c00bd0bd4670cf5533" +source = "git+https://github.com/containerd/rust-extensions?rev=7f7e3117a6ecb49e5e3b48b4f457a4914d2f2b93#7f7e3117a6ecb49e5e3b48b4f457a4914d2f2b93" dependencies = [ "protobuf 3.2.0", "ttrpc", diff --git a/Cargo.toml b/Cargo.toml index afed2967a..48e2eae8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ serde_json = "1.0" env_logger = "0.10" log = "0.4" tar = "0.4" -containerd-shim = {git = "https://github.com/containerd/rust-extensions", rev = "8500b4c665e0d0911b9546c00bd0bd4670cf5533" } +containerd-shim = {git = "https://github.com/containerd/rust-extensions", rev = "7f7e3117a6ecb49e5e3b48b4f457a4914d2f2b93" } ttrpc = "0.8.0" chrono = { version = "0.4", default-features = false, features = ["clock"] } nix = "0.26" diff --git a/crates/containerd-shim-wasm/protos/sandbox.proto b/crates/containerd-shim-wasm/protos/sandbox.proto index e0ff88ca7..733ee2cc1 100644 --- a/crates/containerd-shim-wasm/protos/sandbox.proto +++ b/crates/containerd-shim-wasm/protos/sandbox.proto @@ -14,6 +14,7 @@ message CreateRequest { string id = 2; string ttrpc_address = 3; string working_directory = 4; + string containerd_address = 5; } message CreateResponse { diff --git a/crates/containerd-shim-wasm/src/sandbox/instance.rs b/crates/containerd-shim-wasm/src/sandbox/instance.rs index 3cc2941df..ce0af303b 100644 --- a/crates/containerd-shim-wasm/src/sandbox/instance.rs +++ b/crates/containerd-shim-wasm/src/sandbox/instance.rs @@ -32,13 +32,15 @@ where bundle: Option, /// Namespace for containerd namespace: String, + // /// GRPC address back to main containerd + containerd_address: String, } impl InstanceConfig where E: Send + Sync + Clone, { - pub fn new(engine: E, namespace: String) -> Self { + pub fn new(engine: E, namespace: String, containerd_address: String) -> Self { Self { engine, namespace, @@ -46,6 +48,7 @@ where stdout: None, stderr: None, bundle: None, + containerd_address, } } @@ -102,6 +105,11 @@ where pub fn get_namespace(&self) -> String { self.namespace.clone() } + + /// get the containerd address for the instance + pub fn get_containerd_address(&self) -> String { + self.containerd_address.clone() + } } /// Represents a WASI module(s). diff --git a/crates/containerd-shim-wasm/src/sandbox/manager.rs b/crates/containerd-shim-wasm/src/sandbox/manager.rs index 0de512fb6..a18e967da 100644 --- a/crates/containerd-shim-wasm/src/sandbox/manager.rs +++ b/crates/containerd-shim-wasm/src/sandbox/manager.rs @@ -23,6 +23,7 @@ use containerd_shim::{ }; use nix::sched::{setns, unshare, CloneFlags}; use oci_spec::runtime; +use shim::Flags; use ttrpc::context; use super::error::Error; @@ -38,7 +39,13 @@ where { type Instance: Instance; - fn new(namespace: String, id: String, engine: E, publisher: RemotePublisher) -> Self; + fn new( + namespace: String, + containerd_address: String, + id: String, + engine: E, + publisher: RemotePublisher, + ) -> Self; } /// Service is a manager service which can be used to manage multiple instances of a sandbox in-process. @@ -88,6 +95,7 @@ where let sb = T::new( req.namespace.clone(), + req.containerd_address.clone(), req.id.clone(), self.engine.clone(), publisher, @@ -189,10 +197,10 @@ impl Task for Shim {} impl shim::Shim for Shim { type T = Self; - fn new(_runtime_id: &str, id: &str, namespace: &str, _config: &mut shim::Config) -> Self { + fn new(_runtime_id: &str, args: &Flags, _config: &mut shim::Config) -> Self { Shim { - id: id.to_string(), - namespace: namespace.to_string(), + id: args.id.to_string(), + namespace: args.namespace.to_string(), } } diff --git a/crates/containerd-shim-wasm/src/sandbox/shim.rs b/crates/containerd-shim-wasm/src/sandbox/shim.rs index 7e0a0ec87..196658538 100644 --- a/crates/containerd-shim-wasm/src/sandbox/shim.rs +++ b/crates/containerd-shim-wasm/src/sandbox/shim.rs @@ -37,6 +37,7 @@ use nix::sched::{setns, unshare, CloneFlags}; use nix::sys::stat::Mode; use nix::unistd::mkdir; use oci_spec::runtime; +use shim::Flags; use ttrpc::context::Context; type InstanceDataStatus = (Mutex)>>, Condvar); @@ -339,6 +340,7 @@ where events: Arc>, exit: Arc, namespace: String, + containerd_address: String, } #[cfg(test)] @@ -424,6 +426,7 @@ mod localtests { tx, Arc::new(ExitSignal::default()), "test_namespace".into(), + "/test/address".into(), )); let mut _wrapped = LocalWithDescrutor::new(local.clone()); @@ -454,6 +457,7 @@ mod localtests { etx, exit_signal, "test_namespace".into(), + "/test/address".into(), )); let mut _wrapped = LocalWithDescrutor::new(local.clone()); @@ -622,6 +626,7 @@ mod localtests { etx, exit_signal, "test_namespace".into(), + "/test/address".into(), )); let mut _wrapped = LocalWithDescrutor::new(local.clone()); @@ -735,6 +740,7 @@ where tx: Sender<(String, Box)>, exit: Arc, namespace: String, + containerd_address: String, ) -> Self where T: Instance + Sync + Send, @@ -747,11 +753,16 @@ where events: Arc::new(Mutex::new(tx)), exit, namespace, + containerd_address, } } fn new_base(&self, id: String) -> InstanceData { - let cfg = InstanceConfig::new(self.engine.clone(), self.namespace.clone()); + let cfg = InstanceConfig::new( + self.engine.clone(), + self.namespace.clone(), + self.containerd_address.clone(), + ); InstanceData { instance: None, base: Some(Nop::new(id, None)), @@ -942,7 +953,11 @@ where } let engine = self.engine.clone(); - let mut builder = InstanceConfig::new(engine, self.namespace.clone()); + let mut builder = InstanceConfig::new( + engine, + self.namespace.clone(), + self.containerd_address.clone(), + ); builder .set_stdin(req.stdin().to_string()) .set_stdout(req.stdout().to_string()) @@ -1200,7 +1215,13 @@ where E: Sync + Send + Clone, { type Instance = T; - fn new(namespace: String, _id: String, engine: E, publisher: RemotePublisher) -> Self { + fn new( + namespace: String, + containerd_address: String, + _id: String, + engine: E, + publisher: RemotePublisher, + ) -> Self { let (tx, rx) = channel::<(String, Box)>(); forward_events(namespace.clone(), publisher, rx); Local::::new( @@ -1208,6 +1229,7 @@ where tx.clone(), Arc::new(ExitSignal::default()), namespace, + containerd_address, ) } } @@ -1344,6 +1366,7 @@ where { pub engine: E, namespace: String, + containerd_address: String, phantom: std::marker::PhantomData, exit: Arc, _id: String, @@ -1356,13 +1379,14 @@ where { type T = Local; - fn new(_runtime_id: &str, id: &str, namespace: &str, _config: &mut shim::Config) -> Self { + fn new(_runtime_id: &str, args: &Flags, _config: &mut shim::Config) -> Self { Cli { engine: I::new_engine().unwrap(), phantom: std::marker::PhantomData, - namespace: namespace.to_string(), + namespace: args.namespace.to_string(), + containerd_address: args.address.clone(), exit: Arc::new(ExitSignal::default()), - _id: id.to_string(), + _id: args.id.to_string(), } } @@ -1484,6 +1508,7 @@ where tx.clone(), self.exit.clone(), self.namespace.clone(), + self.containerd_address.clone(), ) } diff --git a/crates/containerd-shim-wasm/src/services/sandbox.rs b/crates/containerd-shim-wasm/src/services/sandbox.rs index 20dd4d653..b6fe980a0 100644 --- a/crates/containerd-shim-wasm/src/services/sandbox.rs +++ b/crates/containerd-shim-wasm/src/services/sandbox.rs @@ -37,6 +37,8 @@ pub struct CreateRequest { pub ttrpc_address: ::std::string::String, // @@protoc_insertion_point(field:runwasi.services.sandbox.v1.CreateRequest.working_directory) pub working_directory: ::std::string::String, + // @@protoc_insertion_point(field:runwasi.services.sandbox.v1.CreateRequest.containerd_address) + pub containerd_address: ::std::string::String, // special fields // @@protoc_insertion_point(special_field:runwasi.services.sandbox.v1.CreateRequest.special_fields) pub special_fields: ::protobuf::SpecialFields, @@ -54,7 +56,7 @@ impl CreateRequest { } fn generated_message_descriptor_data() -> ::protobuf::reflect::GeneratedMessageDescriptorData { - let mut fields = ::std::vec::Vec::with_capacity(4); + let mut fields = ::std::vec::Vec::with_capacity(5); let mut oneofs = ::std::vec::Vec::with_capacity(0); fields.push(::protobuf::reflect::rt::v2::make_simpler_field_accessor::<_, _>( "namespace", @@ -76,6 +78,11 @@ impl CreateRequest { |m: &CreateRequest| { &m.working_directory }, |m: &mut CreateRequest| { &mut m.working_directory }, )); + fields.push(::protobuf::reflect::rt::v2::make_simpler_field_accessor::<_, _>( + "containerd_address", + |m: &CreateRequest| { &m.containerd_address }, + |m: &mut CreateRequest| { &mut m.containerd_address }, + )); ::protobuf::reflect::GeneratedMessageDescriptorData::new_2::( "CreateRequest", fields, @@ -106,6 +113,9 @@ impl ::protobuf::Message for CreateRequest { 34 => { self.working_directory = is.read_string()?; }, + 42 => { + self.containerd_address = is.read_string()?; + }, tag => { ::protobuf::rt::read_unknown_or_skip_group(tag, is, self.special_fields.mut_unknown_fields())?; }, @@ -130,6 +140,9 @@ impl ::protobuf::Message for CreateRequest { if !self.working_directory.is_empty() { my_size += ::protobuf::rt::string_size(4, &self.working_directory); } + if !self.containerd_address.is_empty() { + my_size += ::protobuf::rt::string_size(5, &self.containerd_address); + } my_size += ::protobuf::rt::unknown_fields_size(self.special_fields.unknown_fields()); self.special_fields.cached_size().set(my_size as u32); my_size @@ -148,6 +161,9 @@ impl ::protobuf::Message for CreateRequest { if !self.working_directory.is_empty() { os.write_string(4, &self.working_directory)?; } + if !self.containerd_address.is_empty() { + os.write_string(5, &self.containerd_address)?; + } os.write_unknown_fields(self.special_fields.unknown_fields())?; ::std::result::Result::Ok(()) } @@ -169,6 +185,7 @@ impl ::protobuf::Message for CreateRequest { self.id.clear(); self.ttrpc_address.clear(); self.working_directory.clear(); + self.containerd_address.clear(); self.special_fields.clear(); } @@ -178,6 +195,7 @@ impl ::protobuf::Message for CreateRequest { id: ::std::string::String::new(), ttrpc_address: ::std::string::String::new(), working_directory: ::std::string::String::new(), + containerd_address: ::std::string::String::new(), special_fields: ::protobuf::SpecialFields::new(), }; &instance @@ -847,11 +865,12 @@ impl ::protobuf::reflect::ProtobufValue for DeleteResponse { } static file_descriptor_proto_data: &'static [u8] = b"\ - \n\rsandbox.proto\x12\x1brunwasi.services.sandbox.v1\"\x8f\x01\n\rCreate\ + \n\rsandbox.proto\x12\x1brunwasi.services.sandbox.v1\"\xbe\x01\n\rCreate\ Request\x12\x1c\n\tnamespace\x18\x01\x20\x01(\tR\tnamespace\x12\x0e\n\ \x02id\x18\x02\x20\x01(\tR\x02id\x12#\n\rttrpc_address\x18\x03\x20\x01(\ \tR\x0cttrpcAddress\x12+\n\x11working_directory\x18\x04\x20\x01(\tR\x10w\ - orkingDirectory\"1\n\x0eCreateResponse\x12\x1f\n\x0bsocket_path\x18\x01\ + orkingDirectory\x12-\n\x12containerd_address\x18\x05\x20\x01(\tR\x11cont\ + ainerdAddress\"1\n\x0eCreateResponse\x12\x1f\n\x0bsocket_path\x18\x01\ \x20\x01(\tR\nsocketPath\"E\n\x0eConnectRequest\x12\x0e\n\x02id\x18\x01\ \x20\x01(\tR\x02id\x12#\n\rttrpc_address\x18\x02\x20\x01(\tR\x0cttrpcAdd\ ress\"2\n\x0fConnectResponse\x12\x1f\n\x0bsocket_path\x18\x01\x20\x01(\t\ diff --git a/crates/containerd-shim-wasmedge/src/instance.rs b/crates/containerd-shim-wasmedge/src/instance.rs index 33609343a..0906d15f0 100644 --- a/crates/containerd-shim-wasmedge/src/instance.rs +++ b/crates/containerd-shim-wasmedge/src/instance.rs @@ -371,7 +371,11 @@ mod wasitest { spec.save(dir.path().join("config.json"))?; - let mut cfg = InstanceConfig::new(Wasi::new_engine()?, "test_namespace".into()); + let mut cfg = InstanceConfig::new( + Wasi::new_engine()?, + "test_namespace".into(), + "/containerd/address".into(), + ); let cfg = cfg .set_bundle(dir.path().to_str().unwrap().to_string()) .set_stdout(dir.path().join("stdout").to_str().unwrap().to_string()); @@ -407,7 +411,11 @@ mod wasitest { let vm = VmBuilder::new().with_config(config).build().unwrap(); let i = Wasi::new( "".to_string(), - Some(&InstanceConfig::new(vm, "test_namespace".into())), + Some(&InstanceConfig::new( + vm, + "test_namespace".into(), + "/containerd/address".into(), + )), ); i.delete().unwrap(); } diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index 5b5be474b..0385cb5bf 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -40,14 +40,11 @@ static mut STDERR_FD: Option = None; pub struct Wasi { exit_code: ExitCode, engine: wasmtime::Engine, - stdin: String, stdout: String, stderr: String, bundle: String, - rootdir: PathBuf, - id: String, } @@ -401,7 +398,11 @@ mod wasitest { ) .build()?; spec.save(dir.path().join("config.json"))?; - let mut cfg = InstanceConfig::new(Engine::default(), "test_namespace".into()); + let mut cfg = InstanceConfig::new( + Engine::default(), + "test_namespace".into(), + "/containerd/address".into(), + ); let cfg = cfg .set_bundle(dir.path().to_str().unwrap().to_string()) .set_stdout(dir.path().join("stdout").to_str().unwrap().to_string())