Skip to content

Commit

Permalink
Expose the platform value to the shim
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Oct 23, 2023
1 parent bae4a02 commit 85898d5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
14 changes: 14 additions & 0 deletions crates/containerd-shim-wasm/src/container/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};

use oci_spec::image::Platform;
use oci_spec::runtime::Spec;

use crate::sandbox::oci::WasmLayer;
Expand All @@ -24,6 +25,8 @@ pub trait RuntimeContext {
fn wasi_entrypoint(&self) -> WasiEntrypoint;

fn wasm_layers(&self) -> &[WasmLayer];

fn platform(&self) -> &Platform;
}

pub struct WasiEntrypoint {
Expand All @@ -34,6 +37,7 @@ pub struct WasiEntrypoint {
pub(crate) struct WasiContext<'a> {
pub spec: &'a Spec,
pub wasm_layers: &'a [WasmLayer],
pub platform: &'a Platform,
}

impl RuntimeContext for WasiContext<'_> {
Expand Down Expand Up @@ -62,6 +66,10 @@ impl RuntimeContext for WasiContext<'_> {
fn wasm_layers(&self) -> &[WasmLayer] {
self.wasm_layers
}

fn platform(&self) -> &Platform {
self.platform
}
}

#[cfg(test)]
Expand All @@ -86,6 +94,7 @@ mod tests {
let ctx = WasiContext {
spec: &spec,
wasm_layers: &[],
platform: &Platform::default(),
};

let args = ctx.args();
Expand All @@ -105,6 +114,7 @@ mod tests {
let ctx = WasiContext {
spec: &spec,
wasm_layers: &[],
platform: &Platform::default(),
};

let args = ctx.args();
Expand Down Expand Up @@ -132,6 +142,7 @@ mod tests {
let ctx = WasiContext {
spec: &spec,
wasm_layers: &[],
platform: &Platform::default(),
};

let args = ctx.args();
Expand All @@ -153,6 +164,7 @@ mod tests {
let ctx = WasiContext {
spec: &spec,
wasm_layers: &[],
platform: &Platform::default(),
};

let path = ctx.wasi_entrypoint().path;
Expand Down Expand Up @@ -180,6 +192,7 @@ mod tests {
let ctx = WasiContext {
spec: &spec,
wasm_layers: &[],
platform: &Platform::default(),
};

let WasiEntrypoint { path, func } = ctx.wasi_entrypoint();
Expand Down Expand Up @@ -208,6 +221,7 @@ mod tests {
let ctx = WasiContext {
spec: &spec,
wasm_layers: &[],
platform: &Platform::default(),
};

let WasiEntrypoint { path, func } = ctx.wasi_entrypoint();
Expand Down
43 changes: 26 additions & 17 deletions crates/containerd-shim-wasm/src/sandbox/containerd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use containerd_client::services::v1::{GetContainerRequest, GetImageRequest, Read
use containerd_client::tonic::transport::Channel;
use containerd_client::{tonic, with_namespace};
use futures::TryStreamExt;
use oci_spec::image::{Arch, ImageManifest, ImageConfiguration, MediaType};
use oci_spec::image::{Arch, ImageManifest, MediaType, Platform};
use tokio::runtime::Runtime;
use tonic::Request;

Expand Down Expand Up @@ -116,7 +116,10 @@ impl Client {
// load module will query the containerd store to find an image that has an OS of type 'wasm'
// If found it continues to parse the manifest and return the layers that contains the WASM modules
// and possibly other configuration layers.
pub fn load_modules(&self, containerd_id: impl ToString) -> Result<Vec<oci::WasmLayer>> {
pub fn load_modules(
&self,
containerd_id: impl ToString,
) -> Result<(Vec<oci::WasmLayer>, Platform)> {
let image_name = self.get_image(containerd_id.to_string())?;
let digest = self.get_image_content_sha(image_name)?;
let manifest = self.read_content(digest)?;
Expand All @@ -126,20 +129,20 @@ impl Client {
let image_config_descriptor = manifest.config();
let image_config = self.read_content(image_config_descriptor.digest())?;
let image_config = image_config.as_slice();
let image_config = ImageConfiguration::from_reader(image_config)?;

let arch = image_config.architecture();
match arch {
// the only part we care about here is the platform values
let platform: Platform = serde_json::from_slice(image_config)?;
match platform.architecture() {
Arch::Wasm => {
log::info!("found manifest with WASM OCI image format.");
}
_ => {
log::info!("manifest is not in WASM OCI image format");
return Ok([].to_vec());
return Ok(([].to_vec(), platform));
}
}

manifest
let layers = manifest
.layers()
.iter()
.filter(|x| !is_image_layer_type(x.media_type()))
Expand All @@ -149,19 +152,25 @@ impl Client {
layer: module,
})
})
.collect::<Result<Vec<_>>>()
.collect::<Result<Vec<_>>>()?;
Ok((layers, platform))
}
}

fn is_image_layer_type(media_type: &MediaType)-> bool {
fn is_image_layer_type(media_type: &MediaType) -> bool {
match media_type {
MediaType::ImageLayer |
MediaType::ImageLayerGzip |
MediaType::ImageLayerNonDistributable |
MediaType::ImageLayerNonDistributableGzip |
MediaType::ImageLayerNonDistributableZstd |
MediaType::ImageLayerZstd => true,
MediaType::Other(s) if s.as_str().starts_with("application/vnd.docker.image.rootfs.") => true,
_ => false
MediaType::ImageLayer
| MediaType::ImageLayerGzip
| MediaType::ImageLayerNonDistributable
| MediaType::ImageLayerNonDistributableGzip
| MediaType::ImageLayerNonDistributableZstd
| MediaType::ImageLayerZstd => true,
MediaType::Other(s)
if s.as_str()
.starts_with("application/vnd.docker.image.rootfs.") =>
{
true
}
_ => false,
}
}
12 changes: 10 additions & 2 deletions crates/containerd-shim-wasm/src/sys/unix/container/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use libcontainer::workload::{
Executor as LibcontainerExecutor, ExecutorError as LibcontainerExecutorError,
ExecutorValidationError,
};
use oci_spec::image::Platform;
use oci_spec::runtime::Spec;

use crate::container::{Engine, PathResolve, RuntimeContext, Stdio, WasiContext};
Expand All @@ -28,6 +29,7 @@ pub(crate) struct Executor<E: Engine> {
stdio: Stdio,
inner: OnceCell<InnerExecutor>,
wasm_layers: Vec<WasmLayer>,
platform: Platform,
}

impl<E: Engine> LibcontainerExecutor for Executor<E> {
Expand Down Expand Up @@ -64,18 +66,24 @@ impl<E: Engine> LibcontainerExecutor for Executor<E> {
}

impl<E: Engine> Executor<E> {
pub fn new(engine: E, stdio: Stdio, wasm_layers: Vec<WasmLayer>) -> Self {
pub fn new(engine: E, stdio: Stdio, wasm_layers: Vec<WasmLayer>, platform: Platform) -> Self {
Self {
engine,
stdio,
inner: Default::default(),
wasm_layers,
platform,
}
}

fn ctx<'a>(&'a self, spec: &'a Spec) -> WasiContext<'a> {
let wasm_layers = &self.wasm_layers;
WasiContext { spec, wasm_layers }
let platform = &self.platform;
WasiContext {
spec,
wasm_layers,
platform,
}
}

fn inner(&self, spec: &Spec) -> &InnerExecutor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use libcontainer::syscall::syscall::SyscallType;
use nix::errno::Errno;
use nix::sys::wait::{waitid, Id as WaitID, WaitPidFlag, WaitStatus};
use nix::unistd::Pid;
use oci_spec::image::Platform;

use crate::container::Engine;
use crate::sandbox::instance_utils::{determine_rootdir, get_instance_root, instance_exists};
Expand Down Expand Up @@ -43,15 +44,15 @@ impl<E: Engine> SandboxInstance for Instance<E> {
let stdio = Stdio::init_from_cfg(cfg)?;

// check if container is OCI image with wasm layers and attempt to read the module
let modules = containerd::Client::connect(cfg.get_containerd_address(), &namespace)?
let (modules, platform) = containerd::Client::connect(cfg.get_containerd_address(), &namespace)?
.load_modules(&id)
.unwrap_or_else(|e| {
log::warn!("Error obtaining wasm layers for container {id}. Will attempt to use files inside container image. Error: {e}");
vec![]
(vec![], Platform::default())
});

ContainerBuilder::new(id.clone(), SyscallType::Linux)
.with_executor(Executor::new(engine, stdio, modules))
.with_executor(Executor::new(engine, stdio, modules, platform))
.with_root_path(rootdir.clone())?
.as_init(&bundle)
.with_systemd(false)
Expand Down

0 comments on commit 85898d5

Please sign in to comment.