From 3fe1354408a9ac63f4a036c6c95ab0af047d4a01 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 25 Nov 2024 15:48:12 +0100 Subject: [PATCH 1/3] fix(stackable-versioned): Correctly emit Kubernetes code when macro is used on modules --- .../src/codegen/container/mod.rs | 12 ++--- .../src/codegen/container/struct.rs | 6 +-- .../src/codegen/module.rs | 51 +++++++++++++++++-- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/crates/stackable-versioned-macros/src/codegen/container/mod.rs b/crates/stackable-versioned-macros/src/codegen/container/mod.rs index cef4eb88d..45f9ad153 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/mod.rs @@ -85,9 +85,9 @@ impl Container { /// Kubernetes custom resources. pub(crate) fn generate_kubernetes_merge_crds( &self, - enum_variant_idents: Vec, - enum_variant_strings: Vec, - fn_calls: Vec, + enum_variant_idents: &[IdentString], + enum_variant_strings: &[String], + fn_calls: &[TokenStream], is_nested: bool, ) -> Option { match self { @@ -201,9 +201,9 @@ impl StandaloneContainer { } tokens.extend(self.container.generate_kubernetes_merge_crds( - kubernetes_enum_variant_idents, - kubernetes_enum_variant_strings, - kubernetes_merge_crds_fn_calls, + &kubernetes_enum_variant_idents, + &kubernetes_enum_variant_strings, + &kubernetes_merge_crds_fn_calls, false, )); diff --git a/crates/stackable-versioned-macros/src/codegen/container/struct.rs b/crates/stackable-versioned-macros/src/codegen/container/struct.rs index fcf7ee368..1dd0c8955 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/struct.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/struct.rs @@ -312,9 +312,9 @@ impl Struct { pub(crate) fn generate_kubernetes_merge_crds( &self, - enum_variant_idents: Vec, - enum_variant_strings: Vec, - fn_calls: Vec, + enum_variant_idents: &[IdentString], + enum_variant_strings: &[String], + fn_calls: &[TokenStream], is_nested: bool, ) -> Option { if enum_variant_idents.is_empty() { diff --git a/crates/stackable-versioned-macros/src/codegen/module.rs b/crates/stackable-versioned-macros/src/codegen/module.rs index be2bcc1f3..9069dc8d8 100644 --- a/crates/stackable-versioned-macros/src/codegen/module.rs +++ b/crates/stackable-versioned-macros/src/codegen/module.rs @@ -47,6 +47,9 @@ impl Module { return quote! {}; } + let module_ident = &self.ident; + let module_vis = &self.vis; + // If the 'preserve_module' flag is provided by the user, we need to change the visibility // of version modules (eg. 'v1alpha1') to be public, so that they are accessible inside the // preserved (wrapping) module. Otherwise, we can inherit the visibility from the module @@ -57,14 +60,18 @@ impl Module { &self.vis }; + let mut kubernetes_tokens = TokenStream::new(); let mut tokens = TokenStream::new(); - let module_ident = &self.ident; - let module_vis = &self.vis; + let mut kubernetes_container_items = Vec::new(); let mut versions = self.versions.iter().peekable(); while let Some(version) = versions.next() { + let mut kubernetes_merge_crds_fn_calls = Vec::new(); + let mut kubernetes_enum_variant_idents = Vec::new(); + let mut kubernetes_enum_variant_strings = Vec::new(); + let mut container_definitions = TokenStream::new(); let mut from_impls = TokenStream::new(); @@ -72,11 +79,22 @@ impl Module { for container in &self.containers { container_definitions.extend(container.generate_definition(version)); + from_impls.extend(container.generate_from_impl( version, versions.peek().copied(), self.preserve_module, )); + + // Generate Kubernetes specific code which is placed outside of the container + // definition. + if let Some((enum_variant_ident, enum_variant_string, fn_call)) = + container.generate_kubernetes_item(version) + { + kubernetes_merge_crds_fn_calls.push(fn_call); + kubernetes_enum_variant_idents.push(enum_variant_ident); + kubernetes_enum_variant_strings.push(enum_variant_string); + } } // Only add #[automatically_derived] here if the user doesn't want to preserve the @@ -103,6 +121,29 @@ impl Module { #from_impls }); + + kubernetes_container_items.push(( + kubernetes_merge_crds_fn_calls, + kubernetes_enum_variant_idents, + kubernetes_enum_variant_strings, + )); + } + + // Generate the final Kubernetes specific code for each container (which uses Kubernetes + // specific features) which is appended to the end of container definitions. + for (index, container) in self.containers.iter().enumerate() { + let ( + kubernetes_merge_crds_fn_calls, + kubernetes_enum_variant_idents, + kubernetes_enum_variant_strings, + ) = kubernetes_container_items.get(index).unwrap(); + + kubernetes_tokens.extend(container.generate_kubernetes_merge_crds( + kubernetes_enum_variant_idents, + kubernetes_enum_variant_strings, + kubernetes_merge_crds_fn_calls, + self.preserve_module, + )); } if self.preserve_module { @@ -110,10 +151,14 @@ impl Module { #[automatically_derived] #module_vis mod #module_ident { #tokens + #kubernetes_tokens } } } else { - tokens + quote! { + #tokens + #kubernetes_tokens + } } } } From 34de1829d0f6b67b6eabedf1a5f89939b3a6d860 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 25 Nov 2024 15:49:27 +0100 Subject: [PATCH 2/3] test(stackable-versioned): Add new snapshot tests for K8s + module usage --- .../fixtures/inputs/k8s/module.rs | 27 ++ .../fixtures/inputs/k8s/module_preserve.rs | 28 ++ ...macros__test__k8s_snapshots@module.rs.snap | 375 ++++++++++++++++++ ...est__k8s_snapshots@module_preserve.rs.snap | 360 +++++++++++++++++ 4 files changed, 790 insertions(+) create mode 100644 crates/stackable-versioned-macros/fixtures/inputs/k8s/module.rs create mode 100644 crates/stackable-versioned-macros/fixtures/inputs/k8s/module_preserve.rs create mode 100644 crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module.rs.snap create mode 100644 crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module_preserve.rs.snap diff --git a/crates/stackable-versioned-macros/fixtures/inputs/k8s/module.rs b/crates/stackable-versioned-macros/fixtures/inputs/k8s/module.rs new file mode 100644 index 000000000..71186abf4 --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/inputs/k8s/module.rs @@ -0,0 +1,27 @@ +#[versioned( + version(name = "v1alpha1"), + version(name = "v1"), + version(name = "v2alpha1") +)] +// --- +pub(crate) mod versioned { + #[versioned(k8s(group = "foo.example.org", plural = "foos", namespaced))] + pub struct FooSpec { + bar: usize, + + #[versioned(added(since = "v1"))] + baz: bool, + + #[versioned(deprecated(since = "v2alpha1"))] + deprecated_foo: String, + } + + #[versioned(k8s(group = "bar.example.org", plural = "bars"))] + pub struct BarSpec { + baz: String, + } + + pub struct Baz { + boom: Option, + } +} diff --git a/crates/stackable-versioned-macros/fixtures/inputs/k8s/module_preserve.rs b/crates/stackable-versioned-macros/fixtures/inputs/k8s/module_preserve.rs new file mode 100644 index 000000000..3f0401365 --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/inputs/k8s/module_preserve.rs @@ -0,0 +1,28 @@ +#[versioned( + version(name = "v1alpha1"), + version(name = "v1"), + version(name = "v2alpha1"), + preserve_module +)] +// --- +pub(crate) mod versioned { + #[versioned(k8s(group = "foo.example.org", plural = "foos", namespaced))] + pub struct FooSpec { + bar: usize, + + #[versioned(added(since = "v1"))] + baz: bool, + + #[versioned(deprecated(since = "v2alpha1"))] + deprecated_foo: String, + } + + #[versioned(k8s(group = "bar.example.org", plural = "bars"))] + pub struct BarSpec { + baz: String, + } + + pub struct Baz { + boom: Option, + } +} diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module.rs.snap new file mode 100644 index 000000000..48882f67d --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module.rs.snap @@ -0,0 +1,375 @@ +--- +source: crates/stackable-versioned-macros/src/lib.rs +expression: formatted +input_file: crates/stackable-versioned-macros/fixtures/inputs/k8s/module.rs +--- +#[automatically_derived] +pub(crate) mod v1alpha1 { + use super::*; + #[derive(::kube::CustomResource)] + #[kube( + group = "foo.example.org", + version = "v1alpha1", + kind = "Foo", + plural = "foos", + namespaced + )] + pub struct FooSpec { + pub bar: usize, + pub foo: String, + } + #[derive(::kube::CustomResource)] + #[kube( + group = "bar.example.org", + version = "v1alpha1", + kind = "Bar", + plural = "bars" + )] + pub struct BarSpec { + pub baz: String, + } + pub struct Baz { + pub boom: Option, + } +} +#[automatically_derived] +impl ::std::convert::From for v1::FooSpec { + fn from(__sv_foospec: v1alpha1::FooSpec) -> Self { + Self { + bar: __sv_foospec.bar, + baz: ::std::default::Default::default(), + foo: __sv_foospec.foo, + } + } +} +#[automatically_derived] +impl ::std::convert::From for v1::BarSpec { + fn from(__sv_barspec: v1alpha1::BarSpec) -> Self { + Self { baz: __sv_barspec.baz } + } +} +#[automatically_derived] +impl ::std::convert::From for v1::Baz { + fn from(__sv_baz: v1alpha1::Baz) -> Self { + Self { boom: __sv_baz.boom } + } +} +#[automatically_derived] +pub(crate) mod v1 { + use super::*; + #[derive(::kube::CustomResource)] + #[kube( + group = "foo.example.org", + version = "v1", + kind = "Foo", + plural = "foos", + namespaced + )] + pub struct FooSpec { + pub bar: usize, + pub baz: bool, + pub foo: String, + } + #[derive(::kube::CustomResource)] + #[kube(group = "bar.example.org", version = "v1", kind = "Bar", plural = "bars")] + pub struct BarSpec { + pub baz: String, + } + pub struct Baz { + pub boom: Option, + } +} +#[automatically_derived] +#[allow(deprecated)] +impl ::std::convert::From for v2alpha1::FooSpec { + fn from(__sv_foospec: v1::FooSpec) -> Self { + Self { + bar: __sv_foospec.bar, + baz: __sv_foospec.baz, + deprecated_foo: __sv_foospec.foo, + } + } +} +#[automatically_derived] +impl ::std::convert::From for v2alpha1::BarSpec { + fn from(__sv_barspec: v1::BarSpec) -> Self { + Self { baz: __sv_barspec.baz } + } +} +#[automatically_derived] +impl ::std::convert::From for v2alpha1::Baz { + fn from(__sv_baz: v1::Baz) -> Self { + Self { boom: __sv_baz.boom } + } +} +#[automatically_derived] +pub(crate) mod v2alpha1 { + use super::*; + #[derive(::kube::CustomResource)] + #[kube( + group = "foo.example.org", + version = "v2alpha1", + kind = "Foo", + plural = "foos", + namespaced + )] + pub struct FooSpec { + pub bar: usize, + pub baz: bool, + #[deprecated] + pub deprecated_foo: String, + } + #[derive(::kube::CustomResource)] + #[kube( + group = "bar.example.org", + version = "v2alpha1", + kind = "Bar", + plural = "bars" + )] + pub struct BarSpec { + pub baz: String, + } + pub struct Baz { + pub boom: Option, + } +} +#[automatically_derived] +pub enum Foo { + V1Alpha1, + 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"), + Self::V1Alpha1 => f.write_str("v1alpha1"), + } + } +} +#[automatically_derived] +impl Foo { + /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + 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::CustomResourceExt > ::crd(), < v1alpha1::Bar + as ::kube::CustomResourceExt > ::crd() + ], + &stored_apiversion.to_string(), + ) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to a file located at `path`. + pub fn write_merged_crd

( + path: P, + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> + where + P: AsRef<::std::path::Path>, + { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::write_yaml_schema( + &merged_crd, + path, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to stdout. + pub fn print_merged_crd( + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::print_yaml_schema( + &merged_crd, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } +} +#[automatically_derived] +pub enum Bar { + V1, + V1, +} +#[automatically_derived] +impl ::std::fmt::Display for Bar { + fn fmt( + &self, + f: &mut ::std::fmt::Formatter<'_>, + ) -> ::std::result::Result<(), ::std::fmt::Error> { + match self { + Self::V1 => f.write_str("v1"), + Self::V1 => f.write_str("v1"), + } + } +} +#[automatically_derived] +impl Bar { + /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + 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![ + < v1::Foo as ::kube::CustomResourceExt > ::crd(), < v1::Bar as + ::kube::CustomResourceExt > ::crd() + ], + &stored_apiversion.to_string(), + ) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to a file located at `path`. + pub fn write_merged_crd

( + path: P, + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> + where + P: AsRef<::std::path::Path>, + { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::write_yaml_schema( + &merged_crd, + path, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to stdout. + pub fn print_merged_crd( + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::print_yaml_schema( + &merged_crd, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } +} +#[automatically_derived] +pub enum Baz { + V2Alpha1, + V2Alpha1, +} +#[automatically_derived] +impl ::std::fmt::Display for Baz { + fn fmt( + &self, + f: &mut ::std::fmt::Formatter<'_>, + ) -> ::std::result::Result<(), ::std::fmt::Error> { + match self { + Self::V2Alpha1 => f.write_str("v2alpha1"), + Self::V2Alpha1 => f.write_str("v2alpha1"), + } + } +} +#[automatically_derived] +impl Baz { + /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + 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![ + < v2alpha1::Foo as ::kube::CustomResourceExt > ::crd(), < v2alpha1::Bar + as ::kube::CustomResourceExt > ::crd() + ], + &stored_apiversion.to_string(), + ) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to a file located at `path`. + pub fn write_merged_crd

( + path: P, + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> + where + P: AsRef<::std::path::Path>, + { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::write_yaml_schema( + &merged_crd, + path, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to stdout. + pub fn print_merged_crd( + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::print_yaml_schema( + &merged_crd, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } +} diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module_preserve.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module_preserve.rs.snap new file mode 100644 index 000000000..57659da87 --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@module_preserve.rs.snap @@ -0,0 +1,360 @@ +--- +source: crates/stackable-versioned-macros/src/lib.rs +expression: formatted +input_file: crates/stackable-versioned-macros/fixtures/inputs/k8s/module_preserve.rs +--- +#[automatically_derived] +pub(crate) mod versioned { + pub mod v1alpha1 { + use super::*; + #[derive(::kube::CustomResource)] + #[kube( + group = "foo.example.org", + version = "v1alpha1", + kind = "Foo", + plural = "foos", + namespaced + )] + pub struct FooSpec { + pub bar: usize, + pub foo: String, + } + #[derive(::kube::CustomResource)] + #[kube( + group = "bar.example.org", + version = "v1alpha1", + kind = "Bar", + plural = "bars" + )] + pub struct BarSpec { + pub baz: String, + } + pub struct Baz { + pub boom: Option, + } + } + impl ::std::convert::From for v1::FooSpec { + fn from(__sv_foospec: v1alpha1::FooSpec) -> Self { + Self { + bar: __sv_foospec.bar, + baz: ::std::default::Default::default(), + foo: __sv_foospec.foo, + } + } + } + impl ::std::convert::From for v1::BarSpec { + fn from(__sv_barspec: v1alpha1::BarSpec) -> Self { + Self { baz: __sv_barspec.baz } + } + } + impl ::std::convert::From for v1::Baz { + fn from(__sv_baz: v1alpha1::Baz) -> Self { + Self { boom: __sv_baz.boom } + } + } + pub mod v1 { + use super::*; + #[derive(::kube::CustomResource)] + #[kube( + group = "foo.example.org", + version = "v1", + kind = "Foo", + plural = "foos", + namespaced + )] + pub struct FooSpec { + pub bar: usize, + pub baz: bool, + pub foo: String, + } + #[derive(::kube::CustomResource)] + #[kube(group = "bar.example.org", version = "v1", kind = "Bar", plural = "bars")] + pub struct BarSpec { + pub baz: String, + } + pub struct Baz { + pub boom: Option, + } + } + #[allow(deprecated)] + impl ::std::convert::From for v2alpha1::FooSpec { + fn from(__sv_foospec: v1::FooSpec) -> Self { + Self { + bar: __sv_foospec.bar, + baz: __sv_foospec.baz, + deprecated_foo: __sv_foospec.foo, + } + } + } + impl ::std::convert::From for v2alpha1::BarSpec { + fn from(__sv_barspec: v1::BarSpec) -> Self { + Self { baz: __sv_barspec.baz } + } + } + impl ::std::convert::From for v2alpha1::Baz { + fn from(__sv_baz: v1::Baz) -> Self { + Self { boom: __sv_baz.boom } + } + } + pub mod v2alpha1 { + use super::*; + #[derive(::kube::CustomResource)] + #[kube( + group = "foo.example.org", + version = "v2alpha1", + kind = "Foo", + plural = "foos", + namespaced + )] + pub struct FooSpec { + pub bar: usize, + pub baz: bool, + #[deprecated] + pub deprecated_foo: String, + } + #[derive(::kube::CustomResource)] + #[kube( + group = "bar.example.org", + version = "v2alpha1", + kind = "Bar", + plural = "bars" + )] + pub struct BarSpec { + pub baz: String, + } + pub struct Baz { + pub boom: Option, + } + } + pub enum Foo { + V1Alpha1, + V1Alpha1, + } + 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"), + Self::V1Alpha1 => f.write_str("v1alpha1"), + } + } + } + impl Foo { + /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + 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::CustomResourceExt > ::crd(), < + v1alpha1::Bar as ::kube::CustomResourceExt > ::crd() + ], + &stored_apiversion.to_string(), + ) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to a file located at `path`. + pub fn write_merged_crd

( + path: P, + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> + where + P: AsRef<::std::path::Path>, + { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::write_yaml_schema( + &merged_crd, + path, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to stdout. + pub fn print_merged_crd( + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::print_yaml_schema( + &merged_crd, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + } + pub enum Bar { + V1, + V1, + } + impl ::std::fmt::Display for Bar { + fn fmt( + &self, + f: &mut ::std::fmt::Formatter<'_>, + ) -> ::std::result::Result<(), ::std::fmt::Error> { + match self { + Self::V1 => f.write_str("v1"), + Self::V1 => f.write_str("v1"), + } + } + } + impl Bar { + /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + 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![ + < v1::Foo as ::kube::CustomResourceExt > ::crd(), < v1::Bar as + ::kube::CustomResourceExt > ::crd() + ], + &stored_apiversion.to_string(), + ) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to a file located at `path`. + pub fn write_merged_crd

( + path: P, + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> + where + P: AsRef<::std::path::Path>, + { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::write_yaml_schema( + &merged_crd, + path, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to stdout. + pub fn print_merged_crd( + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::print_yaml_schema( + &merged_crd, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + } + pub enum Baz { + V2Alpha1, + V2Alpha1, + } + impl ::std::fmt::Display for Baz { + fn fmt( + &self, + f: &mut ::std::fmt::Formatter<'_>, + ) -> ::std::result::Result<(), ::std::fmt::Error> { + match self { + Self::V2Alpha1 => f.write_str("v2alpha1"), + Self::V2Alpha1 => f.write_str("v2alpha1"), + } + } + } + impl Baz { + /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + 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![ + < v2alpha1::Foo as ::kube::CustomResourceExt > ::crd(), < + v2alpha1::Bar as ::kube::CustomResourceExt > ::crd() + ], + &stored_apiversion.to_string(), + ) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to a file located at `path`. + pub fn write_merged_crd

( + path: P, + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> + where + P: AsRef<::std::path::Path>, + { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::write_yaml_schema( + &merged_crd, + path, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + /// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]` + /// macro to stdout. + pub fn print_merged_crd( + stored_apiversion: Self, + operator_version: &str, + ) -> Result<(), ::stackable_versioned::Error> { + use ::stackable_shared::yaml::{YamlSchema, SerializeOptions}; + let merged_crd = Self::merged_crd(stored_apiversion) + .map_err(|err| ::stackable_versioned::Error::MergeCrd { + source: err, + })?; + YamlSchema::print_yaml_schema( + &merged_crd, + operator_version, + SerializeOptions::default(), + ) + .map_err(|err| ::stackable_versioned::Error::SerializeYaml { + source: err, + }) + } + } +} From 2a2a94f65de3fef3e2245ea548ece29a8bffc236 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 25 Nov 2024 15:58:12 +0100 Subject: [PATCH 3/3] chore: Add changelog entry --- crates/stackable-versioned/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 280a7d795..475ee82cc 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -13,7 +13,12 @@ All notable changes to this project will be documented in this file. - Bump Rust to 1.82.0 ([#891]). +### Fixed + +- Correctly emit Kubernetes code when macro is used on modules ([#912]). + [#891]: https://github.com/stackabletech/operator-rs/pull/891 +[#912]: https://github.com/stackabletech/operator-rs/pull/912 ## [0.4.1] - 2024-10-23