From 89625289e1c6b8e1eeb66b9662979665c21eafd9 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 21 Nov 2024 13:14:11 +0100 Subject: [PATCH 1/4] feat: Add storage and served argument to derive macro Signed-off-by: Techassi --- kube-derive/src/custom_resource.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 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, }, From a5d1bdf96b32d14a0d26b9de5b17c49b715492f8 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 21 Nov 2024 13:14:31 +0100 Subject: [PATCH 2/4] chore: Update macro doc comment Signed-off-by: Techassi --- kube-derive/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kube-derive/src/lib.rs b/kube-derive/src/lib.rs index b349b3762..fe9647485 100644 --- a/kube-derive/src/lib.rs +++ b/kube-derive/src/lib.rs @@ -1,7 +1,8 @@ //! A crate for kube's derive macros. #![recursion_limit = "1024"] extern crate proc_macro; -#[macro_use] extern crate quote; +#[macro_use] +extern crate quote; mod custom_resource; mod resource; @@ -154,6 +155,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 From fda8d5266b209d05715815af4f9e323588bb6c20 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 21 Nov 2024 13:15:14 +0100 Subject: [PATCH 3/4] test: Adjust schema test to assert arguments work Signed-off-by: Techassi --- kube-derive/tests/crd_schema_test.rs | 39 ++++++++++++++++------------ 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/kube-derive/tests/crd_schema_test.rs b/kube-derive/tests/crd_schema_test.rs index 5086e1e66..81a95e81b 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"), @@ -134,21 +136,24 @@ fn test_shortnames() { #[test] fn test_serialized_matches_expected() { assert_json_eq!( - serde_json::to_value(Foo::new("bar", FooSpec { - non_nullable: "asdf".to_string(), - non_nullable_with_default: "asdf".to_string(), - nullable_skipped: None, - nullable: None, - nullable_skipped_with_default: None, - nullable_with_default: None, - timestamp: DateTime::from_timestamp(0, 0).unwrap(), - complex_enum: ComplexEnum::VariantOne { int: 23 }, - untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge { - age: 42, - gender: Gender::Male, - }), - set: HashSet::from(["foo".to_owned()]) - })) + serde_json::to_value(Foo::new( + "bar", + FooSpec { + non_nullable: "asdf".to_string(), + non_nullable_with_default: "asdf".to_string(), + nullable_skipped: None, + nullable: None, + nullable_skipped_with_default: None, + nullable_with_default: None, + timestamp: DateTime::from_timestamp(0, 0).unwrap(), + complex_enum: ComplexEnum::VariantOne { int: 23 }, + untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge { + age: 42, + gender: Gender::Male, + }), + set: HashSet::from(["foo".to_owned()]) + } + )) .unwrap(), serde_json::json!({ "apiVersion": "clux.dev/v1", @@ -217,8 +222,8 @@ fn test_crd_schema_matches_expected() { "versions": [ { "name": "v1", - "served": true, - "storage": true, + "served": false, + "storage": false, "additionalPrinterColumns": [], "selectableFields": [{ "jsonPath": ".spec.nonNullable" From 99d3ebcd2deecd03d5c48f2b22148001b944757d Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 22 Nov 2024 09:44:36 +0100 Subject: [PATCH 4/4] chore: Re-apply correct nightly formatting Signed-off-by: Techassi --- kube-derive/src/lib.rs | 3 +-- kube-derive/tests/crd_schema_test.rs | 33 +++++++++++++--------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/kube-derive/src/lib.rs b/kube-derive/src/lib.rs index fe9647485..36b7df07c 100644 --- a/kube-derive/src/lib.rs +++ b/kube-derive/src/lib.rs @@ -1,8 +1,7 @@ //! A crate for kube's derive macros. #![recursion_limit = "1024"] extern crate proc_macro; -#[macro_use] -extern crate quote; +#[macro_use] extern crate quote; mod custom_resource; mod resource; diff --git a/kube-derive/tests/crd_schema_test.rs b/kube-derive/tests/crd_schema_test.rs index 81a95e81b..e975d8ff3 100644 --- a/kube-derive/tests/crd_schema_test.rs +++ b/kube-derive/tests/crd_schema_test.rs @@ -136,24 +136,21 @@ fn test_shortnames() { #[test] fn test_serialized_matches_expected() { assert_json_eq!( - serde_json::to_value(Foo::new( - "bar", - FooSpec { - non_nullable: "asdf".to_string(), - non_nullable_with_default: "asdf".to_string(), - nullable_skipped: None, - nullable: None, - nullable_skipped_with_default: None, - nullable_with_default: None, - timestamp: DateTime::from_timestamp(0, 0).unwrap(), - complex_enum: ComplexEnum::VariantOne { int: 23 }, - untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge { - age: 42, - gender: Gender::Male, - }), - set: HashSet::from(["foo".to_owned()]) - } - )) + serde_json::to_value(Foo::new("bar", FooSpec { + non_nullable: "asdf".to_string(), + non_nullable_with_default: "asdf".to_string(), + nullable_skipped: None, + nullable: None, + nullable_skipped_with_default: None, + nullable_with_default: None, + timestamp: DateTime::from_timestamp(0, 0).unwrap(), + complex_enum: ComplexEnum::VariantOne { int: 23 }, + untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge { + age: 42, + gender: Gender::Male, + }), + set: HashSet::from(["foo".to_owned()]) + })) .unwrap(), serde_json::json!({ "apiVersion": "clux.dev/v1",