use minijinja::{Environment, context}; use optional_struct::optional_struct; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::cluster::Cluster; use crate::node::Node; #[optional_struct] #[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, PartialEq, Eq, Default)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub(crate) struct Patches { pub(crate) all: Vec, pub(crate) control_plane: Vec, } pub(crate) type OptionalPatchesString = OptionalPatches; fn render( patches: Vec, env: &Environment, cluster: &Cluster, node: &Node, ) -> Vec { patches .into_iter() .map(|name| { let content = env .get_template(&name) .unwrap() .render(context! { node, cluster }) .unwrap(); serde_yaml::from_str(&content).unwrap() }) .collect() } impl Patches { pub(crate) fn extend(mut self, other: Self) -> Self { self.all.extend(other.all); self.control_plane.extend(other.control_plane); Self { all: self.all, control_plane: self.control_plane, } } pub(crate) fn render( self, env: &Environment, cluster: &Cluster, node: &Node, ) -> Patches { Patches { all: render(self.all.clone(), env, cluster, node), control_plane: render(self.control_plane.clone(), env, cluster, node), } } }