wip: working on ensure_ready to check helm is available

This commit is contained in:
Willem 2025-04-24 11:09:25 -04:00
parent ad1aa897b1
commit c00eadca5d

View File

@ -1,3 +1,5 @@
use std::process::Command;
use async_trait::async_trait; use async_trait::async_trait;
use inquire::Confirm; use inquire::Confirm;
use log::{info, warn}; use log::{info, warn};
@ -11,7 +13,7 @@ use crate::{
topology::LocalhostTopology, topology::LocalhostTopology,
}; };
use super::{Topology, k8s::K8sClient}; use super::{HelmCommand, Topology, k8s::K8sClient};
struct K8sState { struct K8sState {
_client: K8sClient, _client: K8sClient,
@ -28,6 +30,29 @@ pub struct K8sAnywhereTopology {
} }
impl 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> { async fn try_load_system_kubeconfig(&self) -> Option<K8sClient> {
todo!("Use kube-rs default behavior to load system kubeconfig"); todo!("Use kube-rs default behavior to load system kubeconfig");
} }
@ -126,15 +151,21 @@ impl Topology for K8sAnywhereTopology {
} }
async fn ensure_ready(&self) -> Result<Outcome, InterpretError> { async fn ensure_ready(&self) -> Result<Outcome, InterpretError> {
match self let k8s_state = self
.k8s_state .k8s_state
.get_or_try_init(|| self.try_get_or_install_k8s_client()) .get_or_try_init(|| self.try_get_or_install_k8s_client())
.await? .await?;
{ //{
Some(k8s_state) => Ok(Outcome::success(k8s_state.message.clone())), // Some(k8s_state) => Ok(Outcome::success(k8s_state.message.clone())),
None => Err(InterpretError::new( // None => Err(InterpretError::new("No K8s client could be found or installed".to_string(),
"No K8s client could be found or installed".to_string(), // )),
)), //}
match self.is_helm_available() {
Ok(()) => Ok(Outcome::success("helm available".to_string())),
Err(_) => Err(InterpretError::new("helm unavailable".to_string())),
} }
} }
} }
impl HelmCommand for K8sAnywhereTopology {}