Skip to content
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
295c431
Add some tests for CRD generation for enums
sbernauer Oct 8, 2025
13db30c
Update assertions to valid CRDs from kube 1.1.0
sbernauer Oct 9, 2025
fb77413
test: Add some un-documented variants to the NormalEnum
NickLarsenNZ Oct 9, 2025
0cb62f1
test: Partial new impl for one_of hoisting
NickLarsenNZ Oct 9, 2025
85eaf36
Finish enum oneOf hoisting
sbernauer Oct 9, 2025
285ab64
adjust and document the hoist_one_of_enum function
NickLarsenNZ Oct 10, 2025
e256f3a
test: Add more basic testing to ensure hoisting is not performed when…
NickLarsenNZ Oct 10, 2025
35034c5
hoist anyOf optional enums
NickLarsenNZ Oct 10, 2025
3850a19
left a todo for empty structural variants in untagged enums
NickLarsenNZ Oct 10, 2025
c2fbe70
test: Add missing enum entries to expected schemas
NickLarsenNZ Oct 10, 2025
8abef36
test: Add empty object handling to existing hoisting function
NickLarsenNZ Oct 10, 2025
8f578c2
fix: Change hoist_any_of_option_enum to cater to nested any_of as wel…
NickLarsenNZ Oct 14, 2025
c23aabc
test: Add test for internal function, and add docs after seeing it's …
NickLarsenNZ Oct 14, 2025
e91665b
feat: Add functions for specific hoisting tasks
NickLarsenNZ Oct 21, 2025
f13a751
chore: Add a crude fix to code that will be removed in a later commit
NickLarsenNZ Oct 21, 2025
730e0db
fix: update changed function name in tests
NickLarsenNZ Oct 21, 2025
7503fcc
chore: Make new hoisting functions pub(crate)
NickLarsenNZ Oct 21, 2025
17e7a28
chore: Move schema.rs to schema/mod.rs
NickLarsenNZ Oct 21, 2025
cc6b20a
feat: Implement new hoisting logic
NickLarsenNZ Oct 21, 2025
8e40d01
fix: Handle property hoisting for tagged enums (oneOf subschemas)
NickLarsenNZ Oct 22, 2025
e311361
lint: commit whitespace differences
NickLarsenNZ Oct 22, 2025
307e828
chore: use assert/assert_eq with message instead of panic
NickLarsenNZ Oct 22, 2025
25e10db
Add integration test for (optional) complex enums
sbernauer Oct 23, 2025
764c418
Add test case for Option<Option<Option<String>>>
sbernauer Oct 23, 2025
d52c2c6
Remove leftover newline
sbernauer Oct 23, 2025
6d156a0
Add (failing) test for enums without descriptions
sbernauer Oct 23, 2025
e3172d5
Improve test comment
sbernauer Oct 23, 2025
ef6da4f
fix: Remove trailing nulls for optional enums
sbernauer Oct 27, 2025
d0fe594
chore: fix comments, use iter_mut
NickLarsenNZ Oct 27, 2025
39df989
chore: fix comments/descriptions in tests
NickLarsenNZ Oct 27, 2025
2e0a7aa
chore: add expected results to complex enum tests
NickLarsenNZ Oct 27, 2025
4668b97
chore: Fix tests after merge
NickLarsenNZ Oct 27, 2025
021efc8
chore: Fix import after merge
NickLarsenNZ Oct 27, 2025
92fb0e4
fix: adjust metadata hoisting
NickLarsenNZ Oct 27, 2025
438bc7f
test: add few more hoisting tests
NickLarsenNZ Oct 27, 2025
a5d68d8
chore: Remove unused code
NickLarsenNZ Oct 27, 2025
920d6aa
test: Add some tests on untagged enums duplicated fields
sbernauer Oct 27, 2025
c856801
Apply suggestions from code review
NickLarsenNZ Oct 27, 2025
b5cf5a4
Merge remote-tracking branch 'origin/main' into test/add-enum-tests
NickLarsenNZ Oct 27, 2025
cde8cc8
Re-group imports
sbernauer Oct 28, 2025
9c19f10
Move tests into own "tests" module
sbernauer Oct 28, 2025
1ce7e47
Move null schema into NULL_SCHEMA static
sbernauer Oct 28, 2025
9eaedbb
refactor: use more descriptive variable names
NickLarsenNZ Oct 28, 2025
0477fd0
Apply suggestions from code review
NickLarsenNZ Oct 28, 2025
a237e1e
refactor: Replace panic with pattern match
NickLarsenNZ Oct 28, 2025
dcbe17e
refactor: Simplify description preservation and type dropping
NickLarsenNZ Oct 28, 2025
acf3d57
refactor: Simplify the getting or defaulting of the parent object
NickLarsenNZ Oct 28, 2025
925a8e3
refactor: Reorder test enums
NickLarsenNZ Oct 28, 2025
11a8ddc
docs: Revise doc comments
NickLarsenNZ Oct 28, 2025
26146ea
Revert "refactor: Simplify description preservation and type dropping"
NickLarsenNZ Oct 28, 2025
75c7ffa
refactor: Revise code after reverting
NickLarsenNZ Oct 28, 2025
2055029
test: Rework tests that check that the CRDs are valid
sbernauer Oct 28, 2025
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
172 changes: 33 additions & 139 deletions kube-core/src/schema.rs → kube-core/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@
//!
//! [`CustomResourceDefinition`]: `k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition`
mod transform_any_of;
mod transform_one_of;
mod transform_optional_enum_with_null;
mod transform_properties;

