diff --git a/src/runtime-rs/crates/shim/src/bin/main.rs b/src/runtime-rs/crates/shim/src/bin/main.rs index 1526a89cd..262f9e238 100644 --- a/src/runtime-rs/crates/shim/src/bin/main.rs +++ b/src/runtime-rs/crates/shim/src/bin/main.rs @@ -16,8 +16,10 @@ use nix::{ }; use shim::{config, Args, Error, ShimExecutor}; -const DEFAULT_RUNTIME_WORKER_THREADS: usize = 2; -const ENV_RUNTIME_WORKER_THREADS: &str = "RUNTIME_WORKER_THREADS"; +// default tokio runtime worker threads +const DEFAULT_TOKIO_RUNTIME_WORKER_THREADS: usize = 2; +// env to config tokio runtime worker threads +const ENV_TOKIO_RUNTIME_WORKER_THREADS: &str = "TOKIO_RUNTIME_WORKER_THREADS"; #[derive(Debug)] enum Action { @@ -116,10 +118,10 @@ fn show_version(err: Option) { } fn get_tokio_runtime() -> Result { - let worker_threads = std::env::var(ENV_RUNTIME_WORKER_THREADS) + let worker_threads = std::env::var(ENV_TOKIO_RUNTIME_WORKER_THREADS) .unwrap_or_default() .parse() - .unwrap_or(DEFAULT_RUNTIME_WORKER_THREADS); + .unwrap_or(DEFAULT_TOKIO_RUNTIME_WORKER_THREADS); let rt = tokio::runtime::Builder::new_multi_thread() .worker_threads(worker_threads) diff --git a/src/runtime-rs/crates/shim/src/shim.rs b/src/runtime-rs/crates/shim/src/shim.rs index 298f976dc..164e6c6f6 100644 --- a/src/runtime-rs/crates/shim/src/shim.rs +++ b/src/runtime-rs/crates/shim/src/shim.rs @@ -30,29 +30,30 @@ impl ShimExecutor { ShimExecutor { args } } - pub(crate) fn load_oci_spec(&self) -> Result { - let bundle_path = self.get_bundle_path()?; - let spec_file = bundle_path.join("config.json"); - + pub(crate) fn load_oci_spec(&self, path: &Path) -> Result { + let spec_file = path.join("config.json"); oci::Spec::load(spec_file.to_str().unwrap_or_default()).context("load spec") } - pub(crate) fn write_address(&self, address: &Path) -> Result<()> { - let dir = self.get_bundle_path()?; - let file_path = &dir.join("address"); + pub(crate) fn write_address(&self, path: &Path, address: &Path) -> Result<()> { + let file_path = &path.join("address"); std::fs::write(file_path, address.as_os_str().as_bytes()) .context(Error::FileWrite(format!("{:?}", &file_path))) } - pub(crate) fn write_pid_file(&self, pid: u32) -> Result<()> { - let dir = self.get_bundle_path()?; - let file_path = &dir.join(SHIM_PID_FILE); + pub(crate) fn write_pid_file(&self, path: &Path, pid: u32) -> Result<()> { + let file_path = &path.join(SHIM_PID_FILE); std::fs::write(file_path, format!("{}", pid)) .context(Error::FileWrite(format!("{:?}", &file_path))) } - pub(crate) fn read_pid_file(&self, bundle_path: &Path) -> Result { - let file_path = bundle_path.join(SHIM_PID_FILE); + // There may be a multi-container for a Pod, each container has a bundle path, we need to write + // the PID to the file for each container in their own bundle path, so we can directly get the + // `bundle_path()` and write the PID. + // While the real runtime process's PID is stored in the file in the sandbox container's bundle + // path, so needs to read from the sandbox container's bundle path. + pub(crate) fn read_pid_file(&self, path: &Path) -> Result { + let file_path = path.join(SHIM_PID_FILE); let data = std::fs::read_to_string(&file_path) .context(Error::FileOpen(format!("{:?}", file_path)))?; @@ -71,6 +72,10 @@ impl ShimExecutor { let data = [&self.args.address, &self.args.namespace, id].join("/"); let mut hasher = sha2::Sha256::new(); hasher.update(data); + + // Follow + // https://github.com/containerd/containerd/blob/main/runtime/v2/shim/util_unix.go#L68 to + // generate a shim socket path. Ok(PathBuf::from(format!( "unix://{}/s/{:X}", SOCKET_ROOT, @@ -103,13 +108,15 @@ mod tests { let executor = ShimExecutor::new(args); - executor.write_address(Path::new("12345")).unwrap(); + executor + .write_address(bundle_path, Path::new("12345")) + .unwrap(); let dir = executor.get_bundle_path().unwrap(); let file_path = &dir.join("address"); let buf = std::fs::read_to_string(file_path).unwrap(); assert_eq!(&buf, "12345"); - executor.write_pid_file(1267).unwrap(); + executor.write_pid_file(&dir, 1267).unwrap(); let read_pid = executor.read_pid_file(&dir).unwrap(); assert_eq!(read_pid, 1267); } diff --git a/src/runtime-rs/crates/shim/src/shim_start.rs b/src/runtime-rs/crates/shim/src/shim_start.rs index b5b136607..a84053209 100644 --- a/src/runtime-rs/crates/shim/src/shim_start.rs +++ b/src/runtime-rs/crates/shim/src/shim_start.rs @@ -32,7 +32,8 @@ impl ShimExecutor { } fn do_start(&mut self) -> Result { - let spec = self.load_oci_spec()?; + let bundle_path = self.get_bundle_path().context("get bundle path")?; + let spec = self.load_oci_spec(&bundle_path)?; let (container_type, id) = k8s::container_type_with_id(&spec); match container_type { @@ -40,8 +41,8 @@ impl ShimExecutor { let address = self.socket_address(&self.args.id)?; let socket = new_listener(&address)?; let child_pid = self.create_shim_process(socket)?; - self.write_pid_file(child_pid)?; - self.write_address(&address)?; + self.write_pid_file(&bundle_path, child_pid)?; + self.write_address(&bundle_path, &address)?; Ok(address) } ContainerType::PodContainer => { @@ -49,8 +50,8 @@ impl ShimExecutor { .ok_or(Error::InvalidArgument) .context("get sid for container")?; let (address, pid) = self.get_shim_info_from_sandbox(&sid)?; - self.write_pid_file(pid)?; - self.write_address(&address)?; + self.write_pid_file(&bundle_path, pid)?; + self.write_address(&bundle_path, &address)?; Ok(address) } } @@ -200,8 +201,8 @@ mod tests { let executor = ShimExecutor::new(args); let addr = executor.socket_address(&executor.args.id).unwrap(); - executor.write_address(&addr).unwrap(); - executor.write_pid_file(1267).unwrap(); + executor.write_address(bundle_path, &addr).unwrap(); + executor.write_pid_file(bundle_path, 1267).unwrap(); let container_id = gen_id(16); let bundle_path2 = &dir.path().join(&container_id);