Skip to content

Commit 687506f

Browse files
authored
Remove invalid uniqueItems property from CRDs when Sets are used (#1484)
* Fix invalid CRD generation when Sets are used Signed-off-by: Sebastian Bernauer <[email protected]> * Document users need to set x-kubernetes-list-type on their own Signed-off-by: Sebastian Bernauer <[email protected]> * Use 2018 edition for cargo fmt Signed-off-by: Sebastian Bernauer <[email protected]> --------- Signed-off-by: Sebastian Bernauer <[email protected]>
1 parent 6d0c9b1 commit 687506f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

kube-core/src/schema.rs

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ impl Visitor for StructuralSchemaRewriter {
6363
.insert("x-kubernetes-preserve-unknown-fields".into(), true.into());
6464
}
6565
}
66+
67+
// As of version 1.30 Kubernetes does not support setting `uniqueItems` to `true`,
68+
// so we need to remove this fields.
69+
// Users can still set `x-kubernetes-list-type=set` in case they want the apiserver
70+
// to do validation, but we can't make an assumption about the Set contents here.
71+
// See https://kubernetes.io/docs/reference/using-api/server-side-apply/ for details.
72+
if let Some(array) = &mut schema.array {
73+
array.unique_items = None;
74+
}
6675
}
6776
}
6877

kube-derive/tests/crd_schema_test.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
55
use kube_derive::CustomResource;
66
use schemars::JsonSchema;
77
use serde::{Deserialize, Serialize};
8-
use std::collections::HashMap;
8+
use std::collections::{HashMap, HashSet};
99

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

4747
/// This is a untagged enum with a description
4848
untagged_enum_person: UntaggedEnumPerson,
49+
50+
set: HashSet<String>,
4951
}
5052

5153
fn default_value() -> String {
@@ -138,7 +140,8 @@ fn test_serialized_matches_expected() {
138140
untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge {
139141
age: 42,
140142
gender: Gender::Male,
141-
})
143+
}),
144+
set: HashSet::from(["foo".to_owned()])
142145
}))
143146
.unwrap(),
144147
serde_json::json!({
@@ -161,7 +164,8 @@ fn test_serialized_matches_expected() {
161164
"untaggedEnumPerson": {
162165
"age": 42,
163166
"gender": "Male"
164-
}
167+
},
168+
"set": ["foo"]
165169
}
166170
})
167171
)
@@ -299,11 +303,18 @@ fn test_crd_schema_matches_expected() {
299303
}
300304
],
301305
"description": "This is a untagged enum with a description"
302-
}
306+
},
307+
"set": {
308+
"type": "array",
309+
"items": {
310+
"type": "string"
311+
},
312+
},
303313
},
304314
"required": [
305315
"complexEnum",
306316
"nonNullable",
317+
"set",
307318
"timestamp",
308319
"untaggedEnumPerson"
309320
],

0 commit comments

Comments
 (0)