Try to get GVK from YAML

This commit is contained in:
tahahawa 2025-07-09 23:36:39 -04:00
parent 28476af222
commit 2d8bd5c4ae
2 changed files with 26 additions and 19 deletions

View File

@ -247,39 +247,53 @@ impl K8sClient {
pub async fn apply_yaml_many( pub async fn apply_yaml_many(
&self, &self,
api_resource: &ApiResource,
yaml: &Vec<serde_yaml::Value>, yaml: &Vec<serde_yaml::Value>,
ns: Option<&str>, ns: Option<&str>,
) -> Result<(), Error> { ) -> Result<(), Error> {
for y in yaml.iter() { for y in yaml.iter() {
self.apply_yaml(api_resource, y, ns).await?; self.apply_yaml(y, ns).await?;
} }
Ok(()) Ok(())
} }
pub async fn apply_yaml( pub async fn apply_yaml(
&self, &self,
api_resource: &ApiResource,
yaml: &serde_yaml::Value, yaml: &serde_yaml::Value,
ns: Option<&str>, ns: Option<&str>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let obj: DynamicObject = serde_yaml::from_value(yaml.clone()).expect("TODO do not unwrap"); 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 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 { let namespace = match ns {
Some(n) => n, Some(n) => n,
None => { None => obj
obj
.metadata .metadata
.namespace .namespace
.as_ref() .as_ref()
.expect("YAML must have a namespace") .expect("YAML must have a namespace"),
},
}; };
// 5. Create a dynamic API client for this resource type. // 5. Create a dynamic API client for this resource type.
let api: Api<DynamicObject> = let api: Api<DynamicObject> =
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. // 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. // This will create the resource if it doesn't exist, or update it if it does.

View File

@ -57,16 +57,9 @@ impl<T: Topology + K8sclient + HelmCommand> Interpret<T> for ArgoInterpret {
.execute(inventory, topology) .execute(inventory, topology)
.await?; .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?; let k8s_client = topology.k8s_client().await?;
k8s_client k8s_client
.apply_yaml_many( .apply_yaml_many(&self.argo_apps.iter().map(|a| a.to_yaml()).collect(), None)
&api_resource,
&self.argo_apps.iter().map(|a| a.to_yaml()).collect(),
None,
)
.await .await
.unwrap(); .unwrap();
Ok(Outcome::success(format!( Ok(Outcome::success(format!(