feat: Generate talos configs

This commit is contained in:
2026-04-01 06:21:09 +02:00
parent 7a83a8a4be
commit 3c7a29261d
3 changed files with 144 additions and 7 deletions
+67 -5
View File
@@ -1,5 +1,6 @@
use std::net::Ipv4Addr;
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;
use minijinja::Environment;
@@ -9,9 +10,9 @@ use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
use crate::environment::PathEnvironment;
use crate::get_talos_path;
use crate::node::{Node, OptionalNodeDeserialize};
use crate::patch::Patches;
use crate::{get_configs_path, get_talos_path};
#[optional_struct]
#[derive(Debug, Deserialize, JsonSchema, Clone)]
@@ -31,6 +32,16 @@ pub struct Version {
talos: semver::Version,
}
impl Version {
pub(crate) fn kubernetes(&self) -> String {
format!("v{}", self.kubernetes)
}
pub(crate) fn talos(&self) -> String {
format!("v{}", self.talos)
}
}
#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub enum ClusterEnv {
@@ -60,12 +71,12 @@ struct ClusterDeserialize {
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Cluster {
name: String,
version: Version,
pub(crate) name: String,
pub(crate) version: Version,
nodes: Vec<Node>,
cluster_env: ClusterEnv,
control_plane_ip: Ipv4Addr,
secrets_file: PathBuf,
pub(crate) control_plane_ip: Ipv4Addr,
pub(crate) secrets_file: PathBuf,
}
impl Cluster {
@@ -142,6 +153,57 @@ impl Cluster {
pub fn nodes(&self) -> &[Node] {
&self.nodes
}
pub fn talosctl_gen_config_command(&self) -> Command {
let path = get_configs_path().join(&self.name).join("talosconfig");
let mut command = Command::new("talosctl");
command.args([
"gen",
"config",
&self.name,
&format!("https://{}:6443", self.control_plane_ip),
"--with-secrets",
self.secrets_file.to_str().expect("Path should be valid"),
"--output-types",
"talosconfig",
"-o",
path.to_str().expect("Path should be valid utf-8"),
]);
command
}
pub fn talosctl_add_endpoint_command(&self) -> Command {
let path = get_configs_path().join(&self.name).join("talosconfig");
let mut command = Command::new("talosctl");
command.args([
"config",
"--talosconfig",
path.to_str().expect("Path should be valid utf-8"),
"endpoint",
&self.control_plane_ip.to_string(),
]);
command
}
pub fn talosctl_merge_command(&self) -> Command {
let cluster_path = get_configs_path().join(&self.name).join("talosconfig");
let path = get_configs_path().join("talosconfig");
let mut command = Command::new("talosctl");
command.args([
"config",
"--talosconfig",
path.to_str().expect("Path should be valid utf-8"),
"merge",
cluster_path.to_str().expect("Path should be valid utf-8"),
]);
command
}
}
impl JsonSchema for Cluster {