Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stackable-versioned): Forward selected kube args #873

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ pub(crate) struct OptionAttributes {
#[derive(Clone, Debug, FromMeta)]
pub(crate) struct KubernetesAttributes {
pub(crate) skip: Option<KubernetesSkipAttributes>,
pub(crate) singular: Option<String>,
pub(crate) plural: Option<String>,
pub(crate) kind: Option<String>,
pub(crate) namespaced: Flag,
pub(crate) group: String,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ impl<I> VersionedContainer<I> {

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,
});
Expand Down Expand Up @@ -178,7 +181,10 @@ pub(crate) struct VersionedContainerOptions {

#[derive(Debug)]
pub(crate) struct KubernetesOptions {
pub(crate) singular: Option<String>,
pub(crate) plural: Option<String>,
pub(crate) skip_merged_crd: bool,
pub(crate) kind: Option<String>,
pub(crate) namespaced: bool,
pub(crate) group: String,
}
47 changes: 30 additions & 17 deletions crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<TokenStream> {
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.
Expand Down
9 changes: 9 additions & 0 deletions crates/stackable-versioned-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
7 changes: 6 additions & 1 deletion crates/stackable-versioned-macros/tests/k8s/pass/crd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]).

Expand All @@ -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

Expand Down
Loading