fix: Multiple ingress fixes for localk3d, it works nicely now for Application and ntfy at least. Also fix k3d kubeconfig context by force switching to it every time. Not perfect but better and more intuitive for the user to view his resources.
Some checks failed
Run Check Script / check (push) Failing after 18s
Compile and package harmony_composer / package_harmony_composer (push) Successful in 6m32s

This commit is contained in:
2025-09-09 16:41:53 -04:00
parent 21dcb75408
commit 11481b16cd
12 changed files with 80 additions and 24 deletions

View File

@@ -2,8 +2,8 @@ mod downloadable_asset;
use downloadable_asset::*;
use kube::Client;
use log::debug;
use std::path::PathBuf;
use log::{debug, info};
use std::{ffi::OsStr, path::PathBuf};
const K3D_BIN_FILE_NAME: &str = "k3d";
@@ -213,15 +213,19 @@ impl K3d {
}
}
let client;
if !self.is_cluster_initialized() {
debug!("Cluster is not initialized, initializing now");
return self.initialize_cluster().await;
client = self.initialize_cluster().await?;
} else {
self.start_cluster().await?;
debug!("K3d and cluster are already properly set up");
client = self.create_kubernetes_client().await?;
}
self.start_cluster().await?;
debug!("K3d and cluster are already properly set up");
self.create_kubernetes_client().await
self.ensure_k3d_config_is_default(self.get_cluster_name()?)?;
Ok(client)
}
// Private helper methods
@@ -302,7 +306,16 @@ impl K3d {
S: AsRef<std::ffi::OsStr>,
{
let binary_path = self.get_k3d_binary()?;
let output = std::process::Command::new(binary_path).args(args).output();
self.run_command(binary_path, args)
}
pub fn run_command<I, S, C>(&self, cmd: C, args: I) -> Result<std::process::Output, String>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
C: AsRef<OsStr>,
{
let output = std::process::Command::new(cmd).args(args).output();
match output {
Ok(output) => {
let stderr = String::from_utf8_lossy(&output.stderr);
@@ -311,7 +324,7 @@ impl K3d {
debug!("stdout : {}", stdout);
Ok(output)
}
Err(e) => Err(format!("Failed to execute k3d command: {}", e)),
Err(e) => Err(format!("Failed to execute command: {}", e)),
}
}
@@ -323,12 +336,38 @@ impl K3d {
return Err(format!("Failed to create cluster: {}", stderr));
}
debug!("Successfully created k3d cluster '{}'", cluster_name);
info!("Successfully created k3d cluster '{}'", cluster_name);
Ok(())
}
fn ensure_k3d_config_is_default(&self, cluster_name: &str) -> Result<(), String> {
let output = self.run_k3d_command(["kubeconfig", "merge", "-d", cluster_name])?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("Failed to setup k3d kubeconfig : {}", stderr));
}
let output = self.run_command(
"kubectl",
["config", "use-context", &format!("k3d-{cluster_name}")],
)?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!(
"Failed to switch kubectl context to k3d : {}",
stderr
));
}
info!(
"kubectl is now using 'k3d-{}' as default context",
cluster_name
);
Ok(())
}
async fn create_kubernetes_client(&self) -> Result<Client, String> {
// TODO: Connect the client to the right k3d cluster (see https://git.nationtech.io/NationTech/harmony/issues/92)
Client::try_default()
.await
.map_err(|e| format!("Failed to create Kubernetes client: {}", e))