chore: Simplify k8s flavour detection algorithm and do not unwrap when it cannot be detected, just return Err

This commit is contained in:
Jean-Gabriel Gill-Couture 2025-09-30 22:59:50 -04:00
parent 1cec398d4d
commit 2d3c32469c

View File

@ -184,7 +184,7 @@ impl K8sAnywhereTopology {
} }
} }
pub async fn get_k8s_distribution(&self) -> KubernetesDistribution { pub async fn get_k8s_distribution(&self) -> Result<&KubernetesDistribution, PreparationError> {
self.flavour self.flavour
.get_or_try_init(async || { .get_or_try_init(async || {
let client = self.k8s_client().await.unwrap(); let client = self.k8s_client().await.unwrap();
@ -197,45 +197,22 @@ impl K8sAnywhereTopology {
PreparationError::new(format!("Could not get server version: {}", e)) PreparationError::new(format!("Could not get server version: {}", e))
})?; })?;
let rules: &[&dyn Fn() -> Option<KubernetesDistribution>] = &[ // OpenShift / OKD
// OpenShift / OKD if discovery
&|| { .groups()
discovery .any(|g| g.name() == "project.openshift.io")
.groups() {
.any(|g| g.name() == "project.openshift.io") return Ok(KubernetesDistribution::OpenshiftFamily);
.then_some(KubernetesDistribution::OpenshiftFamily) }
},
// K3d / K3s
&|| {
version
.git_version
.contains("k3s")
.then_some(KubernetesDistribution::K3sFamily)
},
// Fallback Kubernetes K8s
&|| {
if !discovery
.groups()
.any(|g| g.name().ends_with("openshift.io"))
&& !version.git_version.contains("k3s")
&& !version.git_version.contains("k3d")
{
Some(KubernetesDistribution::Default)
} else {
None
}
},
];
rules.iter().find_map(|rule| rule()).ok_or_else(|| { // K3d / K3s
PreparationError::new( if version.git_version.contains("k3s") {
"Unable to detect Kubernetes cluster distribution".to_string(), return Ok(KubernetesDistribution::K3sFamily);
) }
})
return Ok(KubernetesDistribution::Default);
}) })
.await .await
.unwrap()
.clone()
} }
async fn get_cluster_observability_operator_prometheus_application_score( async fn get_cluster_observability_operator_prometheus_application_score(