mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-29 04:04:45 +00:00
runtime-rs: modify the review suggestion
Fixes: #3876 Signed-off-by: quanwei.zqw <quanwei.zqw@alibaba-inc.com>
This commit is contained in:
parent
278f843f92
commit
8c0a60e191
@ -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<anyhow::Error>) {
|
||||
}
|
||||
|
||||
fn get_tokio_runtime() -> Result<tokio::runtime::Runtime> {
|
||||
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)
|
||||
|
@ -30,29 +30,30 @@ impl ShimExecutor {
|
||||
ShimExecutor { args }
|
||||
}
|
||||
|
||||
pub(crate) fn load_oci_spec(&self) -> Result<oci::Spec> {
|
||||
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<oci::Spec> {
|
||||
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<u32> {
|
||||
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<u32> {
|
||||
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);
|
||||
}
|
||||
|
@ -32,7 +32,8 @@ impl ShimExecutor {
|
||||
}
|
||||
|
||||
fn do_start(&mut self) -> Result<PathBuf> {
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user