Skip to content

Commit 121a349

Browse files
committed
feat(stackable-versioned): Forward selected kube args
1 parent 7fe86d5 commit 121a349

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ impl<I> VersionedContainer<I> {
118118

119119
let kubernetes_options = attributes.kubernetes_attrs.map(|a| KubernetesOptions {
120120
skip_merged_crd: a.skip.map_or(false, |s| s.merged_crd.is_present()),
121+
namespaced: a.namespaced.is_present(),
122+
singular: a.singular,
123+
plural: a.plural,
121124
group: a.group,
122125
kind: a.kind,
123126
});
@@ -166,7 +169,10 @@ pub(crate) struct VersionedContainerOptions {
166169

167170
#[derive(Debug)]
168171
pub(crate) struct KubernetesOptions {
172+
pub(crate) singular: Option<String>,
173+
pub(crate) plural: Option<String>,
169174
pub(crate) skip_merged_crd: bool,
170175
pub(crate) kind: Option<String>,
176+
pub(crate) namespaced: bool,
171177
pub(crate) group: String,
172178
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,30 @@ impl VersionedStruct {
253253
/// attributes.
254254
fn generate_kubernetes_cr_derive(&self, version: &ContainerVersion) -> Option<TokenStream> {
255255
if let Some(kubernetes_options) = &self.options.kubernetes_options {
256+
// Required arguments
256257
let group = &kubernetes_options.group;
257258
let version = version.inner.to_string();
258259
let kind = kubernetes_options
259260
.kind
260261
.as_ref()
261262
.map_or(self.idents.kubernetes.to_string(), |kind| kind.clone());
262263

264+
// Optional arguments
265+
let namespaced = kubernetes_options
266+
.namespaced
267+
.then_some(quote! { , namespaced });
268+
let singular = kubernetes_options
269+
.singular
270+
.as_ref()
271+
.map(|s| quote! { , singular = #s });
272+
let plural = kubernetes_options
273+
.plural
274+
.as_ref()
275+
.map(|p| quote! { , plural = #p });
276+
263277
return Some(quote! {
264278
#[derive(::kube::CustomResource)]
265-
#[kube(group = #group, version = #version, kind = #kind)]
279+
#[kube(group = #group, version = #version, kind = #kind #singular #plural #namespaced)]
266280
});
267281
}
268282

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

Lines changed: 9 additions & 0 deletions
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`: Specify that this is a namespaced resource rather than
473+
/// cluster level.
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

Lines changed: 6 additions & 1 deletion
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 {

0 commit comments

Comments
 (0)