From addd11e3004e93b473c4e28664aaba3dae7e8103 Mon Sep 17 00:00:00 2001 From: Kevin Song Date: Tue, 20 Dec 2022 19:04:40 -0600 Subject: [PATCH] refactor(kubernetes): Remove nested Options (#4743) Kubernetes module was previously a bit messy, with lots of unnecessarily nested options. Clean this up using filter_map and find_map, with two major results: 1. `ctx_components` is now a Vec instead of a Vec>. This greatly simplified downstream processing of these context components. 2. Instead of storing a partial computation of the namespace in variables `kube_ns`, etc, compute them directly in the formatter mapping. This is made simpler (read: actually doable) by change 1. --- src/modules/kubernetes.rs | 55 ++++++++++++--------------------------- 1 file changed, 17 insertions(+), 38 deletions(-) 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))