From 6c06a4ae07ff5607ceb0efe5f6dba0699be911fd Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 24 Apr 2025 15:51:28 +0000 Subject: [PATCH] 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 Co-authored-by: Willem Co-committed-by: Willem --- harmony/src/domain/topology/k8s_anywhere.rs | 49 ++++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/harmony/src/domain/topology/k8s_anywhere.rs b/harmony/src/domain/topology/k8s_anywhere.rs index 405e46d..c8912d7 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,25 @@ 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( + .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 {}