Skip to content

CRD generation for optional enums broke with kube 2.0.0 #1821

@sbernauer

Description

@sbernauer

Current and expected behavior

Current behavior

Given a normal enum, as the one in the crd_schema_test:

/// Gender of a person
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[serde(rename_all = "PascalCase")]
enum Gender {
    Female,
    Male,
    /// This variant has a comment!
    Other,
}

and referencing it as

// This enum is optional
optional_enum: Option<Gender>,

results in the following CRD:

"optionalEnum": {
    "anyOf": [
        {
            "description": "Gender of a person",
            "enum": [
                "Female",
                "Male",
                "Other"
            ],
            "type": "string"
        },
        {
            "enum": [null],
            "nullable": true
        }
    ]
}

This is rejected by Kubernetes because of the following errors.
Please note that they are for a different CRD, but the errors should be similar.

The CustomResourceDefinition "dummyclusters.dummy.stackable.tech" is invalid: 
* spec.validation.openAPIV3Schema.properties[spec].XXX.properties[level].anyOf[0].description: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].XXX.properties[level].anyOf[0].type: Forbidden: must be empty to be structural
* spec.validation.openAPIV3Schema.properties[spec].XXX.properties[level].anyOf[1].nullable: Forbidden: must be false to be structural
* spec.validation.openAPIV3Schema.properties[spec].XXX.properties[level].type: Required value: must not be empty for specified object fields

Expected behavior

Pre 2.0.0, kube (correctly) produced

"optionalEnum": {
    "description": "This enum is optional",
    "enum": [
        "Female",
        "Male",
        "Other"
    ],
    "type": "string",
    "nullable": true
}

Possible solution

No response

Additional context

No response

Environment

Client Version: v1.33.3
Kustomize Version: v5.6.0
Server Version: v1.33.1

This is the entire patch to reproduce the bug in the kube tests:

diff --git a/kube-derive/tests/crd_schema_test.rs b/kube-derive/tests/crd_schema_test.rs
index c00009ff..11e074d6 100644
--- a/kube-derive/tests/crd_schema_test.rs
+++ b/kube-derive/tests/crd_schema_test.rs
@@ -77,6 +77,9 @@ struct FooSpec {
 
     #[x_kube(merge_strategy = ListMerge::Set)]
     x_kubernetes_set: Vec<String>,
+
+    // This enum is optional
+    optional_enum: Option<Gender>,
 }
 
 #[derive(Clone, Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
@@ -146,6 +149,7 @@ struct GenderAndDateOfBirth {
     date_of_birth: String,
 }
 
+/// Gender of a person
 #[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
 #[serde(rename_all = "PascalCase")]
 enum Gender {
@@ -187,6 +191,7 @@ fn test_serialized_matches_expected() {
             my_list: vec!["".into()],
             set: HashSet::from(["foo".to_owned()]),
             x_kubernetes_set: vec![],
+            optional_enum: Some(Gender::Other),
         }))
         .unwrap(),
         serde_json::json!({
@@ -222,6 +227,7 @@ fn test_serialized_matches_expected() {
                 "myList": [""],
                 "set": ["foo"],
                 "xKubernetesSet": [],
+                "optionalEnum": "Other",
             }
         })
     )
@@ -410,6 +416,23 @@ fn test_crd_schema_matches_expected() {
                                                 },
                                                 "x-kubernetes-list-type": "set",
                                             },
+                                            "optionalEnum": {
+                                                "anyOf": [
+                                                    {
+                                                        "description": "Gender of a person",
+                                                        "enum": [
+                                                            "Female",
+                                                            "Male",
+                                                            "Other"
+                                                        ],
+                                                        "type": "string"
+                                                    },
+                                                    {
+                                                        "enum": [null],
+                                                        "nullable": true
+                                                    }
+                                                ]
+                                            }
                                         },
                                         "required": [
                                             "complexEnum",

Configuration and features

Using kubes main branch to run tests

YAML

No response

Affected crates

kube-derive

Would you like to work on fixing this bug?

maybe

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions