fix: kubeconfig falls back to .kube if KUBECONFIG env variable is not set
Some checks failed
Run Check Script / check (pull_request) Failing after 2s

This commit is contained in:
2026-01-07 15:47:08 -05:00
parent 05c6398875
commit 4dcaf55dc5

View File

@@ -1006,8 +1006,28 @@ impl K8sAnywhereConfig {
}
fn from_env() -> Self {
fn get_kube_config_path() -> Option<std::path::PathBuf> {
// 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")