From 5dcb07b65535873000424077ead703dc07339d0d Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 29 Nov 2024 10:52:10 +0100 Subject: [PATCH] feat(stackable-versioned): Support visibility for CRD enum (#923) * feat(stackable-versioned): Support visibility for CRD enum * chore(stackable-versioned): Update changelog --- .../fixtures/inputs/k8s/basic.rs | 2 +- ...versioned_macros__test__k8s_snapshots@basic.rs.snap | 10 +++++----- ...macros__test__k8s_snapshots@crate_overrides.rs.snap | 2 +- ...ersioned_macros__test__k8s_snapshots@module.rs.snap | 8 ++++---- ...macros__test__k8s_snapshots@module_preserve.rs.snap | 4 ++-- ...ed_macros__test__k8s_snapshots@renamed_kind.rs.snap | 2 +- .../src/codegen/container/mod.rs | 3 +++ .../src/codegen/container/struct.rs | 8 ++++---- .../stackable-versioned-macros/src/codegen/module.rs | 1 + crates/stackable-versioned/CHANGELOG.md | 2 ++ 10 files changed, 24 insertions(+), 18 deletions(-) diff --git a/crates/stackable-versioned-macros/fixtures/inputs/k8s/basic.rs b/crates/stackable-versioned-macros/fixtures/inputs/k8s/basic.rs index 3d0251c9..275c43b8 100644 --- a/crates/stackable-versioned-macros/fixtures/inputs/k8s/basic.rs +++ b/crates/stackable-versioned-macros/fixtures/inputs/k8s/basic.rs @@ -13,7 +13,7 @@ #[derive( Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema, kube::CustomResource, )] -pub struct FooSpec { +pub(crate) struct FooSpec { #[versioned( added(since = "v1beta1"), changed(since = "v1", from_name = "bah", from_type = "u16") diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@basic.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@basic.rs.snap index 24ed425b..920c7913 100644 --- a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@basic.rs.snap +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@basic.rs.snap @@ -4,7 +4,7 @@ expression: formatted input_file: crates/stackable-versioned-macros/fixtures/inputs/k8s/basic.rs --- #[automatically_derived] -pub mod v1alpha1 { +pub(crate) mod v1alpha1 { use super::*; #[derive( Clone, @@ -36,7 +36,7 @@ impl ::std::convert::From for v1beta1::FooSpec { } } #[automatically_derived] -pub mod v1beta1 { +pub(crate) mod v1beta1 { use super::*; #[derive( Clone, @@ -69,7 +69,7 @@ impl ::std::convert::From for v1::FooSpec { } } #[automatically_derived] -pub mod v1 { +pub(crate) mod v1 { use super::*; #[derive( Clone, @@ -93,7 +93,7 @@ pub mod v1 { } } #[automatically_derived] -pub enum Foo { +pub(crate) enum Foo { V1Alpha1, V1Beta1, V1, @@ -113,7 +113,7 @@ impl ::std::fmt::Display for Foo { } #[automatically_derived] impl Foo { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. pub fn merged_crd( stored_apiversion: Self, ) -> ::std::result::Result< diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@crate_overrides.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@crate_overrides.rs.snap index 121bc32e..521a9712 100644 --- a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@crate_overrides.rs.snap +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@crate_overrides.rs.snap @@ -116,7 +116,7 @@ impl ::std::fmt::Display for Foo { } #[automatically_derived] impl Foo { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. pub fn merged_crd( stored_apiversion: Self, ) -> ::std::result::Result< 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 index 099c62bf..7af1303a 100644 --- 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 @@ -206,7 +206,7 @@ pub(crate) mod v2alpha1 { } } #[automatically_derived] -pub enum Foo { +pub(crate) enum Foo { V1Alpha1, V1, V2Alpha1, @@ -226,7 +226,7 @@ impl ::std::fmt::Display for Foo { } #[automatically_derived] impl Foo { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. pub fn merged_crd( stored_apiversion: Self, ) -> ::std::result::Result< @@ -289,7 +289,7 @@ impl Foo { } } #[automatically_derived] -pub enum Bar { +pub(crate) enum Bar { V1Alpha1, V1, V2Alpha1, @@ -309,7 +309,7 @@ impl ::std::fmt::Display for Bar { } #[automatically_derived] impl Bar { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. pub fn merged_crd( stored_apiversion: Self, ) -> ::std::result::Result< 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 index 01f4c033..feddee61 100644 --- 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 @@ -214,7 +214,7 @@ pub(crate) mod versioned { } } impl Foo { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. pub fn merged_crd( stored_apiversion: Self, ) -> ::std::result::Result< @@ -294,7 +294,7 @@ pub(crate) mod versioned { } } impl Bar { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. pub fn merged_crd( stored_apiversion: Self, ) -> ::std::result::Result< diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@renamed_kind.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@renamed_kind.rs.snap index a64f9a35..85f1c77a 100644 --- a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@renamed_kind.rs.snap +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@renamed_kind.rs.snap @@ -113,7 +113,7 @@ impl ::std::fmt::Display for FooBar { } #[automatically_derived] impl FooBar { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// Generates a merged CRD containing all versions and marking `stored_apiversion` as stored. pub fn merged_crd( stored_apiversion: Self, ) -> ::std::result::Result< diff --git a/crates/stackable-versioned-macros/src/codegen/container/mod.rs b/crates/stackable-versioned-macros/src/codegen/container/mod.rs index f05cef9a..580c857d 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/mod.rs @@ -93,6 +93,7 @@ impl Container { enum_variant_idents: &[IdentString], enum_variant_strings: &[String], fn_calls: &[TokenStream], + vis: &Visibility, is_nested: bool, ) -> Option { match self { @@ -100,6 +101,7 @@ impl Container { enum_variant_idents, enum_variant_strings, fn_calls, + vis, is_nested, ), Container::Enum(_) => None, @@ -216,6 +218,7 @@ impl StandaloneContainer { &kubernetes_enum_variant_idents, &kubernetes_enum_variant_strings, &kubernetes_merge_crds_fn_calls, + vis, false, )); diff --git a/crates/stackable-versioned-macros/src/codegen/container/struct.rs b/crates/stackable-versioned-macros/src/codegen/container/struct.rs index 2295ae9a..68d9e54d 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/struct.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/struct.rs @@ -3,7 +3,7 @@ use std::ops::Not; use darling::{util::IdentString, Error, FromAttributes, Result}; use proc_macro2::TokenStream; use quote::{quote, ToTokens}; -use syn::{parse_quote, ItemStruct, Path}; +use syn::{parse_quote, ItemStruct, Path, Visibility}; use crate::{ attrs::container::NestedContainerAttributes, @@ -332,6 +332,7 @@ impl Struct { enum_variant_idents: &[IdentString], enum_variant_strings: &[String], fn_calls: &[TokenStream], + vis: &Visibility, is_nested: bool, ) -> Option { match &self.common.options.kubernetes_options { @@ -347,13 +348,12 @@ impl Struct { let k8s_openapi_path = &*kubernetes_options.crates.k8s_openapi; let kube_core_path = &*kubernetes_options.crates.kube_core; - // TODO (@Techassi): Use proper visibility instead of hard-coding 'pub' // TODO (@Techassi): Move the YAML printing code into 'stackable-versioned' so that we don't // have any cross-dependencies and the macro can be used on it's own (K8s features of course // still need kube and friends). Some(quote! { #automatically_derived - pub enum #enum_ident { + #vis enum #enum_ident { #(#enum_variant_idents),* } @@ -368,7 +368,7 @@ impl Struct { #automatically_derived impl #enum_ident { - /// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro. + /// 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_path::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition, #kube_core_path::crd::MergeError> { diff --git a/crates/stackable-versioned-macros/src/codegen/module.rs b/crates/stackable-versioned-macros/src/codegen/module.rs index 709a6989..ab888973 100644 --- a/crates/stackable-versioned-macros/src/codegen/module.rs +++ b/crates/stackable-versioned-macros/src/codegen/module.rs @@ -137,6 +137,7 @@ impl Module { kubernetes_enum_variant_idents, kubernetes_enum_variant_strings, kubernetes_merge_crds_fn_calls, + version_module_vis, self.preserve_module, )); } diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index bffa7bd9..4ed7f606 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Added +- Use visibility of container definition or module for generated CRD enum ([#923]). - Add support to apply the `#[versioned()]` macro to modules to version all contained items at once ([#891]). - Add support for passing a `status`, `crates`, and `shortname` arguments through to the `#[kube]` @@ -35,6 +36,7 @@ All notable changes to this project will be documented in this file. [#919]: https://github.com/stackabletech/operator-rs/pull/919 [#920]: https://github.com/stackabletech/operator-rs/pull/920 [#922]: https://github.com/stackabletech/operator-rs/pull/922 +[#923]: https://github.com/stackabletech/operator-rs/pull/923 ## [0.4.1] - 2024-10-23