Skip to content

Commit

Permalink
feat: Add deprecated argument to derive macro (#1697)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Techassi and clux authored Feb 19, 2025
1 parent c191439 commit 8876639
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
21 changes: 20 additions & 1 deletion kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated by darling macros, out of our control
#![allow(clippy::manual_unwrap_or_default)]
use darling::{FromDeriveInput, FromMeta};
use darling::{util::Override, FromDeriveInput, FromMeta};
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::{ToTokens, TokenStreamExt as _};
use serde::Deserialize;
Expand Down Expand Up @@ -60,6 +60,11 @@ struct KubeAttrs {
/// Defaults to `true`.
#[darling(default = default_served_arg)]
served: bool,

/// Sets the `deprecated` and optionally the `deprecationWarning` property.
///
/// See https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-deprecation
deprecated: Option<Override<String>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -356,6 +361,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
rules,
storage,
served,
deprecated,
crates:
Crates {
kube_core,
Expand Down Expand Up @@ -640,6 +646,18 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
quote! {}
};

let deprecation = if let Some(deprecation) = deprecated {
match deprecation {
Override::Inherit => quote! { "deprecated": true, },
Override::Explicit(warning) => quote! {
"deprecated": true,
"deprecationWarning": #warning,
},
}
} else {
quote! {}
};

// Known constraints that are hard to enforce elsewhere
let compile_constraints = if !selectable.is_empty() {
quote! {
Expand Down Expand Up @@ -672,6 +690,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
"name": #version,
"served": #served,
"storage": #storage,
#deprecation
"schema": {
"openAPIV3Schema": schema,
},
Expand Down
13 changes: 13 additions & 0 deletions kube-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ mod resource;
/// ## `#[kube(served = true)]`
/// Sets the `served` property to `true` or `false`.
///
/// ## `#[kube(deprecated [= "warning"])]`
/// Sets the `deprecated` property to `true`.
///
/// ```ignore
/// #[kube(deprecated)]
/// ```
///
/// Aditionally, you can provide a `deprecationWarning` using the following example.
///
/// ```ignore
/// #[kube(deprecated = "Replaced by other CRD")]
/// ```
///
/// ## `#[kube(rule = Rule::new("self == oldSelf").message("field is immutable"))]`
/// Inject a top level CEL validation rule for the top level generated struct.
/// This attribute is for resources deriving [`CELSchema`] instead of [`schemars::JsonSchema`].
Expand Down
3 changes: 3 additions & 0 deletions kube-derive/tests/crd_schema_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::collections::{HashMap, HashSet};
shortname = "f",
served = false,
storage = false,
deprecated = "my warning",
selectable = ".spec.nonNullable",
selectable = ".spec.nullable",
annotation("clux.dev", "cluxingv1"),
Expand Down Expand Up @@ -239,6 +240,8 @@ fn test_crd_schema_matches_expected() {
"name": "v1",
"served": false,
"storage": false,
"deprecated": true,
"deprecationWarning": "my warning",
"additionalPrinterColumns": [],
"selectableFields": [{
"jsonPath": ".spec.nonNullable"
Expand Down

0 comments on commit 8876639

Please sign in to comment.