From 4dcaf55dc591c51c1f15ddd06cb462c7216b8fe0 Mon Sep 17 00:00:00 2001 From: wjro Date: Wed, 7 Jan 2026 15:47:08 -0500 Subject: [PATCH] fix: kubeconfig falls back to .kube if KUBECONFIG env variable is not set --- .../topology/k8s_anywhere/k8s_anywhere.rs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/harmony/src/domain/topology/k8s_anywhere/k8s_anywhere.rs b/harmony/src/domain/topology/k8s_anywhere/k8s_anywhere.rs index 76280d8..2ede07f 100644 --- a/harmony/src/domain/topology/k8s_anywhere/k8s_anywhere.rs +++ b/harmony/src/domain/topology/k8s_anywhere/k8s_anywhere.rs @@ -1006,8 +1006,28 @@ impl K8sAnywhereConfig { } fn from_env() -> Self { + fn get_kube_config_path() -> Option { + // 1. Check for the KUBECONFIG environment variable first (standard practice) + if let Ok(val) = std::env::var("KUBECONFIG") { + if !val.is_empty() { + return Some(std::path::PathBuf::from(val)); + } + } + + // 2. Use the standard library to find the home directory + // As of recent Rust versions, this is the preferred cross-platform method. + let mut path = std::env::home_dir()?; + + // 3. Construct the path to .kube/config + // .push() handles OS-specific separators (\ for Windows, / for Unix) + path.push(".kube"); + path.push("config"); + + Some(path) + } + Self { - kubeconfig: std::env::var("KUBECONFIG").ok().map(|v| v.to_string()), + kubeconfig: get_kube_config_path().map(|s| s.to_string_lossy().into_owned()), use_system_kubeconfig: std::env::var("HARMONY_USE_SYSTEM_KUBECONFIG") .map_or_else(|_| false, |v| v.parse().ok().unwrap_or(false)), autoinstall: std::env::var("HARMONY_AUTOINSTALL")