// Used in docs
#[allow(unused_imports)] use schemars::generate::SchemaSettings;

use schemars::{transform::Transform, JsonSchema};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{btree_map::Entry, BTreeMap, BTreeSet};
use std::{
collections::{BTreeMap, BTreeSet},
sync::LazyLock,
};

use crate::schema::{
transform_any_of::hoist_any_of_subschema_with_a_nullable_variant,
transform_one_of::hoist_one_of_enum_with_unit_variants,
transform_optional_enum_with_null::remove_optional_enum_null_variant,
transform_properties::hoist_properties_for_any_of_subschemas,
};

/// This is the signature for the `null` variant produced by schemars.
static NULL_SCHEMA: LazyLock<Value> = LazyLock::new(|| {
serde_json::json!({
"enum": [null],
"nullable": true
})
});

/// schemars [`Visitor`] that rewrites a [`Schema`] to conform to Kubernetes' "structural schema" rules
///
Expand Down Expand Up @@ -248,32 +271,22 @@ enum SingleOrVec<T> {

impl Transform for StructuralSchemaRewriter {
fn transform(&mut self, transform_schema: &mut schemars::Schema) {
// Apply this (self) transform to all subschemas
schemars::transform::transform_subschemas(self, transform_schema);

let mut schema: SchemaObject = match serde_json::from_value(transform_schema.clone().to_value()).ok()
{
// TODO (@NickLarsenNZ): At this point, we are seeing duplicate `title` when printing schema as json.
// This is because `title` is specified under both `extensions` and `other`.
Some(schema) => schema,
None => return,
};

if let Some(subschemas) = &mut schema.subschemas {
if let Some(one_of) = subschemas.one_of.as_mut() {
// Tagged enums are serialized using `one_of`
hoist_subschema_properties(one_of, &mut schema.object, &mut schema.instance_type);

// "Plain" enums are serialized using `one_of` if they have doc tags
hoist_subschema_enum_values(one_of, &mut schema.enum_values, &mut schema.instance_type);

if one_of.is_empty() {
subschemas.one_of = None;
}
}

if let Some(any_of) = &mut subschemas.any_of {
// Untagged enums are serialized using `any_of`
hoist_subschema_properties(any_of, &mut schema.object, &mut schema.instance_type);
}
}
// Hoist parts of the schema to make it valid for k8s
hoist_one_of_enum_with_unit_variants(&mut schema);
hoist_any_of_subschema_with_a_nullable_variant(&mut schema);
hoist_properties_for_any_of_subschemas(&mut schema);
remove_optional_enum_null_variant(&mut schema);

// check for maps without with properties (i.e. flattened maps)
// and allow these to persist dynamically
Expand All @@ -297,130 +310,11 @@ impl Transform for StructuralSchemaRewriter {
array.unique_items = None;
}

// Convert back to schemars::Schema
if let Ok(schema) = serde_json::to_value(schema) {
if let Ok(transformed) = serde_json::from_value(schema) {
*transform_schema = transformed;
}
}
}
}

/// Bring all plain enum values up to the root schema,
/// since Kubernetes doesn't allow subschemas to define enum options.
///
/// (Enum here means a list of hard-coded values, not a tagged union.)
fn hoist_subschema_enum_values(
subschemas: &mut Vec<Schema>,
common_enum_values: &mut Option<Vec<serde_json::Value>>,
instance_type: &mut Option<SingleOrVec<InstanceType>>,
) {
subschemas.retain(|variant| {
if let Schema::Object(SchemaObject {
instance_type: variant_type,
enum_values: Some(variant_enum_values),
..
}) = variant
{
if let Some(variant_type) = variant_type {
match instance_type {
None => *instance_type = Some(variant_type.clone()),
Some(tpe) => {
if tpe != variant_type {
panic!("Enum variant set {variant_enum_values:?} has type {variant_type:?} but was already defined as {instance_type:?}. The instance type must be equal for all subschema variants.")
}
}
}
}
common_enum_values
.get_or_insert_with(Vec::new)
.extend(variant_enum_values.iter().cloned());
false
} else {
true
}
})
}

/// Bring all property definitions from subschemas up to the root schema,
/// since Kubernetes doesn't allow subschemas to define properties.
fn hoist_subschema_properties(
subschemas: &mut Vec<Schema>,
common_obj: &mut Option<Box<ObjectValidation>>,
instance_type: &mut Option<SingleOrVec<InstanceType>>,
) {
for variant in subschemas {
if let Schema::Object(SchemaObject {
instance_type: variant_type,
object: Some(variant_obj),
metadata: variant_metadata,
..
}) = variant
{
let common_obj = common_obj.get_or_insert_with(Box::<ObjectValidation>::default);

if let Some(variant_metadata) = variant_metadata {
// Move enum variant description from oneOf clause to its corresponding property
if let Some(description) = std::mem::take(&mut variant_metadata.description) {
if let Some(Schema::Object(variant_object)) =
only_item(variant_obj.properties.values_mut())
{
let metadata = variant_object
.metadata
.get_or_insert_with(Box::<Metadata>::default);
metadata.description = Some(description);
}
}
}

// Move all properties
let variant_properties = std::mem::take(&mut variant_obj.properties);
for (property_name, property) in variant_properties {
match common_obj.properties.entry(property_name) {
Entry::Vacant(entry) => {
entry.insert(property);
}
Entry::Occupied(entry) => {
if &property != entry.get() {
panic!("Property {:?} has the schema {:?} but was already defined as {:?} in another subschema. The schemas for a property used in multiple subschemas must be identical",
entry.key(),
&property,
entry.get());
}
}
}
}

// Kubernetes doesn't allow variants to set additionalProperties
variant_obj.additional_properties = None;

merge_metadata(instance_type, variant_type.take());
}
}
}

fn only_item<I: Iterator>(mut i: I) -> Option<I::Item> {
let item = i.next()?;
if i.next().is_some() {
return None;
}
Some(item)
}

fn merge_metadata(
instance_type: &mut Option<SingleOrVec<InstanceType>>,
variant_type: Option<SingleOrVec<InstanceType>>,
) {
match (instance_type, variant_type) {
(_, None) => {}
(common_type @ None, variant_type) => {
*common_type = variant_type;
}
(Some(common_type), Some(variant_type)) => {
if *common_type != variant_type {
panic!(
"variant defined type {variant_type:?}, conflicting with existing type {common_type:?}"
);
}
}
}
}
Loading
Loading