containerd_shimkit/sandbox/
instance.rs

1//! Abstractions for running/managing a wasm/wasi instance.
2
3use std::path::PathBuf;
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8use super::error::Error;
9use crate::sandbox::shim::Config;
10
11/// Generic options builder for creating a wasm instance.
12/// This is passed to the `Instance::new` method.
13#[derive(Clone, Serialize, Deserialize, Debug, Default)]
14pub struct InstanceConfig {
15    /// Optional stdin named pipe path.
16    pub stdin: PathBuf,
17    /// Optional stdout named pipe path.
18    pub stdout: PathBuf,
19    /// Optional stderr named pipe path.
20    pub stderr: PathBuf,
21    /// Path to the OCI bundle directory.
22    pub bundle: PathBuf,
23    /// Namespace for containerd
24    pub namespace: String,
25    /// GRPC address back to main containerd
26    pub containerd_address: String,
27    /// containerd runtime options config
28    pub config: Config,
29}
30
31/// Represents a WASI module(s).
32/// Instance is a trait that gets implemented by consumers of this library.
33/// This trait requires that any type implementing it is `'static`, similar to `std::any::Any`.
34/// This means that the type cannot contain a non-`'static` reference.
35#[cfg_attr(not(doc), trait_variant::make(Send))]
36pub trait Instance: 'static {
37    /// Create a new instance
38    async fn new(id: String, cfg: &InstanceConfig) -> Result<Self, Error>
39    where
40        Self: Sized;
41
42    /// Start the instance
43    /// The returned value should be a unique ID (such as a PID) for the instance.
44    /// Nothing internally should be using this ID, but it is returned to containerd where a user may want to use it.
45    async fn start(&self) -> Result<u32, Error>;
46
47    /// Send a signal to the instance
48    async fn kill(&self, signal: u32) -> Result<(), Error>;
49
50    /// Delete any reference to the instance
51    /// This is called after the instance has exited.
52    async fn delete(&self) -> Result<(), Error>;
53
54    /// Waits for the instance to finish and returns its exit code
55    /// This is an async call.
56    async fn wait(&self) -> (u32, DateTime<Utc>);
57}