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-04 05:53:45 +02:00
parent 5a54283248
commit b728cd7d22
6 changed files with 100 additions and 18 deletions
+2
View File
@@ -49,4 +49,6 @@ pub enum Commands {
Generate,
/// Generate completions for your current shell
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::path::PathBuf;
use std::str::FromStr;
use async_walkdir::WalkDir;
use futures::StreamExt;
use futures::future::join_all;
use futures::stream::FuturesUnordered;
use minijinja::Environment;
use optional_struct::{Applicable, optional_struct};
@@ -77,7 +77,7 @@ struct ClusterDeserialize {
pub struct Cluster {
pub(crate) name: String,
pub(crate) version: Version,
nodes: Vec<Node>,
nodes: HashMap<String, Node>,
cluster_env: ClusterEnv,
pub(crate) control_plane_ip: Ipv4Addr,
pub(crate) secrets_file: PathBuf,
@@ -122,22 +122,23 @@ impl Cluster {
let cluster = Self {
name: cluster_name.into(),
version: cluster.version,
nodes: vec![],
nodes: Default::default(),
cluster_env: cluster.cluster_env,
control_plane_ip: cluster.control_plane_ip,
secrets_file,
};
let nodes = nodes
.iter()
.map(|name| async { Node::get(name, env, &cluster, &default, &base).await });
let nodes = nodes.iter().map(|name| async {
let node = Node::get(name, env, &cluster, &default, &base).await;
(node.hostname().to_owned(), node)
});
let nodes = FuturesUnordered::from_iter(nodes).collect().await;
Self { nodes, ..cluster }
}
pub async fn get_all() -> Vec<Cluster> {
pub async fn get_all() -> HashMap<String, Cluster> {
let env = PathEnvironment::new_patches();
let names: Vec<_> = WalkDir::new(get_talos_path().join("clusters"))
@@ -162,17 +163,26 @@ impl Cluster {
.await;
// Get the clusters concurrently
join_all(names.iter().map(|name| {
FuturesUnordered::from_iter(names.iter().map(|name| {
let env = env.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
}
pub fn nodes(&self) -> &[Node] {
&self.nodes
pub fn nodes(&self) -> impl Iterator<Item = &Node> {
self.nodes.values()
}
pub fn node(&self, node: &str) -> Option<&Node> {
self.nodes.get(node)
}
pub async fn talosconfig(&self) -> String {
+39 -7
View File
@@ -7,10 +7,12 @@ use clap_complete::{Shell, generate as generate_complete};
use crete::cluster::Cluster;
use crete::environment::PathEnvironment;
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::future::join_all;
use minijinja::context;
use regex::Regex;
use thiserror::Error;
use crate::cli::{Cli, Commands, GlobalOpts};
@@ -19,11 +21,11 @@ use crate::cli::{Cli, Commands, GlobalOpts};
enum Error {
#[error("No clusters where found")]
NoClustersFound,
#[error("Server error: {0}")]
ServerError(#[from] ServerError),
}
async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
set_repo_path(&opts.repo);
let clusters = Cluster::get_all().await;
if clusters.is_empty() {
@@ -44,7 +46,7 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
let content = template
.render(context! {
clusters,
clusters => clusters.values().collect::<Vec<_>>(),
root => opts.repo
})
.unwrap();
@@ -62,8 +64,8 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
tokio::fs::create_dir_all(&path).await.unwrap();
// Generate config files
join_all(clusters.iter().map(|cluster| async move {
join_all(cluster.nodes().iter().map(|node| {
join_all(clusters.values().map(|cluster| async move {
join_all(cluster.nodes().map(|node| {
let cluster = cluster.clone();
async move {
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
// 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;
}
@@ -94,6 +96,8 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
async fn main() -> Result<(), Error> {
let cli = Cli::parse();
set_repo_path(&cli.global_opts.repo);
match cli.command {
Commands::Generate => generate(&cli.global_opts).await?,
Commands::ShellCompletions => generate_complete(
@@ -102,6 +106,34 @@ async fn main() -> Result<(), Error> {
"crete",
&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();
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(())
+4
View File
@@ -178,6 +178,10 @@ impl Node {
Node { patches, ..node }
}
pub fn hostname(&self) -> &str {
&self.hostname
}
pub async fn config(&self, cluster: &Cluster) -> String {
let mut command = Command::new("talosctl");
command.args([