-
Notifications
You must be signed in to change notification settings - Fork 97
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
refactor: adding LibcontainerInstance
for default implementation of Instance
trait
#226
Conversation
…nce trait This commits adds a new YoukiInstance trait in the containerd-shim-wasm crate. It refactors the common youki Instance implementation into this trait and as a consequence, all the Instance implementation from both the wasmtime and wasmedge shims moved to a common place. This closes containerd#131 Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
I like the way this shares all the work and makes it easier for the implementors and think this is a great direction eventually. The concern I have is I've gotten back to work on #49 and found that since our move to youki this PR could cause an issue since youki doesn't support Windows (and it appears would need some major work to get it going). If a shim such as wasmtime implements |
@jsturtevant I can make |
was hoping to share more but with the direction towards youki, in the short term this is probably the best option. |
👍 |
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
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.
This looks great, thanks for working on it!
Some minor comments. The comments in wasmedge
also apply to wasmtime
.
In the PR description you mention
The downside is that any new shim instance needs to implement both YoukiInstance and the Instance traits.
But there's a blanket implementation of Instance
for YoukiInstance
, so there's no need to implement both traits, or am I missing something?
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
…im-wasm Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
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.
I really like how this is looking, Thanks @Mossaka !
type E: Send + Sync + Clone; | ||
|
||
/// Create a new instance | ||
fn new_youki(id: String, cfg: Option<&InstanceConfig<Self::E>>) -> Self; |
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 / preference: rename this to plain new
:
- In L.65 do
<Self as YoukiInstance>::new(...)
- In the implementations, instead of doing
use containerd_shim_wasm::sandbox:YoukiInstance;
impl YoukiInstance for Wasi { ... }
do
use containerd_shim_wasm::sandbox;
impl sandbox::YoukiInstance for Wasi { ... }
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.
If we change new_youki
to new
, then there is a naming conflict which is pretty annoying.
Rust will complain about the usage of Wasi::new()
by saying that new
is referring both to LibcontainerInstance::new and Instance::new and requires users to specify which one to refer to.
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.
That's why you would need
<Self as LibcontainerInstance>::new(...)
But I'm not sure that's better than the method being called new_libcontainer
.
I guess it's just odd that the method new
is part of a trait, and not part of the struct's impl.
I'm happy to keep this as new_libcontainer
.
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.
Looks great! I've added a couple comments.
.map_err(err_others)?; | ||
// Close the fds now that they have been passed to the container process | ||
// so that we don't leak them. | ||
stdin.map(close); |
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.
Are there cases here where these may not get closed? For example, if one of the map_err
is hit after the initial with_executor
call, will there be an opportunity for the function to exit without calling close? Or, do we only care if build
is called with an Ok result?
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.
I think we only care about build
calling with an Ok result, but you remind me to close file descriptors on the wasmtime 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.
I woiuld be nice if maybe_open_stdio
returned an RAII wrapper.
A simple approach would be to return the File
instead of the RawFd
, and using as_raw_fd()
when needed instead of using into_raw_fd()
early on.
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.
I woiuld be nice if
maybe_open_stdio
returned an RAII wrapper. A simple approach would be to return theFile
instead of theRawFd
, and usingas_raw_fd()
when needed instead of usinginto_raw_fd()
early on.
#240 is similar to what you reference here, thought I am not sure what RAII is
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.
Sorry, that's my inner C++ betraying me.
It's basically when you have a struct implementing Drop
to cleanup after itself.
When you drop
a File
, it automatically closes the file for you.
It is kind of what #240 does, but instead of using into_raw_fd
the executors would hold a Option<File>
instead of an Option<RawFd>
, and the conversion to RawFd
would be done at le last possible moment.
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.
I tried this but ran into some lifetime issues with the File and opted for a smaller change but if you have any suggestions be happy to fix that up 👍
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.
I was thinking something like this jsturtevant/runwasi@use-cross-platfrom-file...jprendes:runwasi:use-cross-platfrom-file
@@ -32,7 +32,7 @@ thiserror = "1.0" | |||
libc = "0.2.147" | |||
oci-spec = { version = "0.6.1", features = ["runtime"] } | |||
sha256 = "1.3.0" | |||
libcontainer = "0.1" | |||
libcontainer = { version = "0.1", default-features = false } |
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.
is default-features=false
needed?
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.
Actually not, let me remove it.
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.
Actually it is needed because otherwise you will get this error
warning: /home/mossaka/developer/runwasi/crates/containerd-shim-wasm/Cargo.toml: `default-features` is ignored for libcontainer, since `default-features` was not specified for `workspace.dependencies.libcontainer`, this could become a hard error in the future
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.
looking really good! I think once the last few open comments are addressed this is in good shape
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Ah that was a mistaken. I used a different implementation so that shim author only needs to implement |
YoukiInstance
for default implementation of Instance
traitLibcontainerInstance
for default implementation of Instance
trait
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.
LGTM
not sure what our current set up is in regards to CI and commits, but maybe squash the commits? |
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.
LGTM!
} | ||
} | ||
} as u32; | ||
let mut ec = lock.lock().unwrap(); |
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.
let mut ec = lock.lock().unwrap(); | |
let mut ec = lock.lock().map_err(|err| Error::Any(anyhow::anyhow!("failed get a lockr: {}", err)))?; |
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.
The error isn't able to automatically transfer so I will leave it as is.
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.
I think its good to let this panic anyway.
If we can't lock something is very wrong (dirty mutex state) and this should crash the program.
crates/containerd-shim-wasm/src/sandbox/libcontainer_instance.rs
Outdated
Show resolved
Hide resolved
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Going to merge this in. Thanks everyone for reviewing it!! Really appreciate it. |
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.
I haven't given this a thorough review yet, but I like where this is going at lot.
fn get_root_dir(&self) -> Result<PathBuf, Error>; | ||
|
||
/// Build the container | ||
fn build_container(&self) -> Result<Container, Error>; |
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.
build_container
seems like a fairly heavy trait function.
I think if we add something like open_stdio
to the trait we can implement common build_container
?
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.
IIUC, that would move the stdio redirection work from the executor to the instance.
Is there any instance / executor that would not want to do the stdio redirection trick (slight/spin/wws)?
Can that be handled by default in Instance
instead of LibcontainerInstance
?
} | ||
} | ||
} as u32; | ||
let mut ec = lock.lock().unwrap(); |
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.
I think its good to let this panic anyway.
If we can't lock something is very wrong (dirty mutex state) and this should crash the program.
This commits adds a new
LibcontainerInstance
trait in the containerd-shim-wasm crate. It refactors the common youki Instance implementation into this trait and as a consequence, all the Instance implementation from both the wasmtime and wasmedge shims moved to a common place.This requires the shim author to implement the newly introduced
LibcontainerInstance
and then uses its default methods to implement Instance. The reason doing that is there are no changes to the existingInstance
trait. The downside is that any new shim instance needs to implement bothLibcontainerInstance
and theInstance
traits.This closes #131