feat: update ensure_ready to check helm is available (#17)

I want to make sure the changes I'm working on in the ensure_ready don't break anything

Reviewed-on: https://git.nationtech.io/NationTech/harmony/pulls/17
Reviewed-by: taha <taha@noreply.git.nationtech.io>
Co-authored-by: Willem <wrolleman@nationtech.io>
Co-committed-by: Willem <wrolleman@nationtech.io>
This commit is contained in:
Willem 2025-04-24 15:51:28 +00:00 committed by taha
parent ad1aa897b1
commit 6c06a4ae07

View File

@ -1,3 +1,5 @@
use std::process::Command;
use async_trait::async_trait;
use inquire::Confirm;
use log::{info, warn};
@ -11,7 +13,7 @@ use crate::{
topology::LocalhostTopology,
};
use super::{Topology, k8s::K8sClient};
use super::{HelmCommand, Topology, k8s::K8sClient};
struct K8sState {
_client: K8sClient,
@ -28,6 +30,29 @@ pub struct K8sAnywhereTopology {
}
impl K8sAnywhereTopology {
pub fn new() -> Self {
Self {
k8s_state: OnceCell::new(),
}
}
fn is_helm_available(&self) -> Result<(), String> {
let version_result = Command::new("helm")
.arg("version")
.output()
.map_err(|e| format!("Failed to execute 'helm -version': {}", e))?;
if !version_result.status.success() {
return Err("Failed to run 'helm -version'".to_string());
}
// Print the version output
let version_output = String::from_utf8_lossy(&version_result.stdout);
println!("Helm version: {}", version_output.trim());
Ok(())
}
async fn try_load_system_kubeconfig(&self) -> Option<K8sClient> {
todo!("Use kube-rs default behavior to load system kubeconfig");
}
@ -126,15 +151,25 @@ impl Topology for K8sAnywhereTopology {
}
async fn ensure_ready(&self) -> Result<Outcome, InterpretError> {
match self
let k8s_state = self
.k8s_state
.get_or_try_init(|| self.try_get_or_install_k8s_client())
.await?
{
Some(k8s_state) => Ok(Outcome::success(k8s_state.message.clone())),
None => Err(InterpretError::new(
.await?;
let k8s_state: &K8sState = k8s_state
.as_ref()
.ok_or(InterpretError::new(
"No K8s client could be found or installed".to_string(),
)),
))?;
match self.is_helm_available() {
Ok(()) => Ok(Outcome::success(format!(
"{} + helm available",
k8s_state.message.clone()
))),
Err(_) => Err(InterpretError::new("helm unavailable".to_string())),
}
}
}
impl HelmCommand for K8sAnywhereTopology {}