-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(shim): support archive layers #121
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,10 +40,6 @@ const SPIN_HTTP_LISTEN_ADDR_ENV: &str = "SPIN_HTTP_LISTEN_ADDR"; | |
const RUNTIME_CONFIG_PATH: &str = "/runtime-config.toml"; | ||
/// Describes an OCI layer with Wasm content | ||
const OCI_LAYER_MEDIA_TYPE_WASM: &str = "application/vnd.wasm.content.layer.v1+wasm"; | ||
/// Describes an OCI layer with data content | ||
const OCI_LAYER_MEDIA_TYPE_DATA: &str = "application/vnd.wasm.content.layer.v1+data"; | ||
/// Describes an OCI layer containing a Spin application config | ||
const OCI_LAYER_MEDIA_TYPE_SPIN_CONFIG: &str = "application/vnd.fermyon.spin.application.v1+config"; | ||
/// Expected location of the Spin manifest when loading from a file rather than | ||
/// an OCI image | ||
const SPIN_MANIFEST_FILE_PATH: &str = "/spin.toml"; | ||
|
@@ -136,7 +132,7 @@ impl SpinEngine { | |
.write_wasm(&artifact.layer, &artifact.config.digest()) | ||
.await?; | ||
} | ||
MediaType::Other(name) if name == OCI_LAYER_MEDIA_TYPE_DATA => { | ||
MediaType::Other(name) if name == spin_oci::client::DATA_MEDIATYPE => { | ||
log::debug!( | ||
"<<< writing data layer to cache, near {:?}", | ||
cache.manifests_dir() | ||
|
@@ -145,6 +141,19 @@ impl SpinEngine { | |
.write_data(&artifact.layer, &artifact.config.digest()) | ||
.await?; | ||
} | ||
MediaType::Other(name) if name == spin_oci::client::ARCHIVE_MEDIATYPE => { | ||
log::debug!( | ||
"<<< writing archive layer and unpacking contents to cache, near {:?}", | ||
cache.manifests_dir() | ||
); | ||
self.handle_archive_layer( | ||
cache, | ||
&artifact.layer, | ||
&artifact.config.digest(), | ||
) | ||
.await | ||
.context("unable to unpack archive layer")?; | ||
} | ||
_ => { | ||
log::debug!( | ||
"<<< unknown media type {:?}", | ||
|
@@ -367,6 +376,25 @@ impl SpinEngine { | |
} | ||
None | ||
} | ||
|
||
async fn handle_archive_layer( | ||
&self, | ||
cache: &Cache, | ||
bytes: impl AsRef<[u8]>, | ||
digest: impl AsRef<str>, | ||
) -> Result<()> { | ||
// spin_oci::client::unpack_archive_layer attempts to create a tempdir via tempfile::tempdir() | ||
// which will fall back to /tmp if TMPDIR is not set. /tmp is either not found or not accessible | ||
// in the shim environment, hence setting to current working directory. | ||
if env::var("TMPDIR").is_err() { | ||
log::debug!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When do we expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point... it appears it isn't ever set, currently. Glad to remove this debug log if you'd like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we would want to leave it, otherwise we might overwrite it. I don't know what scenario it would be set in, but if it was set and we overwrite I don't think that would be an expected behavior? |
||
"<<< TMPDIR is not set; setting to current working directory for unpacking archive layer" | ||
); | ||
env::set_var("TMPDIR", env::current_dir().unwrap_or(".".into())); | ||
} | ||
|
||
spin_oci::client::unpack_archive_layer(cache, bytes, digest).await | ||
} | ||
} | ||
|
||
impl Engine for SpinEngine { | ||
|
@@ -405,8 +433,9 @@ impl Engine for SpinEngine { | |
fn supported_layers_types() -> &'static [&'static str] { | ||
&[ | ||
OCI_LAYER_MEDIA_TYPE_WASM, | ||
OCI_LAYER_MEDIA_TYPE_DATA, | ||
OCI_LAYER_MEDIA_TYPE_SPIN_CONFIG, | ||
spin_oci::client::ARCHIVE_MEDIATYPE, | ||
spin_oci::client::DATA_MEDIATYPE, | ||
spin_oci::client::SPIN_APPLICATION_MEDIA_TYPE, | ||
] | ||
} | ||
|
||
|
@@ -529,7 +558,7 @@ mod tests { | |
let data_content = WasmLayer { | ||
layer: vec![], | ||
config: oci_spec::image::Descriptor::new( | ||
MediaType::Other(OCI_LAYER_MEDIA_TYPE_DATA.to_string()), | ||
MediaType::Other(spin_oci::client::DATA_MEDIATYPE.to_string()), | ||
1024, | ||
"sha256:1234", | ||
), | ||
|
@@ -569,7 +598,7 @@ mod tests { | |
WasmLayer { | ||
layer: vec![], | ||
config: oci_spec::image::Descriptor::new( | ||
MediaType::Other(OCI_LAYER_MEDIA_TYPE_DATA.to_string()), | ||
MediaType::Other(spin_oci::client::DATA_MEDIATYPE.to_string()), | ||
1024, | ||
"sha256:1234", | ||
), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/.spin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM --platform=${BUILDPLATFORM} alpine | ||
COPY assets ./assets | ||
COPY spin.toml . |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'Twas brillig, and the slithy toves | ||
Did gyre and gimble in the wabe: | ||
All mimsy were the borogoves, | ||
And the mome raths outgrabe. | ||
|
||
"Beware the Jabberwock, my son! | ||
The jaws that bite, the claws that catch! | ||
Beware the Jubjub bird, and shun | ||
The frumious Bandersnatch!" | ||
|
||
He took his vorpal sword in hand; | ||
Long time the manxome foe he sought- | ||
So rested he by the Tumtum tree | ||
And stood awhile in thought. | ||
|
||
And, as in uffish thought he stood, | ||
The Jabberwock, with eyes of flame, | ||
Came whiffling through the tulgey wood, | ||
And burbled as it came! | ||
|
||
One, two! One, two! And through and through | ||
The vorpal blade went snicker-snack! | ||
He left it dead, and with its head | ||
He went galumphing back. | ||
|
||
"And hast thou slain the Jabberwock? | ||
Come to my arms, my beamish boy! | ||
O frabjous day! Callooh! Callay!" | ||
He chortled in his joy. | ||
|
||
'Twas brillig, and the slithy toves | ||
Did gyre and gimble in the wabe: | ||
All mimsy were the borogoves, | ||
And the mome raths outgrabe. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
spin_manifest_version = 2 | ||
|
||
[application] | ||
name = "spin-static-assets" | ||
version = "0.1.0" | ||
authors = ["SpinKube Engineering Team"] | ||
|
||
[application.trigger.http] | ||
base = "/" | ||
|
||
[[trigger.http]] | ||
id = "trigger-static-fileserver" | ||
component = "static-fileserver" | ||
route = "/..." | ||
|
||
[component.static-fileserver] | ||
source = { url = "https://github.com/fermyon/spin-fileserver/releases/download/v0.3.0/spin_static_fs.wasm", digest = "sha256:ef88708817e107bf49985c7cefe4dd1f199bf26f6727819183d5c996baa3d148" } | ||
files = [{ source = "assets", destination = "/" }] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: make
spin_oci::client::WASM_LAYER_MEDIA_TYPE
public and use it in the Spin Shim.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, Spin itself is waiting on the canonical type/string upstream: https://github.com/fermyon/spin/blob/main/crates/oci/src/client.rs#L34-L35. To be defined in https://github.com/bytecodealliance/registry perhaps? Therefore expecting both Spin and the shim would pull this from the upstream crate.