Skip to content

Commit

Permalink
container: Move to its own crate
Browse files Browse the repository at this point in the history
Our container implementation could be used externally.
Moreover, and in order to have a cleaner separation between the kaps CLI
and binary and the container API, we should move the container code into
its own crate.

With that move, kaps becomes a simple CLI consuming the container crate.

Fixes #17

Signed-off-by: Samuel Ortiz <[email protected]>
  • Loading branch information
Samuel Ortiz authored and sameo committed Mar 7, 2022
1 parent 4bf27cc commit 379a9c1
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 19 deletions.
13 changes: 10 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors = ["Polytech Montpellier - DevOps"]

[dependencies]
clap = { version = "3.0.5", features = ["derive"] }
lazy_static = "1.4.0"
oci-spec = "0.5.3"
unshare = { git = "https://github.com/virt-do/unshare", branch = "main" }
container = { path = "container" }

[workspace]
members = [
"container"
]
10 changes: 10 additions & 0 deletions container/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "container"
version = "0.1.0"
edition = "2021"
authors = ["Polytech Montpellier - DevOps"]

[dependencies]
lazy_static = "1.4.0"
oci-spec = "0.5.3"
unshare = { git = "https://github.com/virt-do/unshare", branch = "main" }
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/container/mod.rs → container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::path::PathBuf;

use oci_spec::runtime::Spec;

use crate::container::environment::Environment;
use command::Command;
use environment::Environment;
use mounts::Mounts;
use namespaces::Namespaces;

Expand Down
10 changes: 5 additions & 5 deletions src/container/mounts.rs → container/src/mounts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::container::Error;
use crate::Error;
use std::path::PathBuf;
use std::process::Command;

Expand Down Expand Up @@ -36,7 +36,7 @@ impl Mounts {

/// Cleanup the mounts of a rootfs.
/// This method should be called when a container has ended, to clean up the FS.
pub fn cleanup(&self, rootfs: PathBuf) -> Result<(), crate::container::Error> {
pub fn cleanup(&self, rootfs: PathBuf) -> Result<(), crate::Error> {
for mount in &self.vec {
let mut path = rootfs.clone();
path.push(&mount.source);
Expand All @@ -48,9 +48,9 @@ impl Mounts {
.code()
{
if code != 0 {
return Err(crate::container::Error::Unmount(
std::io::Error::from_raw_os_error(code),
));
return Err(crate::Error::Unmount(std::io::Error::from_raw_os_error(
code,
)));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/container/namespaces.rs → container/src/namespaces.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::container::Error;
use crate::Error;
use oci_spec::runtime::{LinuxNamespace, LinuxNamespaceType};
use unshare::Namespace;

Expand All @@ -16,7 +16,7 @@ impl Namespaces {
/// Convert an `oci_spec::runtime::LinuxNamespaceType` to an `unshare::Namespace`
/// It returns an error if the namespace is invalid, or if it does not match any pattern.
#[allow(unreachable_patterns)]
fn from_oci_namespace(namespace: LinuxNamespaceType) -> crate::container::Result<Namespace> {
fn from_oci_namespace(namespace: LinuxNamespaceType) -> crate::Result<Namespace> {
match namespace {
LinuxNamespaceType::Cgroup => Ok(Namespace::Cgroup),
LinuxNamespaceType::Ipc => Ok(Namespace::Ipc),
Expand Down
6 changes: 3 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use clap::{Parser, Subcommand};
/// CLI related errors
#[derive(Debug)]
pub enum Error {
Run(crate::container::Error),
Run(container::Error),
}

impl From<crate::container::Error> for Error {
fn from(error: crate::container::Error) -> Self {
impl From<container::Error> for Error {
fn from(error: container::Error) -> Self {
Self::Run(error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::container::Container;
use crate::{Handler, Result};
use clap::Args;
use container::Container;

/// Arguments for our `RunCommand`.
///
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use clap::Parser;
use crate::cli::{Cli, Handler, Result};

mod cli;
mod container;

fn main() -> Result<()> {
let cli: Cli = Cli::parse();
Expand Down

0 comments on commit 379a9c1

Please sign in to comment.