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

Remove invalid uniqueItems property from CRDs when Sets are used #1484

Merged
merged 4 commits into from
May 2, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions kube-core/src/schema.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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