From 2d8bd5c4aeaa3d4669d4711260de6d91b132d526 Mon Sep 17 00:00:00 2001 From: tahahawa Date: Wed, 9 Jul 2025 23:36:39 -0400 Subject: [PATCH] Try to get GVK from YAML --- harmony/src/domain/topology/k8s.rs | 36 +++++++++++++------ .../application/features/helm_argocd_score.rs | 9 +---- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/harmony/src/domain/topology/k8s.rs b/harmony/src/domain/topology/k8s.rs index dcc1307..750dafe 100644 --- a/harmony/src/domain/topology/k8s.rs +++ b/harmony/src/domain/topology/k8s.rs @@ -247,39 +247,53 @@ impl K8sClient { pub async fn apply_yaml_many( &self, - api_resource: &ApiResource, yaml: &Vec, ns: Option<&str>, ) -> Result<(), Error> { for y in yaml.iter() { - self.apply_yaml(api_resource, y, ns).await?; + self.apply_yaml(y, ns).await?; } Ok(()) } pub async fn apply_yaml( &self, - api_resource: &ApiResource, yaml: &serde_yaml::Value, ns: Option<&str>, ) -> Result<(), Error> { let obj: DynamicObject = serde_yaml::from_value(yaml.clone()).expect("TODO do not unwrap"); let name = obj.metadata.name.as_ref().expect("YAML must have a name"); + let api_version = yaml + .get("apiVersion") + .expect("couldn't get apiVersion from YAML") + .as_str() + .expect("couldn't get apiVersion as str"); + let kind = yaml + .get("kind") + .expect("couldn't get kind from YAML") + .as_str() + .expect("couldn't get kind as str"); + + let split: Vec<&str> = api_version.splitn(2, "/").collect(); + let g = split[0]; + let v = split[1]; + + let gvk = GroupVersionKind::gvk(g, v, kind); + let api_resource = ApiResource::from_gvk(&gvk); + let namespace = match ns { Some(n) => n, - None => { - obj - .metadata - .namespace - .as_ref() - .expect("YAML must have a namespace") - }, + None => obj + .metadata + .namespace + .as_ref() + .expect("YAML must have a namespace"), }; // 5. Create a dynamic API client for this resource type. let api: Api = - Api::namespaced_with(self.client.clone(), namespace, api_resource); + Api::namespaced_with(self.client.clone(), namespace, &api_resource); // 6. Apply the object to the cluster using Server-Side Apply. // This will create the resource if it doesn't exist, or update it if it does. diff --git a/harmony/src/modules/application/features/helm_argocd_score.rs b/harmony/src/modules/application/features/helm_argocd_score.rs index 148d996..bcfa97f 100644 --- a/harmony/src/modules/application/features/helm_argocd_score.rs +++ b/harmony/src/modules/application/features/helm_argocd_score.rs @@ -57,16 +57,9 @@ impl Interpret for ArgoInterpret { .execute(inventory, topology) .await?; - let gvk = GroupVersionKind::gvk("argoproj.io", "v1alpha1", "Application"); - let api_resource = ApiResource::from_gvk_with_plural(&gvk, "applications"); - let k8s_client = topology.k8s_client().await?; k8s_client - .apply_yaml_many( - &api_resource, - &self.argo_apps.iter().map(|a| a.to_yaml()).collect(), - None, - ) + .apply_yaml_many(&self.argo_apps.iter().map(|a| a.to_yaml()).collect(), None) .await .unwrap(); Ok(Outcome::success(format!(