Skip to content
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

Merged
merged 12 commits into from
Aug 14, 2023

Conversation

Mossaka
Copy link
Member

@Mossaka Mossaka commented Aug 3, 2023

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 existing Instance trait. The downside is that any new shim instance needs to implement both LibcontainerInstance and the Instance traits.

This closes #131

…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>
@Mossaka Mossaka requested review from utam0k and cpuguy83 August 3, 2023 01:34
Mossaka added 2 commits August 3, 2023 01:36
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
@jsturtevant
Copy link
Contributor

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 YoukiInstance then we won't be able to add the windows support at all since we take a hard dependecy on libcontainer , where as right now the youki work can be hidden behind some compile time flags.

@Mossaka
Copy link
Member Author

Mossaka commented Aug 4, 2023

@jsturtevant I can make libcontainer dependency completely optional in containerd-shim-wasm. At least this will unblock you from the Windows support work, but you still need to implement non-youki version for the wasmtime shim that does not dep on libcontainer.

@jsturtevant
Copy link
Contributor

but you still need to implement non-youki version for the wasmtime shim that does not dep on libcontainer.

was hoping to share more but with the direction towards youki, in the short term this is probably the best option.

@jsturtevant
Copy link
Contributor

I can make libcontainer dependency completely optional in containerd-shim-wasm.

👍

Mossaka added 2 commits August 4, 2023 23:35
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
Copy link
Collaborator

@jprendes jprendes left a 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?

crates/containerd-shim-wasm/Cargo.toml Outdated Show resolved Hide resolved
crates/containerd-shim-wasmedge/Cargo.toml Outdated Show resolved Hide resolved
crates/containerd-shim-wasm/src/sandbox/instance.rs Outdated Show resolved Hide resolved
crates/containerd-shim-wasmedge/src/instance.rs Outdated Show resolved Hide resolved
crates/containerd-shim-wasmedge/src/instance.rs Outdated Show resolved Hide resolved
crates/containerd-shim-wasmedge/src/instance.rs Outdated Show resolved Hide resolved
crates/containerd-shim-wasm/Cargo.toml Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
crates/containerd-shim-wasm/src/sandbox/instance.rs Outdated Show resolved Hide resolved
Mossaka added 4 commits August 9, 2023 22:47
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>
Copy link
Collaborator

@jprendes jprendes left a 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 !

crates/containerd-shim-wasm/src/sandbox/youki_instance.rs Outdated Show resolved Hide resolved
crates/containerd-shim-wasm/src/sandbox/youki_instance.rs Outdated Show resolved Hide resolved
crates/containerd-shim-wasm/src/sandbox/mod.rs Outdated Show resolved Hide resolved
crates/containerd-shim-wasm/src/sandbox/youki_instance.rs Outdated Show resolved Hide resolved
type E: Send + Sync + Clone;

/// Create a new instance
fn new_youki(id: String, cfg: Option<&InstanceConfig<Self::E>>) -> Self;
Copy link
Collaborator

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 { ... }

Copy link
Member Author

@Mossaka Mossaka Aug 10, 2023

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.

Copy link
Collaborator

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.

Copy link
Contributor

@devigned devigned left a 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.

crates/containerd-shim-wasm/src/sandbox/youki_instance.rs Outdated Show resolved Hide resolved
.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);
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Collaborator

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.

Copy link
Contributor

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.

#240 is similar to what you reference here, thought I am not sure what RAII is

Copy link
Collaborator

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.

Copy link
Contributor

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 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -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 }
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Contributor

@jsturtevant jsturtevant left a 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>
@Mossaka
Copy link
Member Author

Mossaka commented Aug 10, 2023

But there's a blanket implementation of Instance for YoukiInstance, so there's no need to implement both traits, or am I missing something?

Ah that was a mistaken. I used a different implementation so that shim author only needs to implement LibcontainerInstance trait now.

@Mossaka Mossaka changed the title refactor: adding YoukiInstance for default implementation of Instance trait refactor: adding LibcontainerInstance for default implementation of Instance trait Aug 11, 2023
Copy link
Contributor

@jsturtevant jsturtevant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jsturtevant
Copy link
Contributor

not sure what our current set up is in regards to CI and commits, but maybe squash the commits?

Copy link
Collaborator

@jprendes jprendes left a 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut ec = lock.lock().unwrap();
let mut ec = lock.lock().map_err(|err| Error::Any(anyhow::anyhow!("failed get a lockr: {}", err)))?;

Copy link
Member Author

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.

Copy link
Member

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.

Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
@Mossaka
Copy link
Member Author

Mossaka commented Aug 14, 2023

Going to merge this in. Thanks everyone for reviewing it!! Really appreciate it.

@Mossaka Mossaka merged commit d04bf6b into containerd:main Aug 14, 2023
@Mossaka Mossaka deleted the youki-containerd-shim-wasm branch August 14, 2023 18:11
Copy link
Member

@cpuguy83 cpuguy83 left a 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>;
Copy link
Member

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?

Copy link
Collaborator

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();
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use youki libcontainer crate for all shims
6 participants