diff --git a/crates/stackable-versioned-macros/src/attrs/common/container.rs b/crates/stackable-versioned-macros/src/attrs/common/container.rs index d5a3ccb0a..434c9e1ab 100644 --- a/crates/stackable-versioned-macros/src/attrs/common/container.rs +++ b/crates/stackable-versioned-macros/src/attrs/common/container.rs @@ -141,7 +141,10 @@ pub(crate) struct OptionAttributes { #[derive(Clone, Debug, FromMeta)] pub(crate) struct KubernetesAttributes { pub(crate) skip: Option, + pub(crate) singular: Option, + pub(crate) plural: Option, pub(crate) kind: Option, + pub(crate) namespaced: Flag, pub(crate) group: String, } diff --git a/crates/stackable-versioned-macros/src/codegen/common/container.rs b/crates/stackable-versioned-macros/src/codegen/common/container.rs index c4d427d6e..4472552d9 100644 --- a/crates/stackable-versioned-macros/src/codegen/common/container.rs +++ b/crates/stackable-versioned-macros/src/codegen/common/container.rs @@ -130,6 +130,9 @@ impl VersionedContainer { let kubernetes_options = attributes.kubernetes_attrs.map(|a| KubernetesOptions { skip_merged_crd: a.skip.map_or(false, |s| s.merged_crd.is_present()), + namespaced: a.namespaced.is_present(), + singular: a.singular, + plural: a.plural, group: a.group, kind: a.kind, }); @@ -178,7 +181,10 @@ pub(crate) struct VersionedContainerOptions { #[derive(Debug)] pub(crate) struct KubernetesOptions { + pub(crate) singular: Option, + pub(crate) plural: Option, pub(crate) skip_merged_crd: bool, pub(crate) kind: Option, + pub(crate) namespaced: bool, pub(crate) group: String, } diff --git a/crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs b/crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs index 2ed889f69..0c6b42c5f 100644 --- a/crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs @@ -9,8 +9,7 @@ use crate::{ attrs::common::ContainerAttributes, codegen::{ common::{ - Container, ContainerInput, ContainerVersion, Item, KubernetesOptions, VersionExt, - VersionedContainer, + Container, ContainerInput, ContainerVersion, Item, VersionExt, VersionedContainer, }, vstruct::field::VersionedField, }, @@ -152,7 +151,7 @@ impl VersionedStruct { Some(options) => { // Generate the CustomResource derive macro with the appropriate // attributes supplied using #[kube()]. - let cr_derive = self.generate_kubernetes_cr_derive(version, options); + let cr_derive = self.generate_kubernetes_cr_derive(version); // Generate merged_crd specific code when not opted out. let merged_crd = if !options.skip_merged_crd { @@ -282,22 +281,36 @@ impl VersionedStruct { impl VersionedStruct { /// Generates the `kube::CustomResource` derive with the appropriate macro /// attributes. - fn generate_kubernetes_cr_derive( - &self, - version: &ContainerVersion, - options: &KubernetesOptions, - ) -> TokenStream { - let group = &options.group; - let version = version.inner.to_string(); - let kind = options - .kind - .as_ref() - .map_or(self.idents.kubernetes.to_string(), |kind| kind.clone()); + fn generate_kubernetes_cr_derive(&self, version: &ContainerVersion) -> Option { + if let Some(kubernetes_options) = &self.options.kubernetes_options { + // Required arguments + let group = &kubernetes_options.group; + let version = version.inner.to_string(); + let kind = kubernetes_options + .kind + .as_ref() + .map_or(self.idents.kubernetes.to_string(), |kind| kind.clone()); + + // Optional arguments + let namespaced = kubernetes_options + .namespaced + .then_some(quote! { , namespaced }); + let singular = kubernetes_options + .singular + .as_ref() + .map(|s| quote! { , singular = #s }); + let plural = kubernetes_options + .plural + .as_ref() + .map(|p| quote! { , plural = #p }); - quote! { - #[derive(::kube::CustomResource)] - #[kube(group = #group, version = #version, kind = #kind)] + return Some(quote! { + #[derive(::kube::CustomResource)] + #[kube(group = #group, version = #version, kind = #kind #singular #plural #namespaced)] + }); } + + None } /// Generates the `merge_crds` function call. diff --git a/crates/stackable-versioned-macros/src/lib.rs b/crates/stackable-versioned-macros/src/lib.rs index 9aa688f3f..814b1c62b 100644 --- a/crates/stackable-versioned-macros/src/lib.rs +++ b/crates/stackable-versioned-macros/src/lib.rs @@ -462,6 +462,15 @@ println!("{}", serde_yaml::to_string(&merged_crd).unwrap()); ``` "# )] +/// Currently, the following arguments are supported: +/// +/// - `group`: Sets the CRD group, usually the domain of the company. +/// - `kind`: Allows overwriting the kind field of the CRD. This defaults +/// to the struct name (without the 'Spec' suffix). +/// - `singular`: Sets the singular name. +/// - `plural`: Sets the plural name. +/// - `namespaced`: Specifies that this is a namespaced resource rather than +/// a cluster scoped. #[proc_macro_attribute] pub fn versioned(attrs: TokenStream, input: TokenStream) -> TokenStream { let attrs = match NestedMeta::parse_meta_list(attrs.into()) { diff --git a/crates/stackable-versioned-macros/tests/k8s/pass/crd.rs b/crates/stackable-versioned-macros/tests/k8s/pass/crd.rs index c9255d8fa..73f690dc1 100644 --- a/crates/stackable-versioned-macros/tests/k8s/pass/crd.rs +++ b/crates/stackable-versioned-macros/tests/k8s/pass/crd.rs @@ -9,7 +9,12 @@ fn main() { version(name = "v1alpha1"), version(name = "v1beta1"), version(name = "v1"), - k8s(group = "stackable.tech") + k8s( + group = "stackable.tech", + singular = "foo", + plural = "foos", + namespaced, + ) )] #[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] pub struct FooSpec { diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index a66ea0c86..6b6f561ec 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file. ### Added +- Add forwarding of `singular`, `plural`, and `namespaced` arguments in `k8s()` + ([#873]). - Generate a `Version` enum containing all declared versions as variants ([#872]). @@ -15,6 +17,7 @@ All notable changes to this project will be documented in this file. input ([#872]). [#872]: https://github.com/stackabletech/operator-rs/pull/872 +[#873]: https://github.com/stackabletech/operator-rs/pull/873 ## [0.2.0] - 2024-09-19