diff --git a/crates/stackable-versioned-macros/fixtures/inputs/k8s/shortnames.rs b/crates/stackable-versioned-macros/fixtures/inputs/k8s/shortnames.rs new file mode 100644 index 000000000..aa43ca7b0 --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/inputs/k8s/shortnames.rs @@ -0,0 +1,9 @@ +#[versioned( + version(name = "v1alpha1"), + k8s(group = "stackable.tech", shortname = "f", shortname = "fo",) +)] +// --- +#[derive( + Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema, kube::CustomResource, +)] +pub(crate) struct FooSpec {} diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@shortnames.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@shortnames.rs.snap new file mode 100644 index 000000000..b92e44ecb --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@shortnames.rs.snap @@ -0,0 +1,55 @@ +--- +source: crates/stackable-versioned-macros/src/lib.rs +expression: formatted +input_file: crates/stackable-versioned-macros/fixtures/inputs/k8s/shortnames.rs +--- +#[automatically_derived] +pub(crate) mod v1alpha1 { + use super::*; + #[derive( + Clone, + Debug, + serde::Deserialize, + serde::Serialize, + schemars::JsonSchema, + kube::CustomResource, + )] + #[kube( + group = "stackable.tech", + version = "v1alpha1", + kind = "Foo", + shortname = "f", + shortname = "fo" + )] + pub struct FooSpec {} +} +#[automatically_derived] +pub(crate) enum Foo { + V1Alpha1, +} +#[automatically_derived] +impl ::std::fmt::Display for Foo { + fn fmt( + &self, + f: &mut ::std::fmt::Formatter<'_>, + ) -> ::std::result::Result<(), ::std::fmt::Error> { + match self { + Self::V1Alpha1 => f.write_str("v1alpha1"), + } + } +} +#[automatically_derived] +impl Foo { + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. + pub fn merged_crd( + stored_apiversion: Self, + ) -> ::std::result::Result< + ::k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition, + ::kube::core::crd::MergeError, + > { + ::kube::core::crd::merge_crds( + vec![< v1alpha1::Foo as ::kube::core::CustomResourceExt > ::crd()], + &stored_apiversion.to_string(), + ) + } +} diff --git a/crates/stackable-versioned-macros/src/attrs/k8s.rs b/crates/stackable-versioned-macros/src/attrs/k8s.rs index 0397d12c7..9a08424b9 100644 --- a/crates/stackable-versioned-macros/src/attrs/k8s.rs +++ b/crates/stackable-versioned-macros/src/attrs/k8s.rs @@ -8,16 +8,19 @@ use syn::Path; /// /// Supported arguments are: /// -/// - `group`, which sets the CRD group, usually the domain of the company. -/// - `kind`, which allows overwriting the kind field of the CRD. This defaults to the struct name -/// (without the 'Spec' suffix). -/// - `singular`, to specify the singular name of the CR object. -/// - `plural`, to specify the plural name of the CR object. -/// - `namespaced`, to specify that this is a namespaced resource rather than cluster level. +/// - `group`: Set the group of the CR object, usually the domain of the company. +/// This argument is Required. +/// - `kind`: Override the kind field of the CR object. This defaults to the struct +/// name (without the 'Spec' suffix). +/// - `singular`: Set the singular name of the CR object. +/// - `plural`: Set the plural name of the CR object. +/// - `namespaced`: Indicate that this is a namespaced scoped resource rather than a +/// cluster scoped resource. /// - `crates`: Override specific crates. -/// - `status`: Sets the specified struct as the status subresource. -/// - `shortname`: Sets the shortname of the CRD. -/// - `skip`, which controls skipping parts of the generation. +/// - `status`: Set the specified struct as the status subresource. +/// - `shortname`: Set a shortname for the CR object. This can be specified multiple +/// times. +/// - `skip`: Controls skipping parts of the generation. #[derive(Clone, Debug, FromMeta)] pub(crate) struct KubernetesArguments { pub(crate) group: String, @@ -32,7 +35,8 @@ pub(crate) struct KubernetesArguments { // schema // scale // printcolumn - pub(crate) shortname: Option, + #[darling(multiple, rename = "shortname")] + pub(crate) shortnames: Vec, // category // selectable // doc diff --git a/crates/stackable-versioned-macros/src/codegen/container/mod.rs b/crates/stackable-versioned-macros/src/codegen/container/mod.rs index 580c857dd..6a7729f18 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/mod.rs @@ -280,7 +280,7 @@ pub(crate) struct KubernetesOptions { // schema // scale // printcolumn - pub(crate) shortname: Option, + pub(crate) shortnames: Vec, // category // selectable // doc @@ -301,7 +301,7 @@ impl From for KubernetesOptions { .crates .map_or_else(KubernetesCrateOptions::default, |crates| crates.into()), status: args.status, - shortname: args.shortname, + shortnames: args.shortnames, skip_merged_crd: args.skip.map_or(false, |s| s.merged_crd.is_present()), } } diff --git a/crates/stackable-versioned-macros/src/codegen/container/struct.rs b/crates/stackable-versioned-macros/src/codegen/container/struct.rs index 5d3038f7c..081b9e9e8 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/struct.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/struct.rs @@ -282,10 +282,11 @@ impl Struct { .status .as_ref() .map(|s| quote! { , status = #s }); - let shortname = kubernetes_options - .shortname - .as_ref() - .map(|s| quote! { , shortname = #s }); + let shortnames: TokenStream = kubernetes_options + .shortnames + .iter() + .map(|s| quote! { , shortname = #s }) + .collect(); Some(quote! { // The end-developer needs to derive CustomResource and JsonSchema. @@ -294,7 +295,7 @@ impl Struct { // These must be comma separated (except the last) as they always exist: group = #group, version = #version, kind = #kind // These fields are optional, and therefore the token stream must prefix each with a comma: - #singular #plural #namespaced #crates #status #shortname + #singular #plural #namespaced #crates #status #shortnames )] }) } diff --git a/crates/stackable-versioned-macros/src/lib.rs b/crates/stackable-versioned-macros/src/lib.rs index b28e7a431..368f94e3d 100644 --- a/crates/stackable-versioned-macros/src/lib.rs +++ b/crates/stackable-versioned-macros/src/lib.rs @@ -603,16 +603,18 @@ 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. +- `group`: Set the group of the CR object, usually the domain of the company. + This argument is Required. +- `kind`: Override the kind field of the CR object. This defaults to the struct + name (without the 'Spec' suffix). +- `singular`: Set the singular name of the CR object. +- `plural`: Set the plural name of the CR object. +- `namespaced`: Indicate that this is a namespaced scoped resource rather than a + cluster scoped resource. - `crates`: Override specific crates. -- `status`: Sets the specified struct as the status subresource. -- `shortname`: Sets the shortname of the CRD. +- `status`: Set the specified struct as the status subresource. +- `shortname`: Set a shortname for the CR object. This can be specified multiple + times. ### Versioning Items in a Module diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 487ef1246..39fd686dd 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +- Add support for multiple k8s `shortname` arguments ([#958]). + +[#958]: https://github.com/stackabletech/operator-rs/pull/958 + ## [0.5.0] - 2024-12-03 ### Added