From 9f93e2fe19b07b9af74593f0d6ce170e7176710e Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 22 Nov 2024 11:26:09 +0100 Subject: [PATCH] feat: Add storage and served argument to derive macro (#1644) * feat: Add storage and served argument to derive macro Signed-off-by: Techassi * chore: Update macro doc comment Signed-off-by: Techassi * test: Adjust schema test to assert arguments work Signed-off-by: Techassi * chore: Re-apply correct nightly formatting Signed-off-by: Techassi --------- Signed-off-by: Techassi --- kube-derive/src/custom_resource.rs | 28 ++++++++++++++++++++++++++-- kube-derive/src/lib.rs | 6 ++++++ kube-derive/tests/crd_schema_test.rs | 6 ++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/kube-derive/src/custom_resource.rs b/kube-derive/src/custom_resource.rs index 40deeef7f..055664f31 100644 --- a/kube-derive/src/custom_resource.rs +++ b/kube-derive/src/custom_resource.rs @@ -41,6 +41,18 @@ struct KubeAttrs { annotations: Vec, #[darling(multiple, rename = "label")] labels: Vec, + + /// 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)] @@ -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")] @@ -201,6 +223,8 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea printcolums, selectable, scale, + storage, + served, crates: Crates { kube_core, @@ -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, }, diff --git a/kube-derive/src/lib.rs b/kube-derive/src/lib.rs index b349b3762..36b7df07c 100644 --- a/kube-derive/src/lib.rs +++ b/kube-derive/src/lib.rs @@ -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 diff --git a/kube-derive/tests/crd_schema_test.rs b/kube-derive/tests/crd_schema_test.rs index 5086e1e66..e975d8ff3 100644 --- a/kube-derive/tests/crd_schema_test.rs +++ b/kube-derive/tests/crd_schema_test.rs @@ -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"), @@ -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"