Skip to content

Commit

Permalink
Remove invalid uniqueItems property from CRDs when Sets are used (#…
Browse files Browse the repository at this point in the history
…1484)

* Fix invalid CRD generation when Sets are used

Signed-off-by: Sebastian Bernauer <sebastian.bernauer@stackable.de>

* Document users need to set x-kubernetes-list-type on their own

Signed-off-by: Sebastian Bernauer <sebastian.bernauer@stackable.de>

* Use 2018 edition for cargo fmt

Signed-off-by: Sebastian Bernauer <sebastian.bernauer@stackable.de>

---------

Signed-off-by: Sebastian Bernauer <sebastian.bernauer@stackable.de>
  • Loading branch information
sbernauer committed May 2, 2024
1 parent 6d0c9b1 commit 687506f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 9 additions & 0 deletions kube-core/src/schema.rs
Expand Up @@ -63,6 +63,15 @@ impl Visitor for StructuralSchemaRewriter {
.insert("x-kubernetes-preserve-unknown-fields".into(), true.into());
}
}

// As of version 1.30 Kubernetes does not support setting `uniqueItems` to `true`,
// so we need to remove this fields.
// Users can still set `x-kubernetes-list-type=set` in case they want the apiserver
// to do validation, but we can't make an assumption about the Set contents here.
// See https://kubernetes.io/docs/reference/using-api/server-side-apply/ for details.
if let Some(array) = &mut schema.array {
array.unique_items = None;
}
}
}

Expand Down
19 changes: 15 additions & 4 deletions kube-derive/tests/crd_schema_test.rs
Expand Up @@ -5,7 +5,7 @@ use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

// See `crd_derive_schema` example for how the schema generated from this struct affects defaulting and validation.
#[derive(CustomResource, Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
Expand Down Expand Up @@ -46,6 +46,8 @@ struct FooSpec {

/// This is a untagged enum with a description
untagged_enum_person: UntaggedEnumPerson,

set: HashSet<String>,
}

fn default_value() -> String {
Expand Down Expand Up @@ -138,7 +140,8 @@ fn test_serialized_matches_expected() {
untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge {
age: 42,
gender: Gender::Male,
})
}),
set: HashSet::from(["foo".to_owned()])
}))
.unwrap(),
serde_json::json!({
Expand All @@ -161,7 +164,8 @@ fn test_serialized_matches_expected() {
"untaggedEnumPerson": {
"age": 42,
"gender": "Male"
}
},
"set": ["foo"]
}
})
)
Expand Down Expand Up @@ -299,11 +303,18 @@ fn test_crd_schema_matches_expected() {
}
],
"description": "This is a untagged enum with a description"
}
},
"set": {
"type": "array",
"items": {
"type": "string"
},
},
},
"required": [
"complexEnum",
"nonNullable",
"set",
"timestamp",
"untaggedEnumPerson"
],
Expand Down

0 comments on commit 687506f

Please sign in to comment.