Skip to content

Commit 34c6c4f

Browse files
Techassisbernauer
andauthored
feat(stackable-versioned): Forward selected kube args (#873)
* feat(stackable-versioned): Forward selected kube args * chore: Apply suggestion Co-authored-by: Sebastian Bernauer <[email protected]> * chore: Reword doc comment * chore: Add changelog entry --------- Co-authored-by: Sebastian Bernauer <[email protected]>
1 parent 9f774d1 commit 34c6c4f

File tree

6 files changed

+57
-18
lines changed

6 files changed

+57
-18
lines changed

crates/stackable-versioned-macros/src/attrs/common/container.rs

+3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ pub(crate) struct OptionAttributes {
141141
#[derive(Clone, Debug, FromMeta)]
142142
pub(crate) struct KubernetesAttributes {
143143
pub(crate) skip: Option<KubernetesSkipAttributes>,
144+
pub(crate) singular: Option<String>,
145+
pub(crate) plural: Option<String>,
144146
pub(crate) kind: Option<String>,
147+
pub(crate) namespaced: Flag,
145148
pub(crate) group: String,
146149
}
147150

crates/stackable-versioned-macros/src/codegen/common/container.rs

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ impl<I> VersionedContainer<I> {
130130

131131
let kubernetes_options = attributes.kubernetes_attrs.map(|a| KubernetesOptions {
132132
skip_merged_crd: a.skip.map_or(false, |s| s.merged_crd.is_present()),
133+
namespaced: a.namespaced.is_present(),
134+
singular: a.singular,
135+
plural: a.plural,
133136
group: a.group,
134137
kind: a.kind,
135138
});
@@ -178,7 +181,10 @@ pub(crate) struct VersionedContainerOptions {
178181

179182
#[derive(Debug)]
180183
pub(crate) struct KubernetesOptions {
184+
pub(crate) singular: Option<String>,
185+
pub(crate) plural: Option<String>,
181186
pub(crate) skip_merged_crd: bool,
182187
pub(crate) kind: Option<String>,
188+
pub(crate) namespaced: bool,
183189
pub(crate) group: String,
184190
}

crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs

+30-17
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use crate::{
99
attrs::common::ContainerAttributes,
1010
codegen::{
1111
common::{
12-
Container, ContainerInput, ContainerVersion, Item, KubernetesOptions, VersionExt,
13-
VersionedContainer,
12+
Container, ContainerInput, ContainerVersion, Item, VersionExt, VersionedContainer,
1413
},
1514
vstruct::field::VersionedField,
1615
},
@@ -152,7 +151,7 @@ impl VersionedStruct {
152151
Some(options) => {
153152
// Generate the CustomResource derive macro with the appropriate
154153
// attributes supplied using #[kube()].
155-
let cr_derive = self.generate_kubernetes_cr_derive(version, options);
154+
let cr_derive = self.generate_kubernetes_cr_derive(version);
156155

157156
// Generate merged_crd specific code when not opted out.
158157
let merged_crd = if !options.skip_merged_crd {
@@ -282,22 +281,36 @@ impl VersionedStruct {
282281
impl VersionedStruct {
283282
/// Generates the `kube::CustomResource` derive with the appropriate macro
284283
/// attributes.
285-
fn generate_kubernetes_cr_derive(
286-
&self,
287-
version: &ContainerVersion,
288-
options: &KubernetesOptions,
289-
) -> TokenStream {
290-
let group = &options.group;
291-
let version = version.inner.to_string();
292-
let kind = options
293-
.kind
294-
.as_ref()
295-
.map_or(self.idents.kubernetes.to_string(), |kind| kind.clone());
284+
fn generate_kubernetes_cr_derive(&self, version: &ContainerVersion) -> Option<TokenStream> {
285+
if let Some(kubernetes_options) = &self.options.kubernetes_options {
286+
// Required arguments
287+
let group = &kubernetes_options.group;
288+
let version = version.inner.to_string();
289+
let kind = kubernetes_options
290+
.kind
291+
.as_ref()
292+
.map_or(self.idents.kubernetes.to_string(), |kind| kind.clone());
293+
294+
// Optional arguments
295+
let namespaced = kubernetes_options
296+
.namespaced
297+
.then_some(quote! { , namespaced });
298+
let singular = kubernetes_options
299+
.singular
300+
.as_ref()
301+
.map(|s| quote! { , singular = #s });
302+
let plural = kubernetes_options
303+
.plural
304+
.as_ref()
305+
.map(|p| quote! { , plural = #p });
296306

297-
quote! {
298-
#[derive(::kube::CustomResource)]
299-
#[kube(group = #group, version = #version, kind = #kind)]
307+
return Some(quote! {
308+
#[derive(::kube::CustomResource)]
309+
#[kube(group = #group, version = #version, kind = #kind #singular #plural #namespaced)]
310+
});
300311
}
312+
313+
None
301314
}
302315

303316
/// Generates the `merge_crds` function call.

crates/stackable-versioned-macros/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,15 @@ println!("{}", serde_yaml::to_string(&merged_crd).unwrap());
462462
```
463463
"#
464464
)]
465+
/// Currently, the following arguments are supported:
466+
///
467+
/// - `group`: Sets the CRD group, usually the domain of the company.
468+
/// - `kind`: Allows overwriting the kind field of the CRD. This defaults
469+
/// to the struct name (without the 'Spec' suffix).
470+
/// - `singular`: Sets the singular name.
471+
/// - `plural`: Sets the plural name.
472+
/// - `namespaced`: Specifies that this is a namespaced resource rather than
473+
/// a cluster scoped.
465474
#[proc_macro_attribute]
466475
pub fn versioned(attrs: TokenStream, input: TokenStream) -> TokenStream {
467476
let attrs = match NestedMeta::parse_meta_list(attrs.into()) {

crates/stackable-versioned-macros/tests/k8s/pass/crd.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ fn main() {
99
version(name = "v1alpha1"),
1010
version(name = "v1beta1"),
1111
version(name = "v1"),
12-
k8s(group = "stackable.tech")
12+
k8s(
13+
group = "stackable.tech",
14+
singular = "foo",
15+
plural = "foos",
16+
namespaced,
17+
)
1318
)]
1419
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
1520
pub struct FooSpec {

crates/stackable-versioned/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.
66

77
### Added
88

9+
- Add forwarding of `singular`, `plural`, and `namespaced` arguments in `k8s()`
10+
([#873]).
911
- Generate a `Version` enum containing all declared versions as variants
1012
([#872]).
1113

@@ -15,6 +17,7 @@ All notable changes to this project will be documented in this file.
1517
input ([#872]).
1618

1719
[#872]: https://github.com/stackabletech/operator-rs/pull/872
20+
[#873]: https://github.com/stackabletech/operator-rs/pull/873
1821

1922
## [0.2.0] - 2024-09-19
2023

0 commit comments

Comments
 (0)