Skip to content

Commit

Permalink
Derive getters if builder is used
Browse files Browse the repository at this point in the history
The idea is that we use the derived getters from the `getset` create if
we use the builder pattern.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert committed Aug 18, 2021
1 parent ed3b45b commit 06d0f77
Show file tree
Hide file tree
Showing 12 changed files with 1,549 additions and 1,275 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -84,7 +84,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --lib --all-features --no-fail-fast
args: --no-fail-fast
env:
CARGO_INCREMENTAL: '0'
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests'
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
[features]
default = []
proptests = ["quickcheck"]
builder = ["derive_builder"]
builder = ["derive_builder", "getset"]

[dependencies]
serde = { version = "1.0.127", features = ["derive"] }
Expand All @@ -17,6 +17,8 @@ serde_json = "1.0.66"
caps = { git = "https://github.com/lucab/caps-rs", rev = "cb54844", features = ["serde_support"] }
quickcheck = { version = "1.0.3", optional = true }
derive_builder = { version = "0.10.2", optional = true }
getset = { version = "0.1.1", optional = true }
cfg-if = "1.0.0"

[dev-dependencies]
tempfile = "3.2.0"
3 changes: 3 additions & 0 deletions src/lib.rs
@@ -1,2 +1,5 @@
#[macro_use]
mod macros;

