From fc2f644237f7fc7effa22aa334c9211679227595 Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Tue, 12 Nov 2019 06:34:01 +0100 Subject: [PATCH] fix: Add support for multiple Kubeconfig files (#635) This adds support for having multiple Kubeconfig file set as part of the `KUBECONFIG` env var. --- src/modules/kubernetes.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/modules/kubernetes.rs b/src/modules/kubernetes.rs index 16336d37..c8d55be1 100644 --- a/src/modules/kubernetes.rs +++ b/src/modules/kubernetes.rs @@ -38,15 +38,23 @@ fn get_kube_context(contents: &str) -> Option<(String, String)> { Some((current_ctx.to_string(), ns.to_string())) } +fn parse_kubectl_file(filename: &path::PathBuf) -> Option<(String, String)> { + let contents = utils::read_file(filename).ok()?; + get_kube_context(&contents) +} + pub fn module<'a>(context: &'a Context) -> Option> { - let filename = match env::var("KUBECONFIG") { - Ok(path) => path::PathBuf::from(path), - Err(_) => dirs::home_dir()?.join(".kube").join("config"), + let kube_cfg = match env::var("KUBECONFIG") { + Ok(paths) => env::split_paths(&paths) + .filter_map(|filename| parse_kubectl_file(&filename)) + .nth(0), + Err(_) => { + let filename = dirs::home_dir()?.join(".kube").join("config"); + parse_kubectl_file(&filename) + } }; - let contents = utils::read_file(filename).ok()?; - - match get_kube_context(&contents) { + match kube_cfg { Some(kube_cfg) => { let (kube_ctx, kube_ns) = kube_cfg;