Skip to content

Commit 8876639

Browse files
Techassiclux
andauthored
feat: Add deprecated argument to derive macro (#1697)
* feat: Add deprecated argument to derive macro Signed-off-by: Techassi <[email protected]> * docs: Simplify deprecated argument doc comments Signed-off-by: Techassi <[email protected]> * chore: Apply suggestion Co-authored-by: Eirik A <[email protected]> Signed-off-by: Techassi <[email protected]> --------- Signed-off-by: Techassi <[email protected]> Co-authored-by: Eirik A <[email protected]>
1 parent c191439 commit 8876639

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

kube-derive/src/custom_resource.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Generated by darling macros, out of our control
22
#![allow(clippy::manual_unwrap_or_default)]
3-
use darling::{FromDeriveInput, FromMeta};
3+
use darling::{util::Override, FromDeriveInput, FromMeta};
44
use proc_macro2::{Ident, Literal, Span, TokenStream};
55
use quote::{ToTokens, TokenStreamExt as _};
66
use serde::Deserialize;
@@ -60,6 +60,11 @@ struct KubeAttrs {
6060
/// Defaults to `true`.
6161
#[darling(default = default_served_arg)]
6262
served: bool,
63+
64+
/// Sets the `deprecated` and optionally the `deprecationWarning` property.
65+
///
66+
/// See https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-deprecation
67+
deprecated: Option<Override<String>>,
6368
}
6469

6570
#[derive(Debug)]
@@ -356,6 +361,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
356361
rules,
357362
storage,
358363
served,
364+
deprecated,
359365
crates:
360366
Crates {
361367
kube_core,
@@ -640,6 +646,18 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
640646
quote! {}
641647
};
642648

649+
let deprecation = if let Some(deprecation) = deprecated {
650+
match deprecation {
651+
Override::Inherit => quote! { "deprecated": true, },
652+
Override::Explicit(warning) => quote! {
653+
"deprecated": true,
654+
"deprecationWarning": #warning,
655+
},
656+
}
657+
} else {
658+
quote! {}
659+
};
660+
643661
// Known constraints that are hard to enforce elsewhere
644662
let compile_constraints = if !selectable.is_empty() {
645663
quote! {
@@ -672,6 +690,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
672690
"name": #version,
673691
"served": #served,
674692
"storage": #storage,
693+
#deprecation
675694
"schema": {
676695
"openAPIV3Schema": schema,
677696
},

kube-derive/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ mod resource;
175175
/// ## `#[kube(served = true)]`
176176
/// Sets the `served` property to `true` or `false`.
177177
///
178+
/// ## `#[kube(deprecated [= "warning"])]`
179+
/// Sets the `deprecated` property to `true`.
180+
///
181+
/// ```ignore
182+
/// #[kube(deprecated)]
183+
/// ```
184+
///
185+
/// Aditionally, you can provide a `deprecationWarning` using the following example.
186+
///
187+
/// ```ignore
188+
/// #[kube(deprecated = "Replaced by other CRD")]
189+
/// ```
190+
///
178191
/// ## `#[kube(rule = Rule::new("self == oldSelf").message("field is immutable"))]`
179192
/// Inject a top level CEL validation rule for the top level generated struct.
180193
/// This attribute is for resources deriving [`CELSchema`] instead of [`schemars::JsonSchema`].

kube-derive/tests/crd_schema_test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::collections::{HashMap, HashSet};
2323
shortname = "f",
2424
served = false,
2525
storage = false,
26+
deprecated = "my warning",
2627
selectable = ".spec.nonNullable",
2728
selectable = ".spec.nullable",
2829
annotation("clux.dev", "cluxingv1"),
@@ -239,6 +240,8 @@ fn test_crd_schema_matches_expected() {
239240
"name": "v1",
240241
"served": false,
241242
"storage": false,
243+
"deprecated": true,
244+
"deprecationWarning": "my warning",
242245
"additionalPrinterColumns": [],
243246
"selectableFields": [{
244247
"jsonPath": ".spec.nonNullable"

0 commit comments

Comments
 (0)