-
Notifications
You must be signed in to change notification settings - Fork 346
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
Modularize container creation #121
Changes from all commits
3d509e0
8a13f82
9e7b13c
a46faff
a387a43
53c9b73
5146d0e
9be94d9
1643dd2
d2ff085
0eaa906
e76360b
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use crate::command::linux::LinuxSyscall; | ||
use std::path::PathBuf; | ||
|
||
use super::{init_builder::InitContainerBuilder, tenant_builder::TenantContainerBuilder}; | ||
pub struct ContainerBuilder { | ||
pub(super) container_id: String, | ||
|
||
pub(super) root_path: PathBuf, | ||
|
||
pub(super) syscall: LinuxSyscall, | ||
|
||
pub(super) pid_file: Option<PathBuf>, | ||
|
||
pub(super) console_socket: Option<PathBuf>, | ||
} | ||
|
||
/// Builder that can be used to configure the common properties of | ||
/// either a init or a tenant container | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// use youki::container::builder::ContainerBuilder; | ||
/// | ||
/// ContainerBuilder::new("74f1a4cb3801".to_owned()) | ||
/// .with_root_path("/run/containers/youki") | ||
/// .with_pid_file("/var/run/docker.pid") | ||
/// .with_console_socket("/var/run/docker/sock.tty") | ||
/// .as_init("/var/run/docker/bundle") | ||
/// .build(); | ||
/// ``` | ||
impl ContainerBuilder { | ||
/// Generates the base configuration for a container which can be | ||
/// transformed into either a init container or a tenant container | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// use youki::container::builder::ContainerBuilder; | ||
/// | ||
/// let builder = ContainerBuilder::new("74f1a4cb3801".to_owned()); | ||
/// ``` | ||
pub fn new(container_id: String) -> Self { | ||
let root_path = PathBuf::from("/run/youki"); | ||
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. It may be better to use Default Trait. 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. Also, it's better to use Default for other builders as well. 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. The default trait does not allow to specify arguments. I could do
instead of with new(id) and generate an id automatically if none is specified in with_id if you like. The TenantContainerBuilder and InitContainerBuilder are not intended to be instantiated directly but by calling as_tenant/is_tenant on the ContainerBuilder, so you do not really have a default value for them because you always have a builder that will be provided in new(...) 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 was planning to use default() in new, and I thought default would be better to use in case there are more constructor functions of different types. 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. We only have one constructor function at the moment, so I think we should do a default implementation when we start to get multiple. |
||
|
||
Self { | ||
container_id, | ||
root_path, | ||
syscall: LinuxSyscall, | ||
pid_file: None, | ||
console_socket: None, | ||
} | ||
} | ||
|
||
/// Transforms this builder into a tenant builder | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// # use youki::container::builder::ContainerBuilder; | ||
/// | ||
/// ContainerBuilder::new("74f1a4cb3801".to_owned()) | ||
/// .as_tenant() | ||
/// .with_container_command(vec!["sleep".to_owned(), "9001".to_owned()]) | ||
/// .build(); | ||
/// ``` | ||
#[allow(clippy::wrong_self_convention)] | ||
pub fn as_tenant(self) -> TenantContainerBuilder { | ||
TenantContainerBuilder::new(self) | ||
} | ||
|
||
/// Transforms this builder into an init builder | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// # use youki::container::builder::ContainerBuilder; | ||
/// | ||
/// ContainerBuilder::new("74f1a4cb3801".to_owned()) | ||
/// .as_init("/var/run/docker/bundle") | ||
/// .with_systemd(false) | ||
/// .build(); | ||
/// ``` | ||
#[allow(clippy::wrong_self_convention)] | ||
pub fn as_init<P: Into<PathBuf>>(self, bundle: P) -> InitContainerBuilder { | ||
InitContainerBuilder::new(self, bundle.into()) | ||
} | ||
|
||
/// Sets the root path which will be used to store the container state | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// # use youki::container::builder::ContainerBuilder; | ||
/// | ||
/// ContainerBuilder::new("74f1a4cb3801".to_owned()) | ||
/// .with_root_path("/run/containers/youki"); | ||
/// ``` | ||
pub fn with_root_path<P: Into<PathBuf>>(mut self, path: P) -> Self { | ||
self.root_path = path.into(); | ||
self | ||
} | ||
|
||
/// Sets the pid file which will be used to write the pid of the container | ||
/// process | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// # use youki::container::builder::ContainerBuilder; | ||
/// | ||
/// ContainerBuilder::new("74f1a4cb3801".to_owned()) | ||
/// .with_pid_file("/var/run/docker.pid"); | ||
/// ``` | ||
pub fn with_pid_file<P: Into<PathBuf>>(mut self, path: P) -> Self { | ||
self.pid_file = Some(path.into()); | ||
self | ||
} | ||
|
||
/// Sets the console socket, which will be used to send the file descriptor | ||
/// of the pseudoterminal | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// # use youki::container::builder::ContainerBuilder; | ||
/// | ||
/// ContainerBuilder::new("74f1a4cb3801".to_owned()) | ||
/// .with_console_socket("/var/run/docker/sock.tty"); | ||
/// ``` | ||
pub fn with_console_socket<P: Into<PathBuf>>(mut self, path: P) -> Self { | ||
self.console_socket = Some(path.into()); | ||
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.
👍