refactor: Big internal refactor
This commit is contained in:
+76
-34
@@ -5,9 +5,9 @@ use optional_struct::{Applicable, optional_struct};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::cluster::Cluster;
|
||||
use crate::get_talos_config_path;
|
||||
use crate::patch::{OptionalPatches, Patches};
|
||||
use crate::cluster::{Base, Cluster};
|
||||
use crate::get_talos_path;
|
||||
use crate::patch::{OptionalPatches, OptionalPatchesString, Patches};
|
||||
use crate::schematic::Schematic;
|
||||
use crate::secret::Secret;
|
||||
|
||||
@@ -19,6 +19,15 @@ enum NodeType {
|
||||
ControlPlane,
|
||||
}
|
||||
|
||||
impl From<NodeType> for &str {
|
||||
fn from(value: NodeType) -> Self {
|
||||
match value {
|
||||
NodeType::Worker => "worker",
|
||||
NodeType::ControlPlane => "controlplane",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Copy, PartialEq, Eq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum NodeArch {
|
||||
@@ -59,11 +68,9 @@ struct Install {
|
||||
}
|
||||
|
||||
#[optional_struct]
|
||||
#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Deserialize, JsonSchema, Clone, PartialEq, Eq)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Node {
|
||||
#[serde(skip_deserializing)]
|
||||
hostname: String,
|
||||
pub(crate) struct NodeDeserialize {
|
||||
arch: NodeArch,
|
||||
schematic: Schematic,
|
||||
r#type: NodeType,
|
||||
@@ -75,64 +82,99 @@ pub struct Node {
|
||||
#[optional_wrap]
|
||||
install: Install,
|
||||
kernel_args: Vec<String>,
|
||||
#[optional_rename(OptionalPatches)]
|
||||
#[optional_rename(OptionalPatchesString)]
|
||||
#[optional_wrap]
|
||||
pub(crate) patches: Patches,
|
||||
patches: Patches<String>,
|
||||
sops: Secret,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Node {
|
||||
hostname: String,
|
||||
arch: NodeArch,
|
||||
schematic: Schematic,
|
||||
r#type: NodeType,
|
||||
network: Network,
|
||||
ntp: String,
|
||||
install: Install,
|
||||
kernel_args: Vec<String>,
|
||||
patches: Patches<serde_yaml::Value>,
|
||||
sops: Secret,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn get(node_name: &str, env: &Environment, cluster: &Cluster) -> Self {
|
||||
let mut path = get_talos_config_path().join("nodes").join(node_name);
|
||||
let named = OptionalNode {
|
||||
hostname: Some(
|
||||
path.file_name()
|
||||
.expect("Path should be valid")
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
),
|
||||
..OptionalNode::default()
|
||||
};
|
||||
pub(crate) fn get(
|
||||
node_name: &str,
|
||||
env: &Environment,
|
||||
cluster: &Cluster,
|
||||
default: &OptionalNodeDeserialize,
|
||||
base: &Base,
|
||||
) -> Self {
|
||||
let mut path = get_talos_path().join("nodes").join(node_name);
|
||||
let hostname = path
|
||||
.file_name()
|
||||
.expect("Path should be valid")
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
path.add_extension("yaml");
|
||||
let content = std::fs::read_to_string(path).unwrap();
|
||||
|
||||
let node: OptionalNode = serde_yaml::from_str(&content).unwrap();
|
||||
let node: OptionalNodeDeserialize = serde_yaml::from_str(&content).unwrap();
|
||||
|
||||
// We want all vectors to be empty vectors by default
|
||||
// Sadly we have to this manually
|
||||
// TODO: Find a better way of doing this
|
||||
let default = OptionalNode {
|
||||
let default = OptionalNodeDeserialize {
|
||||
patches: Some(OptionalPatches {
|
||||
all: Some(vec![]),
|
||||
control_plane: Some(vec![]),
|
||||
}),
|
||||
kernel_args: vec![].into(),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
.apply(default.clone());
|
||||
|
||||
// Combine all the optional node parts into complete struct
|
||||
let mut node: Node = default
|
||||
// Apply cluster default settings
|
||||
.apply(cluster.default.clone())
|
||||
// Apply hostname based on filename
|
||||
.apply(named)
|
||||
let node: NodeDeserialize = default
|
||||
// Override node specific settings
|
||||
.apply(node)
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
// Prepend the cluster base values
|
||||
let mut kernel_args = cluster.base.kernel_args.clone();
|
||||
let mut kernel_args = base.kernel_args.clone();
|
||||
kernel_args.extend(node.kernel_args);
|
||||
node.kernel_args = kernel_args;
|
||||
|
||||
let patches = cluster.base.patches.clone().extend(node.patches);
|
||||
node.patches = patches;
|
||||
let patches = base.patches.clone().extend(node.patches);
|
||||
|
||||
let node = Node {
|
||||
hostname,
|
||||
arch: node.arch,
|
||||
schematic: node.schematic,
|
||||
r#type: node.r#type,
|
||||
network: node.network,
|
||||
ntp: node.ntp,
|
||||
install: node.install,
|
||||
kernel_args,
|
||||
patches: Default::default(),
|
||||
sops: node.sops,
|
||||
};
|
||||
|
||||
// Render patches
|
||||
node.patches = node.patches.clone().render(env, cluster, &node);
|
||||
let patches = patches.render(env, cluster, &node);
|
||||
|
||||
node
|
||||
Node { patches, ..node }
|
||||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for Node {
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
"Node".into()
|
||||
}
|
||||
|
||||
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
|
||||
OptionalNodeDeserialize::json_schema(generator)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user