use engenho_control_types::types;
use crate::child::{Child, DeathCause, Driver, Listener, Respawn};
use crate::control::reinit::ReinitOp;
use crate::layout::Area;
macro_rules! driver_names {
($($variant:ident),* $(,)?) => {
#[must_use]
pub const fn driver_to_wire(driver: Driver) -> types::DriverName {
match driver { $(Driver::$variant => types::DriverName::$variant),* }
}
#[must_use]
pub const fn driver_from_wire(name: types::DriverName) -> Driver {
match name { $(types::DriverName::$variant => Driver::$variant),* }
}
#[must_use]
pub const fn child_to_wire(child: Child) -> types::ChildName {
match child {
$(Child::Driver(Driver::$variant) => types::ChildName::$variant,)*
Child::Listener(Listener::KubeletHttp) => types::ChildName::KubeletHttp,
Child::Listener(Listener::EtcdFacade) => types::ChildName::EtcdFacade,
Child::NodeLease => types::ChildName::NodeLease,
}
}
#[must_use]
pub const fn child_from_wire(name: types::ChildName) -> Child {
match name {
$(types::ChildName::$variant => Child::Driver(Driver::$variant),)*
types::ChildName::KubeletHttp => Child::Listener(Listener::KubeletHttp),
types::ChildName::EtcdFacade => Child::Listener(Listener::EtcdFacade),
types::ChildName::NodeLease => Child::NodeLease,
}
}
};
}
driver_names!(
Deployment,
ReplicaSet,
StatefulSet,
DaemonSet,
Job,
CronJob,
PodDisruptionBudget,
Endpoints,
ServiceRouting,
Gc,
Namespace,
PvBinder,
VolumeSnapshot,
PvcProtection,
Crd,
Scheduler,
ServedCapability,
NetworkPolicy,
CsiRegistrar,
CniStatus,
NodeManifests,
Kubelet,
);
#[must_use]
pub const fn child_kind(child: Child) -> types::ChildKind {
match child {
Child::Driver(_) => types::ChildKind::Driver,
Child::Listener(_) => types::ChildKind::Listener,
Child::NodeLease => types::ChildKind::NodeLease,
}
}
#[must_use]
pub const fn death_to_wire(cause: DeathCause) -> types::DeathCause {
match cause {
DeathCause::Panicked => types::DeathCause::Panicked,
DeathCause::Cancelled => types::DeathCause::Cancelled,
}
}
#[must_use]
pub const fn respawn_to_wire(respawn: Respawn) -> types::RespawnClass {
match respawn {
Respawn::Rebuild => types::RespawnClass::Rebuild,
Respawn::RebuildWith(_) => types::RespawnClass::RebuildWith,
Respawn::RuntimeRestartOnly => types::RespawnClass::RuntimeRestartOnly,
}
}
#[must_use]
pub const fn reinit_op_to_wire(op: ReinitOp) -> types::ReinitOp {
match op {
ReinitOp::RotateAdminToken => types::ReinitOp::RotateAdminToken,
ReinitOp::ReseedPki => types::ReinitOp::ReseedPki,
ReinitOp::WipeStore => types::ReinitOp::WipeStore,
ReinitOp::RotateControlIdentity => types::ReinitOp::RotateControlIdentity,
}
}
#[must_use]
pub const fn area_to_wire(area: Area) -> types::LayoutName {
match area {
Area::Store => types::LayoutName::Store,
Area::Pki => types::LayoutName::Pki,
Area::LocalPath => types::LayoutName::LocalPath,
Area::Volumes => types::LayoutName::Volumes,
Area::Plugins => types::LayoutName::Plugins,
Area::Pods => types::LayoutName::Pods,
Area::Snapshots => types::LayoutName::Snapshots,
Area::Control => types::LayoutName::Control,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_child_round_trips_through_its_api_name() {
for child in Child::all() {
assert_eq!(child_from_wire(child_to_wire(child)), child);
}
for driver in Driver::ALL {
assert_eq!(driver_from_wire(driver_to_wire(*driver)), *driver);
}
assert_eq!(
child_to_wire(Child::Driver(Driver::PodDisruptionBudget)).to_string(),
"pod_disruption_budget"
);
assert_eq!(child_to_wire(Child::NodeLease).to_string(), "node_lease");
}
}