shim: introduce Shim::wait()

Introduce Shim::wait() to wait for a shim instance to exit, so the
`Shim` implementor could customize the way to manage exit event.

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
Liu Jiang 2021-12-13 01:10:06 +08:00
parent fdd50661f3
commit a2bfeed2ca
2 changed files with 16 additions and 21 deletions

View File

@ -17,11 +17,11 @@
use containerd_shim as shim; use containerd_shim as shim;
use log::info; use log::info;
use shim::{api, TtrpcContext, TtrpcResult}; use shim::{api, ExitSignal, TtrpcContext, TtrpcResult};
#[derive(Clone)] #[derive(Clone)]
struct Service { struct Service {
exit: shim::ExitSignal, exit: ExitSignal,
} }
impl shim::Shim for Service { impl shim::Shim for Service {
@ -33,9 +33,10 @@ impl shim::Shim for Service {
_namespace: &str, _namespace: &str,
_publisher: shim::RemotePublisher, _publisher: shim::RemotePublisher,
_config: &mut shim::Config, _config: &mut shim::Config,
exit: shim::ExitSignal,
) -> Self { ) -> Self {
Service { exit } Service {
exit: ExitSignal::default(),
}
} }
fn start_shim(&mut self, opts: shim::StartOpts) -> Result<String, shim::Error> { fn start_shim(&mut self, opts: shim::StartOpts) -> Result<String, shim::Error> {
@ -43,6 +44,10 @@ impl shim::Shim for Service {
Ok(address) Ok(address)
} }
fn wait(&mut self) {
self.exit.wait();
}
fn get_task_service(&self) -> Self::T { fn get_task_service(&self) -> Self::T {
self.clone() self.clone()
} }

View File

@ -109,7 +109,7 @@ impl ExitSignal {
} }
/// Wait for the exit signal to be set. /// Wait for the exit signal to be set.
fn wait(&self) { pub fn wait(&self) {
let (lock, cvar) = &*self.0; let (lock, cvar) = &*self.0;
let mut started = lock.lock().unwrap(); let mut started = lock.lock().unwrap();
while !*started { while !*started {
@ -129,13 +129,7 @@ pub trait Shim {
type T: Task + Send + Sync; type T: Task + Send + Sync;
/// Create a new instance of Shim. /// Create a new instance of Shim.
fn new( fn new(id: &str, namespace: &str, publisher: RemotePublisher, config: &mut Config) -> Self;
id: &str,
namespace: &str,
publisher: RemotePublisher,
config: &mut Config,
exit: ExitSignal,
) -> Self;
/// Start shim will be called by containerd when launching new shim instance. /// Start shim will be called by containerd when launching new shim instance.
/// ///
@ -149,6 +143,9 @@ pub trait Shim {
Ok(DeleteResponse::default()) Ok(DeleteResponse::default())
} }
/// Wait for the shim to exit.
fn wait(&mut self);
/// Get the task service object. /// Get the task service object.
fn get_task_service(&self) -> Self::T; fn get_task_service(&self) -> Self::T;
} }
@ -176,15 +173,8 @@ where
let publisher = publisher::RemotePublisher::new(&ttrpc_address)?; let publisher = publisher::RemotePublisher::new(&ttrpc_address)?;
// Create shim instance // Create shim instance
let exit_signal = ExitSignal::default();
let mut config = Config::default(); let mut config = Config::default();
let mut shim = T::new( let mut shim = T::new(id, &flags.namespace, publisher, &mut config);
id,
&flags.namespace,
publisher,
&mut config,
exit_signal.clone(),
);
if !config.no_sub_reaper { if !config.no_sub_reaper {
reap::set_subreaper()?; reap::set_subreaper()?;
@ -237,7 +227,7 @@ where
server.start()?; server.start()?;
info!("Shim successfully started, waiting for exit signal..."); info!("Shim successfully started, waiting for exit signal...");
exit_signal.wait(); shim.wait();
info!("Shutting down shim instance"); info!("Shutting down shim instance");
server.shutdown(); server.shutdown();