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 4 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 @@ -118,6 +118,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 @@ -166,7 +169,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,
}
16 changes: 15 additions & 1 deletion crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,30 @@ impl VersionedStruct {
/// attributes.
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 });

return Some(quote! {
#[derive(::kube::CustomResource)]
#[kube(group = #group, version = #version, kind = #kind)]
#[kube(group = #group, version = #version, kind = #kind #singular #plural #namespaced)]
});
}

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
7 changes: 7 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- Add forwarding of `singular`, `plural`, and `namespaced` arguments in `k8s()`
([#873]).

[#873]: https://github.com/stackabletech/operator-rs/pull/873

## [0.2.0] - 2024-09-19

### Added
Expand Down