diff --git a/src/modules/kubernetes.rs b/src/modules/kubernetes.rs index 6fec82d72..aa7ae9bec 100644 --- a/src/modules/kubernetes.rs +++ b/src/modules/kubernetes.rs @@ -34,10 +34,7 @@ fn get_kube_context(filename: path::PathBuf) -> Option { Some(current_ctx.to_string()) } -fn get_kube_ctx_component( - filename: path::PathBuf, - current_ctx: String, -) -> Option { +fn get_kube_ctx_component(filename: path::PathBuf, current_ctx: &str) -> Option { let contents = utils::read_file(filename).ok()?; let yaml_docs = YamlLoader::load_from_str(&contents).ok()?; @@ -120,7 +117,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { }; // If we have some config for doing the directory scan then we use it but if we don't then we - // assume we should treat it like the module is enabled to preserve backward compatability. + // assume we should treat it like the module is enabled to preserve backward compatibility. let have_scan_config = !(config.detect_files.is_empty() && config.detect_folders.is_empty() && config.detect_extensions.is_empty()); @@ -144,23 +141,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { let kube_ctx = env::split_paths(&kube_cfg).find_map(get_kube_context)?; - let ctx_components: Vec> = env::split_paths(&kube_cfg) - .map(|filename| get_kube_ctx_component(filename, kube_ctx.clone())) + let ctx_components: Vec = env::split_paths(&kube_cfg) + .filter_map(|filename| get_kube_ctx_component(filename, &kube_ctx)) .collect(); - let kube_user = ctx_components.iter().find(|&ctx| match ctx { - Some(kube) => kube.user.is_some(), - None => false, - }); - let kube_ns = ctx_components.iter().find(|&ctx| match ctx { - Some(kube) => kube.namespace.is_some(), - None => false, - }); - let kube_cluster = ctx_components.iter().find(|&ctx| match ctx { - Some(kube) => kube.cluster.is_some(), - None => false, - }); - let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|variable, _| match variable { @@ -174,25 +158,20 @@ pub fn module<'a>(context: &'a Context) -> Option> { .map(|variable| match variable { "context" => Some(Ok(get_kube_context_name(&config, &kube_ctx))), - "namespace" => kube_ns.and_then(|ctx| { - ctx.as_ref().map(|kube| { - // unwrap is safe as kube_ns only holds kube.namespace.is_some() - Ok(Cow::Borrowed(kube.namespace.as_ref().unwrap().as_str())) - }) - }), - "user" => kube_user.and_then(|ctx| { - ctx.as_ref().map(|kube| { - // unwrap is safe as kube_user only holds kube.user.is_some() - Ok(get_kube_user(&config, kube.user.as_ref().unwrap().as_str())) - }) - }), - "cluster" => kube_cluster.and_then(|ctx| { - ctx.as_ref().map(|kube| { - // unwrap is safe as kube_cluster only holds kube.cluster.is_some() - Ok(Cow::Borrowed(kube.cluster.as_ref().unwrap().as_str())) - }) - }), + "namespace" => ctx_components + .iter() + .find_map(|kube| kube.namespace.as_deref()) + .map(|namespace| Ok(Cow::Borrowed(namespace))), + "user" => ctx_components + .iter() + .find_map(|kube| kube.user.as_deref()) + .map(|user| Ok(get_kube_user(&config, user))), + + "cluster" => ctx_components + .iter() + .find_map(|kube| kube.cluster.as_deref()) + .map(|cluster| Ok(Cow::Borrowed(cluster))), _ => None, }) .parse(None, Some(context))