diff --git a/src/callback_handler/kubernetes.rs b/src/callback_handler/kubernetes.rs index fd82e39c..0581f88e 100644 --- a/src/callback_handler/kubernetes.rs +++ b/src/callback_handler/kubernetes.rs @@ -182,6 +182,11 @@ impl Client { .map_err(anyhow::Error::new)? .ok_or_else(|| anyhow!("Cannot find {api_version}/{kind} named '{name}' inside of namespace '{namespace:?}'")) } + + async fn get_resource_plural_name(&mut self, api_version: &str, kind: &str) -> Result { + let resource = self.build_kube_resource(api_version, kind).await?; + Ok(resource.resource.plural) + } } #[cached( @@ -287,3 +292,24 @@ pub(crate) async fn get_resource_cached( ) -> Result> { get_resource(client, api_version, kind, name, namespace).await } + +pub(crate) async fn get_resource_plural_name( + client: Option<&mut Client>, + api_version: &str, + kind: &str, +) -> Result> { + if client.is_none() { + return Err(anyhow!("kube::Client was not initialized properly")); + } + + client + .unwrap() + .get_resource_plural_name(api_version, kind) + .await + .map(|value| cached::Return { + // this is always cached, because the client builds an overview of + // the cluster resources at bootstrap time + was_cached: true, + value, + }) +} diff --git a/src/callback_handler/mod.rs b/src/callback_handler/mod.rs index 588fcca3..0321ccea 100644 --- a/src/callback_handler/mod.rs +++ b/src/callback_handler/mod.rs @@ -300,6 +300,24 @@ impl CallbackHandler { ) } } + CallbackRequestType::KubernetesGetResourcePluralName { + api_version, + kind, + } => { + handle_callback!( + req, + format!("{api_version}/{kind}"), + "Is Kubernetes resource namespaced", + { + kubernetes::get_resource_plural_name( + self.kubernetes_client.as_mut(), + &api_version, + &kind, + ) + } + ) + } + } } }, diff --git a/src/callback_requests.rs b/src/callback_requests.rs index a3619222..226130eb 100644 --- a/src/callback_requests.rs +++ b/src/callback_requests.rs @@ -162,6 +162,14 @@ pub enum CallbackRequestType { /// might cause issues to the cluster disable_cache: bool, }, + + /// Get the plural name of a Kubernetes resource. E.g. `v1/Service` -> `services` + KubernetesGetResourcePluralName { + /// apiVersion of the resource (v1 for core group, groupName/groupVersions for other). + api_version: String, + /// Singular PascalCase name of the resource + kind: String, + }, } impl From for CallbackRequestType {