From 8a3c4680c9a0d4d75c502d1cde8ae5e8de8cb127 Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Sun, 1 Sep 2024 21:24:33 +0200 Subject: [PATCH] Add ResourceInherit derive macro Allows to generate Resource trait implementation for types which proxy another type internally. ConfigMaps and Secrets can be strictly typed on the client side. While using DeserializeGuard, resources can be listed and watched, skipping invalid resources. Signed-off-by: Danil-Grigorev --- kube-derive/src/lib.rs | 50 +++++++ kube-derive/src/resource_inherit.rs | 188 ++++++++++++++++++++++++++ kube-derive/tests/resource_inherit.rs | 55 ++++++++ kube/src/lib.rs | 4 + 4 files changed, 297 insertions(+) create mode 100644 kube-derive/src/resource_inherit.rs create mode 100644 kube-derive/tests/resource_inherit.rs diff --git a/kube-derive/src/lib.rs b/kube-derive/src/lib.rs index a6ea66ab3..e31bc8c14 100644 --- a/kube-derive/src/lib.rs +++ b/kube-derive/src/lib.rs @@ -4,6 +4,7 @@ extern crate proc_macro; #[macro_use] extern crate quote; mod custom_resource; +mod resource_inherit; /// A custom derive for kubernetes custom resource definitions. /// @@ -308,3 +309,52 @@ mod custom_resource; pub fn derive_custom_resource(input: proc_macro::TokenStream) -> proc_macro::TokenStream { custom_resource::derive(proc_macro2::TokenStream::from(input)).into() } + +/// A custom derive for inheriting Resource impl for the type. +/// +/// This will generate a [`kube::Resource`] trait implementation, which inherits the specified +/// resources trait implementation. +/// +/// Such implementation allows to add strict typing to some typical resources like `Secret` or `ConfigMap`, +/// in cases when implementing CRD is not desirable or it does not fit the use-case. +/// +/// This object can be used with [`kube::Api`]. +/// +/// # Example +/// +/// ```rust,no_run +/// use kube::api::ObjectMeta; +/// use k8s_openapi::api::core::v1::ConfigMap; +/// use kube_derive::ResourceInherit; +/// use kube::Client; +/// use kube::Api; +/// use serde::Deserialize; +/// +/// #[derive(ResourceInherit, Clone, Debug, Deserialize)] +/// #[inherit( +/// resource = "ConfigMap", +/// namespaced, +/// )] +/// struct FooMap { +/// metadata: ObjectMeta, +/// data: Option, +/// } +/// +/// #[derive(Clone, Debug, Deserialize)] +/// struct FooMapSpec { +/// field: String, +/// } +/// +/// let client: Client = todo!(); +/// let api: Api = Api::default_namespaced(client); +/// let config_map = api.get("with-field"); +/// ``` +/// +/// The example above will generate: +/// ``` +/// // impl kube::Resource for FooMap { .. } +/// ``` +#[proc_macro_derive(ResourceInherit, attributes(inherit))] +pub fn derive_resource_inherit(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + resource_inherit::derive(proc_macro2::TokenStream::from(input)).into() +} diff --git a/kube-derive/src/resource_inherit.rs b/kube-derive/src/resource_inherit.rs new file mode 100644 index 000000000..109a9b811 --- /dev/null +++ b/kube-derive/src/resource_inherit.rs @@ -0,0 +1,188 @@ +// Generated by darling macros, out of our control +#![allow(clippy::manual_unwrap_or_default)] + +use darling::{FromDeriveInput, FromMeta}; +use syn::{parse_quote, Data, DeriveInput, Path}; + +/// Values we can parse from #[kube(attrs)] +#[derive(Debug, FromDeriveInput)] +#[darling(attributes(inherit))] +struct InheritAttrs { + resource: syn::Path, + #[darling(default)] + namespaced: bool, + #[darling(default)] + dynamic: bool, + #[darling(default)] + crates: Crates, +} + +#[derive(Debug, FromMeta)] +struct Crates { + #[darling(default = "Self::default_kube_core")] + kube_core: Path, + #[darling(default = "Self::default_k8s_openapi")] + k8s_openapi: Path, + #[darling(default = "Self::default_discovery")] + discovery: Path, +} + +// Default is required when the subattribute isn't mentioned at all +// Delegate to darling rather than deriving, so that we can piggyback off the `#[darling(default)]` clauses +impl Default for Crates { + fn default() -> Self { + Self::from_list(&[]).unwrap() + } +} + +impl Crates { + fn default_kube_core() -> Path { + parse_quote! { ::kube::core } // by default must work well with people using facade crate + } + + fn default_k8s_openapi() -> Path { + parse_quote! { ::k8s_openapi } + } + + fn default_discovery() -> Path { + parse_quote! { ::kube::discovery } + } +} + +pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream { + let derive_input: DeriveInput = match syn::parse2(input) { + Err(err) => return err.to_compile_error(), + Ok(di) => di, + }; + // Limit derive to structs + match derive_input.data { + Data::Struct(_) | Data::Enum(_) => {} + _ => { + return syn::Error::new_spanned(&derive_input.ident, r#"Unions can not #[derive(Resource)]"#) + .to_compile_error() + } + } + let kube_attrs = match InheritAttrs::from_derive_input(&derive_input) { + Err(err) => return err.write_errors(), + Ok(attrs) => attrs, + }; + + let InheritAttrs { + resource, + namespaced, + dynamic, + crates: Crates { + kube_core, + k8s_openapi, + discovery, + }, + .. + } = kube_attrs; + + let rootident = derive_input.ident; + + if namespaced && dynamic { + return syn::Error::new_spanned(&rootident, r#"Resource can be namespaced or dynamic, not both"#) + .to_compile_error(); + } + + let scope_quote = if namespaced { + quote! { #kube_core::NamespaceResourceScope } + } else if dynamic { + quote! { #kube_core::DynamicResourceScope } + } else { + quote! { #kube_core::ClusterResourceScope } + }; + + let dynamic_type = if dynamic { + quote! { #discovery::ApiResource } + } else { + quote! { () } + }; + + let inherit_resource = quote! { + impl #kube_core::Resource for #rootident { + type DynamicType = #dynamic_type; + type Scope = #scope_quote; + + fn group(_: &#dynamic_type) -> std::borrow::Cow<'_, str> { + #resource::group(&Default::default()).into_owned().into() + } + + fn kind(_: &#dynamic_type) -> std::borrow::Cow<'_, str> { + #resource::kind(&Default::default()).into_owned().into() + } + + fn version(_: &#dynamic_type) -> std::borrow::Cow<'_, str> { + #resource::version(&Default::default()).into_owned().into() + } + + fn api_version(_: &#dynamic_type) -> std::borrow::Cow<'_, str> { + #resource::api_version(&Default::default()).into_owned().into() + } + + fn plural(_: &#dynamic_type) -> std::borrow::Cow<'_, str> { + #resource::plural(&Default::default()).into_owned().into() + } + + fn meta(&self) -> &#k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta { + &self.metadata + } + + fn meta_mut(&mut self) -> &mut #k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta { + &mut self.metadata + } + } + }; + + // Concat output + quote! { + #inherit_resource + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_inherit_namespaced() { + let input = quote! { + #[derive(ResourceInherit)] + #[inherit(resource = "ConfigMap", namespaced)] + struct Foo { metadata: ObjectMeta } + }; + + let input = syn::parse2(input).unwrap(); + let inherit_attrs = InheritAttrs::from_derive_input(&input).unwrap(); + assert!(inherit_attrs.namespaced); + assert!(!inherit_attrs.dynamic); + } + + #[test] + fn test_parse_inherit_cluster() { + let input = quote! { + #[derive(ResourceInherit)] + #[inherit(resource = "ConfigMap")] + struct Foo { metadata: ObjectMeta } + }; + + let input = syn::parse2(input).unwrap(); + let inherit_attrs = InheritAttrs::from_derive_input(&input).unwrap(); + assert!(!inherit_attrs.namespaced); + } + + #[test] + fn test_parse_inherit_dynamic() { + let input = quote! { + #[derive(ResourceInherit)] + #[inherit(resource = "ConfigMap", dynamic)] + struct Foo { metadata: ObjectMeta } + }; + + let input = syn::parse2(input).unwrap(); + let inherit_attrs = InheritAttrs::from_derive_input(&input).unwrap(); + assert!(!inherit_attrs.namespaced); + assert!(inherit_attrs.dynamic); + } +} diff --git a/kube-derive/tests/resource_inherit.rs b/kube-derive/tests/resource_inherit.rs new file mode 100644 index 000000000..52c221e81 --- /dev/null +++ b/kube-derive/tests/resource_inherit.rs @@ -0,0 +1,55 @@ +use k8s_openapi::{ + api::core::v1::{ConfigMap, Secret}, + ByteString, +}; +use kube::api::ObjectMeta; +use kube_derive::ResourceInherit; + +#[derive(ResourceInherit, Default)] +#[inherit(resource = "ConfigMap", namespaced)] +struct TypedMap { + metadata: ObjectMeta, + data: Option, +} + +#[derive(Default)] +struct TypedData { + field: String, +} + +#[derive(ResourceInherit, Default)] +#[inherit(resource = "Secret", namespaced)] +struct TypedSecret { + metadata: ObjectMeta, + data: Option, +} + +#[derive(Default)] +struct TypedSecretData { + field: ByteString, +} + +#[cfg(test)] +mod tests { + use kube::Resource; + + use crate::{TypedMap, TypedSecret}; + + #[test] + fn test_parse_config_map_default() { + TypedMap::default(); + assert_eq!(TypedMap::kind(&()), "ConfigMap"); + assert_eq!(TypedMap::api_version(&()), "v1"); + assert_eq!(TypedMap::group(&()), ""); + assert_eq!(TypedMap::plural(&()), "configmaps"); + } + + #[test] + fn test_parse_secret_default() { + TypedSecret::default(); + assert_eq!(TypedSecret::kind(&()), "Secret"); + assert_eq!(TypedSecret::api_version(&()), "v1"); + assert_eq!(TypedSecret::group(&()), ""); + assert_eq!(TypedSecret::plural(&()), "secrets"); + } +} diff --git a/kube/src/lib.rs b/kube/src/lib.rs index af8f7490b..0dfbb4979 100644 --- a/kube/src/lib.rs +++ b/kube/src/lib.rs @@ -165,6 +165,10 @@ cfg_error! { #[cfg_attr(docsrs, doc(cfg(feature = "derive")))] pub use kube_derive::CustomResource; +#[cfg(feature = "derive")] +#[cfg_attr(docsrs, doc(cfg(feature = "derive")))] +pub use kube_derive::ResourceInherit; + /// Re-exports from `kube-runtime` #[cfg(feature = "runtime")] #[cfg_attr(docsrs, doc(cfg(feature = "runtime")))]