Skip to content

Commit

Permalink
feat: Add storage and served argument to derive macro (#1644)
Browse files Browse the repository at this point in the history
* feat: Add storage and served argument to derive macro

Signed-off-by: Techassi <[email protected]>

* chore: Update macro doc comment

Signed-off-by: Techassi <[email protected]>

* test: Adjust schema test to assert arguments work

Signed-off-by: Techassi <[email protected]>

* chore: Re-apply correct nightly formatting

Signed-off-by: Techassi <[email protected]>

---------

Signed-off-by: Techassi <[email protected]>
  • Loading branch information
Techassi authored Nov 22, 2024
1 parent fa7b7f2 commit 9f93e2f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
28 changes: 26 additions & 2 deletions kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ struct KubeAttrs {
annotations: Vec<KVTuple>,
#[darling(multiple, rename = "label")]
labels: Vec<KVTuple>,

/// Sets the `storage` property to `true` or `false`.
///
/// Defaults to `true`.
#[darling(default = default_storage_arg)]
storage: bool,

/// Sets the `served` property to `true` or `false`.
///
/// Defaults to `true`.
#[darling(default = default_served_arg)]
served: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -77,6 +89,16 @@ impl ToTokens for KVTuple {
}
}

fn default_storage_arg() -> bool {
// This defaults to true to be backwards compatible.
true
}

fn default_served_arg() -> bool {
// This defaults to true to be backwards compatible.
true
}

#[derive(Debug, FromMeta)]
struct Crates {
#[darling(default = "Self::default_kube_core")]
Expand Down Expand Up @@ -201,6 +223,8 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
printcolums,
selectable,
scale,
storage,
served,
crates:
Crates {
kube_core,
Expand Down Expand Up @@ -505,8 +529,8 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
},
"versions": [{
"name": #version,
"served": true,
"storage": true,
"served": #served,
"storage": #storage,
"schema": {
"openAPIV3Schema": schema,
},
Expand Down
6 changes: 6 additions & 0 deletions kube-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ mod resource;
/// ## `#[kube(label("LABEL_KEY", "LABEL_VALUE"))]`
/// Add a single label to the generated CRD.
///
/// ## `#[kube(storage = true)]`
/// Sets the `storage` property to `true` or `false`.
///
/// ## `#[kube(served = true)]`
/// Sets the `served` property to `true` or `false`.
///
/// ## Example with all properties
///
/// ```rust
Expand Down
6 changes: 4 additions & 2 deletions kube-derive/tests/crd_schema_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::collections::{HashMap, HashSet};
derive = "PartialEq",
shortname = "fo",
shortname = "f",
served = false,
storage = false,
selectable = ".spec.nonNullable",
selectable = ".spec.nullable",
annotation("clux.dev", "cluxingv1"),
Expand Down Expand Up @@ -217,8 +219,8 @@ fn test_crd_schema_matches_expected() {
"versions": [
{
"name": "v1",
"served": true,
"storage": true,
"served": false,
"storage": false,
"additionalPrinterColumns": [],
"selectableFields": [{
"jsonPath": ".spec.nonNullable"
Expand Down

0 comments on commit 9f93e2f

Please sign in to comment.