From c00eadca5dde420b470fbbba897c676f504cd092 Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 24 Apr 2025 11:09:25 -0400 Subject: [PATCH 1/2] wip: working on ensure_ready to check helm is available --- harmony/src/domain/topology/k8s_anywhere.rs | 47 +++++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/harmony/src/domain/topology/k8s_anywhere.rs b/harmony/src/domain/topology/k8s_anywhere.rs index 405e46d..0c3451d 100644 --- a/harmony/src/domain/topology/k8s_anywhere.rs +++ b/harmony/src/domain/topology/k8s_anywhere.rs @@ -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 { todo!("Use kube-rs default behavior to load system kubeconfig"); } @@ -126,15 +151,21 @@ impl Topology for K8sAnywhereTopology { } async fn ensure_ready(&self) -> Result { - 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( - "No K8s client could be found or installed".to_string(), - )), + .await?; + //{ + // Some(k8s_state) => Ok(Outcome::success(k8s_state.message.clone())), + // None => Err(InterpretError::new("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 {} -- 2.39.5 From 7576c4a74cb1aff312b7d36adcc869d089396846 Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 24 Apr 2025 11:46:26 -0400 Subject: [PATCH 2/2] fix: made -version version, used ok_or to return error if k8s_state is not ready --- harmony/src/domain/topology/k8s_anywhere.rs | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/harmony/src/domain/topology/k8s_anywhere.rs b/harmony/src/domain/topology/k8s_anywhere.rs index 0c3451d..c8912d7 100644 --- a/harmony/src/domain/topology/k8s_anywhere.rs +++ b/harmony/src/domain/topology/k8s_anywhere.rs @@ -38,7 +38,7 @@ impl K8sAnywhereTopology { fn is_helm_available(&self) -> Result<(), String> { let version_result = Command::new("helm") - .arg("-version") + .arg("version") .output() .map_err(|e| format!("Failed to execute 'helm -version': {}", e))?; @@ -151,20 +151,24 @@ impl Topology for K8sAnywhereTopology { } async fn ensure_ready(&self) -> Result { - let k8s_state = 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("No K8s client could be found or installed".to_string(), - // )), - //} + + 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("helm available".to_string())), + Ok(()) => Ok(Outcome::success(format!( + "{} + helm available", + k8s_state.message.clone() + ))), Err(_) => Err(InterpretError::new("helm unavailable".to_string())), } - } } -- 2.39.5