Files
crete/src/patch.rs
T

74 lines
1.9 KiB
Rust

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<T> {
pub(crate) all: Vec<T>,
pub(crate) control_plane: Vec<T>,
}
pub(crate) type OptionalPatchesString = OptionalPatches<String>;
async fn render(
patches: Vec<String>,
env: &Environment<'static>,
cluster: &Cluster,
node: &Node,
) -> Vec<serde_yaml::Value> {
let env = env.clone();
let cluster = cluster.clone();
let node = node.clone();
// Minijinja will still have blocking I/O internally
tokio::task::spawn_blocking(move || {
patches
.into_iter()
.map(move |name| {
let content = env
.get_template(&name)
.unwrap()
.render(context! {
node,
cluster
})
.unwrap();
serde_yaml::from_str(&content).unwrap()
})
.collect()
})
.await
.unwrap()
}
impl Patches<String> {
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) async fn render(
self,
env: &Environment<'static>,
cluster: &Cluster,
node: &Node,
) -> Patches<serde_yaml::Value> {
Patches {
all: render(self.all.clone(), env, cluster, node).await,
control_plane: render(self.control_plane.clone(), env, cluster, node).await,
}
}
}