feat!: Added command to serve config files over tftp

This is a breaking change as the return type of cluster.nodes has
changed, resulting in old templates potentially no longer working.
This commit is contained in:
2026-10-03 21:33:35 +02:00
parent ffeb39923e
commit 16d1389707
6 changed files with 101 additions and 17 deletions
Generated
+33
View File
@@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "aho-corasick"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "anstream" name = "anstream"
version = "1.0.0" version = "1.0.0"
@@ -299,6 +308,7 @@ dependencies = [
"netascii", "netascii",
"num_enum", "num_enum",
"optional_struct", "optional_struct",
"regex",
"reqwest", "reqwest",
"schemars", "schemars",
"semver", "semver",
@@ -1511,11 +1521,34 @@ dependencies = [
"syn 3.0.4", "syn 3.0.4",
] ]
[[package]]
name = "regex"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.4.18" version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
[[package]] [[package]]
name = "repo_path" name = "repo_path"
+1
View File
@@ -24,6 +24,7 @@ minijinja = { version = "2.18.0", features = ["json", "loader"] }
netascii = "0.1.0" netascii = "0.1.0"
num_enum = "0.7.6" num_enum = "0.7.6"
optional_struct = "0.5.2" optional_struct = "0.5.2"
regex = "1.13.1"
reqwest = { version = "0.13.2", default-features = false, features = [ reqwest = { version = "0.13.2", default-features = false, features = [
"blocking", "blocking",
"stream", "stream",
+2
View File
@@ -49,4 +49,6 @@ pub enum Commands {
Generate, Generate,
/// Generate completions for your current shell /// Generate completions for your current shell
ShellCompletions, ShellCompletions,
/// Serve config files over tftp
Serve,
} }
+21 -11
View File
@@ -1,10 +1,10 @@
use std::collections::HashMap;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use async_walkdir::WalkDir; use async_walkdir::WalkDir;
use futures::StreamExt; use futures::StreamExt;
use futures::future::join_all;
use futures::stream::FuturesUnordered; use futures::stream::FuturesUnordered;
use minijinja::Environment; use minijinja::Environment;
use optional_struct::{Applicable, optional_struct}; use optional_struct::{Applicable, optional_struct};
@@ -77,7 +77,7 @@ struct ClusterDeserialize {
pub struct Cluster { pub struct Cluster {
pub(crate) name: String, pub(crate) name: String,
pub(crate) version: Version, pub(crate) version: Version,
nodes: Vec<Node>, nodes: HashMap<String, Node>,
cluster_env: ClusterEnv, cluster_env: ClusterEnv,
pub(crate) control_plane_ip: Ipv4Addr, pub(crate) control_plane_ip: Ipv4Addr,
pub(crate) secrets_file: PathBuf, pub(crate) secrets_file: PathBuf,
@@ -122,22 +122,23 @@ impl Cluster {
let cluster = Self { let cluster = Self {
name: cluster_name.into(), name: cluster_name.into(),
version: cluster.version, version: cluster.version,
nodes: vec![], nodes: Default::default(),
cluster_env: cluster.cluster_env, cluster_env: cluster.cluster_env,
control_plane_ip: cluster.control_plane_ip, control_plane_ip: cluster.control_plane_ip,
secrets_file, secrets_file,
}; };
let nodes = nodes let nodes = nodes.iter().map(|name| async {
.iter() let node = Node::get(name, env, &cluster, &default, &base).await;
.map(|name| async { Node::get(name, env, &cluster, &default, &base).await }); (node.hostname().to_owned(), node)
});
let nodes = FuturesUnordered::from_iter(nodes).collect().await; let nodes = FuturesUnordered::from_iter(nodes).collect().await;
Self { nodes, ..cluster } Self { nodes, ..cluster }
} }
pub async fn get_all() -> Vec<Cluster> { pub async fn get_all() -> HashMap<String, Cluster> {
let env = PathEnvironment::new_patches(); let env = PathEnvironment::new_patches();
let names: Vec<_> = WalkDir::new(get_talos_path().join("clusters")) let names: Vec<_> = WalkDir::new(get_talos_path().join("clusters"))
@@ -162,17 +163,26 @@ impl Cluster {
.await; .await;
// Get the clusters concurrently // Get the clusters concurrently
join_all(names.iter().map(|name| { FuturesUnordered::from_iter(names.iter().map(|name| {
let env = env.clone(); let env = env.clone();
let name = name.clone(); let name = name.clone();
async move { Cluster::get(&name, &env).await } async move {
let cluster = Cluster::get(&name, &env).await;
(name, cluster)
}
})) }))
.collect()
.await .await
} }
pub fn nodes(&self) -> &[Node] { pub fn nodes(&self) -> impl Iterator<Item = &Node> {
&self.nodes self.nodes.values()
}
pub fn node(&self, node: &str) -> Option<&Node> {
self.nodes.get(node)
} }
pub async fn talosconfig(&self) -> String { pub async fn talosconfig(&self) -> String {
+40 -6
View File
@@ -7,10 +7,12 @@ use clap_complete::{Shell, generate as generate_complete};
use crete::cluster::Cluster; use crete::cluster::Cluster;
use crete::environment::PathEnvironment; use crete::environment::PathEnvironment;
use crete::talosconfig::{get_current_context, set_context}; use crete::talosconfig::{get_current_context, set_context};
use crete::{get_configs_path, get_repo_path, set_repo_path}; use crete::tftp::ServerError;
use crete::{get_configs_path, get_repo_path, set_repo_path, tftp};
use futures::StreamExt; use futures::StreamExt;
use futures::future::join_all; use futures::future::join_all;
use minijinja::context; use minijinja::context;
use regex::Regex;
use thiserror::Error; use thiserror::Error;
use crate::cli::{Cli, Commands, GlobalOpts}; use crate::cli::{Cli, Commands, GlobalOpts};
@@ -19,11 +21,11 @@ use crate::cli::{Cli, Commands, GlobalOpts};
enum Error { enum Error {
#[error("No clusters where found")] #[error("No clusters where found")]
NoClustersFound, NoClustersFound,
#[error("Server error: {0}")]
ServerError(#[from] ServerError),
} }
async fn generate(opts: &GlobalOpts) -> Result<(), Error> { async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
set_repo_path(&opts.repo);
let clusters = Cluster::get_all().await; let clusters = Cluster::get_all().await;
if clusters.is_empty() { if clusters.is_empty() {
@@ -62,8 +64,8 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
tokio::fs::create_dir_all(&path).await.unwrap(); tokio::fs::create_dir_all(&path).await.unwrap();
// Generate config files // Generate config files
join_all(clusters.iter().map(|cluster| async move { join_all(clusters.values().map(|cluster| async move {
join_all(cluster.nodes().iter().map(|node| { join_all(cluster.nodes().map(|node| {
let cluster = cluster.clone(); let cluster = cluster.clone();
async move { async move {
node.config_write(&cluster).await; node.config_write(&cluster).await;
@@ -79,7 +81,7 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
// Merge all the cluster talosconfig files into a single file // Merge all the cluster talosconfig files into a single file
// Since this all modifies the same output file this has to happen sequentially // Since this all modifies the same output file this has to happen sequentially
for cluster in clusters { for (_, cluster) in clusters {
cluster.talosconfig_merge().await; cluster.talosconfig_merge().await;
} }
@@ -94,6 +96,8 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
async fn main() -> Result<(), Error> { async fn main() -> Result<(), Error> {
let cli = Cli::parse(); let cli = Cli::parse();
set_repo_path(&cli.global_opts.repo);
match cli.command { match cli.command {
Commands::Generate => generate(&cli.global_opts).await?, Commands::Generate => generate(&cli.global_opts).await?,
Commands::ShellCompletions => generate_complete( Commands::ShellCompletions => generate_complete(
@@ -102,6 +106,36 @@ async fn main() -> Result<(), Error> {
"crete", "crete",
&mut std::io::stdout(), &mut std::io::stdout(),
), ),
Commands::Serve => {
let re = Regex::new(r"talos/(?<cluster>.+)/(?<node>.+)\.ya?ml")
.expect("regex should be valid");
tftp::serve(move |filename| {
let filename = filename.to_owned();
let re = re.clone();
println!("{filename}");
async move {
if let Some(caps) = re.captures(&filename) {
let cluster = &caps["cluster"];
let node = &caps["node"];
let clusters = Cluster::get_all().await;
let cluster = clusters.get(cluster)?;
let node = cluster.node(node)?;
let config = node.config(cluster).await;
Some(config.into())
} else {
None
}
}
})
.await?
}
}; };
Ok(()) Ok(())
+4
View File
@@ -178,6 +178,10 @@ impl Node {
Node { patches, ..node } Node { patches, ..node }
} }
pub fn hostname(&self) -> &str {
&self.hostname
}
pub async fn config(&self, cluster: &Cluster) -> String { pub async fn config(&self, cluster: &Cluster) -> String {
let mut command = Command::new("talosctl"); let mut command = Command::new("talosctl");
command.args([ command.args([