Skip to content

Commit 85898d5

Browse files
committed
Expose the platform value to the shim
Signed-off-by: James Sturtevant <[email protected]>
1 parent bae4a02 commit 85898d5

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

crates/containerd-shim-wasm/src/container/context.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::path::{Path, PathBuf};
22

3+
use oci_spec::image::Platform;
34
use oci_spec::runtime::Spec;
45

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

2627
fn wasm_layers(&self) -> &[WasmLayer];
28+
29+
fn platform(&self) -> &Platform;
2730
}
2831

2932
pub struct WasiEntrypoint {
@@ -34,6 +37,7 @@ pub struct WasiEntrypoint {
3437
pub(crate) struct WasiContext<'a> {
3538
pub spec: &'a Spec,
3639
pub wasm_layers: &'a [WasmLayer],
40+
pub platform: &'a Platform,
3741
}
3842

3943
impl RuntimeContext for WasiContext<'_> {
@@ -62,6 +66,10 @@ impl RuntimeContext for WasiContext<'_> {
6266
fn wasm_layers(&self) -> &[WasmLayer] {
6367
self.wasm_layers
6468
}
69+
70+
fn platform(&self) -> &Platform {
71+
self.platform
72+
}
6573
}
6674

6775
#[cfg(test)]
@@ -86,6 +94,7 @@ mod tests {
8694
let ctx = WasiContext {
8795
spec: &spec,
8896
wasm_layers: &[],
97+
platform: &Platform::default(),
8998
};
9099

91100
let args = ctx.args();
@@ -105,6 +114,7 @@ mod tests {
105114
let ctx = WasiContext {
106115
spec: &spec,
107116
wasm_layers: &[],
117+
platform: &Platform::default(),
108118
};
109119

110120
let args = ctx.args();
@@ -132,6 +142,7 @@ mod tests {
132142
let ctx = WasiContext {
133143
spec: &spec,
134144
wasm_layers: &[],
145+
platform: &Platform::default(),
135146
};
136147

137148
let args = ctx.args();
@@ -153,6 +164,7 @@ mod tests {
153164
let ctx = WasiContext {
154165
spec: &spec,
155166
wasm_layers: &[],
167+
platform: &Platform::default(),
156168
};
157169

158170
let path = ctx.wasi_entrypoint().path;
@@ -180,6 +192,7 @@ mod tests {
180192
let ctx = WasiContext {
181193
spec: &spec,
182194
wasm_layers: &[],
195+
platform: &Platform::default(),
183196
};
184197

185198
let WasiEntrypoint { path, func } = ctx.wasi_entrypoint();
@@ -208,6 +221,7 @@ mod tests {
208221
let ctx = WasiContext {
209222
spec: &spec,
210223
wasm_layers: &[],
224+
platform: &Platform::default(),
211225
};
212226

213227
let WasiEntrypoint { path, func } = ctx.wasi_entrypoint();

crates/containerd-shim-wasm/src/sandbox/containerd.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use containerd_client::services::v1::{GetContainerRequest, GetImageRequest, Read
1010
use containerd_client::tonic::transport::Channel;
1111
use containerd_client::{tonic, with_namespace};
1212
use futures::TryStreamExt;
13-
use oci_spec::image::{Arch, ImageManifest, ImageConfiguration, MediaType};
13+
use oci_spec::image::{Arch, ImageManifest, MediaType, Platform};
1414
use tokio::runtime::Runtime;
1515
use tonic::Request;
1616

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

131-
let arch = image_config.architecture();
132-
match arch {
133+
// the only part we care about here is the platform values
134+
let platform: Platform = serde_json::from_slice(image_config)?;
135+
match platform.architecture() {
133136
Arch::Wasm => {
134137
log::info!("found manifest with WASM OCI image format.");
135138
}
136139
_ => {
137140
log::info!("manifest is not in WASM OCI image format");
138-
return Ok([].to_vec());
141+
return Ok(([].to_vec(), platform));
139142
}
140143
}
141144

142-
manifest
145+
let layers = manifest
143146
.layers()
144147
.iter()
145148
.filter(|x| !is_image_layer_type(x.media_type()))
@@ -149,19 +152,25 @@ impl Client {
149152
layer: module,
150153
})
151154
})
152-
.collect::<Result<Vec<_>>>()
155+
.collect::<Result<Vec<_>>>()?;
156+
Ok((layers, platform))
153157
}
154158
}
155159

156-
fn is_image_layer_type(media_type: &MediaType)-> bool {
160+
fn is_image_layer_type(media_type: &MediaType) -> bool {
157161
match media_type {
158-
MediaType::ImageLayer |
159-
MediaType::ImageLayerGzip |
160-
MediaType::ImageLayerNonDistributable |
161-
MediaType::ImageLayerNonDistributableGzip |
162-
MediaType::ImageLayerNonDistributableZstd |
163-
MediaType::ImageLayerZstd => true,
164-
MediaType::Other(s) if s.as_str().starts_with("application/vnd.docker.image.rootfs.") => true,
165-
_ => false
162+
MediaType::ImageLayer
163+
| MediaType::ImageLayerGzip
164+
| MediaType::ImageLayerNonDistributable
165+
| MediaType::ImageLayerNonDistributableGzip
166+
| MediaType::ImageLayerNonDistributableZstd
167+
| MediaType::ImageLayerZstd => true,
168+
MediaType::Other(s)
169+
if s.as_str()
170+
.starts_with("application/vnd.docker.image.rootfs.") =>
171+
{
172+
true
173+
}
174+
_ => false,
166175
}
167176
}

crates/containerd-shim-wasm/src/sys/unix/container/executor.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use libcontainer::workload::{
1010
Executor as LibcontainerExecutor, ExecutorError as LibcontainerExecutorError,
1111
ExecutorValidationError,
1212
};
13+
use oci_spec::image::Platform;
1314
use oci_spec::runtime::Spec;
1415

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

3335
impl<E: Engine> LibcontainerExecutor for Executor<E> {
@@ -64,18 +66,24 @@ impl<E: Engine> LibcontainerExecutor for Executor<E> {
6466
}
6567

6668
impl<E: Engine> Executor<E> {
67-
pub fn new(engine: E, stdio: Stdio, wasm_layers: Vec<WasmLayer>) -> Self {
69+
pub fn new(engine: E, stdio: Stdio, wasm_layers: Vec<WasmLayer>, platform: Platform) -> Self {
6870
Self {
6971
engine,
7072
stdio,
7173
inner: Default::default(),
7274
wasm_layers,
75+
platform,
7376
}
7477
}
7578

7679
fn ctx<'a>(&'a self, spec: &'a Spec) -> WasiContext<'a> {
7780
let wasm_layers = &self.wasm_layers;
78-
WasiContext { spec, wasm_layers }
81+
let platform = &self.platform;
82+
WasiContext {
83+
spec,
84+
wasm_layers,
85+
platform,
86+
}
7987
}
8088

8189
fn inner(&self, spec: &Spec) -> &InnerExecutor {

crates/containerd-shim-wasm/src/sys/unix/container/instance.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use libcontainer::syscall::syscall::SyscallType;
1212
use nix::errno::Errno;
1313
use nix::sys::wait::{waitid, Id as WaitID, WaitPidFlag, WaitStatus};
1414
use nix::unistd::Pid;
15+
use oci_spec::image::Platform;
1516

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

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

5354
ContainerBuilder::new(id.clone(), SyscallType::Linux)
54-
.with_executor(Executor::new(engine, stdio, modules))
55+
.with_executor(Executor::new(engine, stdio, modules, platform))
5556
.with_root_path(rootdir.clone())?
5657
.as_init(&bundle)
5758
.with_systemd(false)

0 commit comments

Comments
 (0)