Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting a description on a derived CRD #1359

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/crd_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use serde::{Deserialize, Serialize};
plural = "fooz",
root = "FooCrd",
namespaced,
description = "Custom resource representing a Foo",
status = "FooStatus",
derive = "PartialEq",
derive = "Default",
Expand Down Expand Up @@ -119,7 +120,7 @@ fn verify_crd() {
],
"schema": {
"openAPIV3Schema": {
"description": "Auto-generated derived type for MyFoo via `CustomResource`",
"description": "Custom resource representing a Foo",
"properties": {
"spec": {
"description": "Our spec for Foo\n\nA struct with our chosen Kind will be created for us, using the following kube attrs",
Expand Down
5 changes: 4 additions & 1 deletion kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct KubeAttrs {
group: String,
version: String,
kind: String,
description: Option<String>,
#[darling(rename = "root")]
kind_struct: Option<String>,
/// lowercase plural of kind (inferred if omitted)
Expand Down Expand Up @@ -145,6 +146,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
kind,
kind_struct,
version,
description,
namespaced,
derives,
schema: schema_mode,
Expand Down Expand Up @@ -239,7 +241,8 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
derive_paths.push(syn::parse_quote! { #schemars::JsonSchema });
}

let docstr = format!(" Auto-generated derived type for {ident} via `CustomResource`");
let docstr = description
.unwrap_or_else(|| format!(" Auto-generated derived type for {ident} via `CustomResource`"));
let quoted_serde = Literal::string(&serde.to_token_stream().to_string());
let root_obj = quote! {
#[doc = #docstr]
Expand Down
5 changes: 5 additions & 0 deletions kube-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ mod custom_resource;
/// ## `#[kube(category = "apps")]`
/// Add a single category to `crd.spec.names.categories`.
///
/// ## `#[kube(description = "description")]`
/// Sets the description of the schema in the generated CRD. If not specified
/// `Auto-generated derived type for {customResourceName} via CustomResource` will be used instead.
///
/// ## Example with all properties
///
/// ```rust
Expand All @@ -155,6 +159,7 @@ mod custom_resource;
/// kind = "Foo",
/// root = "FooCrd",
/// namespaced,
/// description = "Custom resource representing a Foo",
/// status = "FooStatus",
/// derive = "PartialEq",
/// singular = "foot",
Expand Down
7 changes: 4 additions & 3 deletions kube-derive/tests/crd_schema_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![recursion_limit = "256"]

use assert_json_diff::assert_json_eq;
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -15,6 +15,7 @@ use std::collections::HashMap;
kind = "Foo",
category = "clux",
namespaced,
description = "Custom resource representing a Foo",
derive = "PartialEq",
shortname = "fo",
shortname = "f"
Expand Down Expand Up @@ -132,7 +133,7 @@ fn test_serialized_matches_expected() {
nullable: None,
nullable_skipped_with_default: None,
nullable_with_default: None,
timestamp: DateTime::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc),
timestamp: TimeZone::from_utc_datetime(&Utc, &NaiveDateTime::from_timestamp_opt(0, 0).unwrap()),
complex_enum: ComplexEnum::VariantOne { int: 23 },
untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge {
age: 42,
Expand Down Expand Up @@ -195,7 +196,7 @@ fn test_crd_schema_matches_expected() {
"additionalPrinterColumns": [],
"schema": {
"openAPIV3Schema": {
"description": "Auto-generated derived type for FooSpec via `CustomResource`",
"description": "Custom resource representing a Foo",
"properties": {
"spec": {
"properties": {
Expand Down