fix: Add support for multiple Kubeconfig files (#635)

This adds support for having multiple Kubeconfig file set as part of the
`KUBECONFIG` env var.
This commit is contained in:
Thomas O'Donnell 2019-11-12 06:34:01 +01:00 committed by Matan Kushner
parent c7cbaa155e
commit fc2f644237
1 changed files with 14 additions and 6 deletions

View File

@ -38,15 +38,23 @@ fn get_kube_context(contents: &str) -> Option<(String, String)> {
Some((current_ctx.to_string(), ns.to_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<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let filename = match env::var("KUBECONFIG") { let kube_cfg = match env::var("KUBECONFIG") {
Ok(path) => path::PathBuf::from(path), Ok(paths) => env::split_paths(&paths)
Err(_) => dirs::home_dir()?.join(".kube").join("config"), .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 kube_cfg {
match get_kube_context(&contents) {
Some(kube_cfg) => { Some(kube_cfg) => {
let (kube_ctx, kube_ns) = kube_cfg; let (kube_ctx, kube_ns) = kube_cfg;