/// [OCI runtime spec](https://github.com/opencontainers/runtime-spec) types and definitions.
pub mod runtime;
41 changes: 41 additions & 0 deletions src/macros.rs
@@ -0,0 +1,41 @@
#[cfg(not(feature = "builder"))]
macro_rules! make_pub {
{
$(#[$outer:meta])*
struct $name:ident {
$(
$(#[$inner:ident $($args:tt)*])*
$field:ident: $t:ty,
)*
}
} => {
$(#[$outer])*
pub struct $name {
$(
$(#[$inner $($args)*])*
pub $field: $t,
)*
}
}
}

#[cfg(feature = "builder")]
macro_rules! make_pub {
{
$(#[$outer:meta])*
struct $name:ident {
$(
$(#[$inner:ident $($args:tt)*])*
$field:ident: $t:ty,
)*
}
} => {
$(#[$outer])*
pub struct $name {
$(
$(#[$inner $($args)*])*
$field: $t,
)*
}
}
}
153 changes: 81 additions & 72 deletions src/runtime/hooks.rs
@@ -1,85 +1,94 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder),
builder(default, pattern = "owned", setter(into, strip_option))
)]
/// Hooks specifies a command that is run in the container at a particular event in the lifecycle
/// (setup and teardown) of a container.
pub struct Hooks {
#[deprecated(
note = "Prestart hooks were deprecated in favor of `createRuntime`, `createContainer` and `startContainer` hooks"
make_pub!(
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder, getset::Getters),
builder(default, pattern = "owned", setter(into, strip_option)),
getset(get = "pub")
)]
#[serde(default, skip_serializing_if = "Option::is_none")]
/// The `prestart` hooks MUST be called after the `start` operation is called but before the
/// user-specified program command is executed.
///
/// On Linux, for example, they are called after the container namespaces are created, so they
/// provide an opportunity to customize the container (e.g. the network namespace could be
/// specified in this hook).
///
/// The `prestart` hooks' path MUST resolve in the runtime namespace.
/// The `prestart` hooks MUST be executed in the runtime namespace.
pub prestart: Option<Vec<Hook>>,
/// Hooks specifies a command that is run in the container at a particular event in the lifecycle
/// (setup and teardown) of a container.
struct Hooks {
#[deprecated(
note = "Prestart hooks were deprecated in favor of `createRuntime`, `createContainer` and `startContainer` hooks"
)]
#[serde(default, skip_serializing_if = "Option::is_none")]
/// The `prestart` hooks MUST be called after the `start` operation is called but before the
/// user-specified program command is executed.
///
/// On Linux, for example, they are called after the container namespaces are created, so they
/// provide an opportunity to customize the container (e.g. the network namespace could be
/// specified in this hook).
///
/// The `prestart` hooks' path MUST resolve in the runtime namespace.
/// The `prestart` hooks MUST be executed in the runtime namespace.
prestart: Option<Vec<Hook>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// CreateRuntime is a list of hooks to be run after the container has been created but before
/// `pivot_root` or any equivalent operation has been called. It is called in the Runtime
/// Namespace.
pub create_runtime: Option<Vec<Hook>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// CreateRuntime is a list of hooks to be run after the container has been created but before
/// `pivot_root` or any equivalent operation has been called. It is called in the Runtime
/// Namespace.
create_runtime: Option<Vec<Hook>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// CreateContainer is a list of hooks to be run after the container has been created but
/// before `pivot_root` or any equivalent operation has been called. It is called in the
/// Container Namespace.
pub create_container: Option<Vec<Hook>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// CreateContainer is a list of hooks to be run after the container has been created but
/// before `pivot_root` or any equivalent operation has been called. It is called in the
/// Container Namespace.
create_container: Option<Vec<Hook>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// StartContainer is a list of hooks to be run after the start operation is called but before
/// the container process is started. It is called in the Container Namespace.
pub start_container: Option<Vec<Hook>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// StartContainer is a list of hooks to be run after the start operation is called but before
/// the container process is started. It is called in the Container Namespace.
start_container: Option<Vec<Hook>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// Poststart is a list of hooks to be run after the container process is started. It is called
/// in the Runtime Namespace.
pub poststart: Option<Vec<Hook>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Poststart is a list of hooks to be run after the container process is started. It is called
/// in the Runtime Namespace.
poststart: Option<Vec<Hook>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// Poststop is a list of hooks to be run after the container process exits. It is called in
/// the Runtime Namespace.
pub poststop: Option<Vec<Hook>>,
}
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Poststop is a list of hooks to be run after the container process exits. It is called in
/// the Runtime Namespace.
poststop: Option<Vec<Hook>>,
}
);

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder),
builder(default, pattern = "owned", setter(into, strip_option))
)]
/// Hook specifies a command that is run at a particular event in the lifecycle of a container.
pub struct Hook {
/// Path to the binary to be executed. Following similar semantics to [IEEE Std 1003.1-2008
/// `execv`'s path](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html). This
/// specification extends the IEEE standard in that path MUST be absolute.
pub path: PathBuf,
make_pub!(
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder, getset::CopyGetters, getset::Getters),
builder(default, pattern = "owned", setter(into, strip_option))
)]
/// Hook specifies a command that is run at a particular event in the lifecycle of a container.
struct Hook {
#[cfg_attr(feature = "builder", getset(get = "pub"))]
/// Path to the binary to be executed. Following similar semantics to [IEEE Std 1003.1-2008
/// `execv`'s path](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html). This
/// specification extends the IEEE standard in that path MUST be absolute.
path: PathBuf,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// Arguments used for the binary, including the binary name itself. Following the same
/// semantics as [IEEE Std 1003.1-2008 `execv`'s
/// argv](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html).
pub args: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "builder", getset(get = "pub"))]
/// Arguments used for the binary, including the binary name itself. Following the same
/// semantics as [IEEE Std 1003.1-2008 `execv`'s
/// argv](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html).
args: Option<Vec<String>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// Additional `key=value` environment variables. Following the same semantics as [IEEE Std
/// 1003.1-2008's `environ`](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_01).
pub env: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "builder", getset(get = "pub"))]
/// Additional `key=value` environment variables. Following the same semantics as [IEEE Std
/// 1003.1-2008's `environ`](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_01).
env: Option<Vec<String>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// Timeout is the number of seconds before aborting the hook. If set, timeout MUST be greater
/// than zero.
pub timeout: Option<i64>,
}
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "builder", getset(get_copy = "pub"))]
/// Timeout is the number of seconds before aborting the hook. If set, timeout MUST be greater
/// than zero.
timeout: Option<i64>,
}
);

0 comments on commit 06d0f77

Please sign in to comment.