diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index d5fafa12a..94f059c65 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +- Add `utils::crds::raw_any_nullable_value_schema`, which accepts any JSON value (not only objects) ([#1326]). + +[#1326]: https://github.com/stackabletech/operator-rs/pull/1326 + ## [0.122.0] - 2026-11-03 ### Added diff --git a/crates/stackable-operator/crds/DummyCluster.yaml b/crates/stackable-operator/crds/DummyCluster.yaml index 1cf714e8c..5993b87f7 100644 --- a/crates/stackable-operator/crds/DummyCluster.yaml +++ b/crates/stackable-operator/crds/DummyCluster.yaml @@ -2685,6 +2685,68 @@ spec: status: nullable: true properties: + changedValues: + description: Contains changed values during upgrades and downgrades of CRDs. + properties: + downgrades: + additionalProperties: + items: + description: Contains a changed value for a single field of the CRD. + properties: + downgradedValue: + description: |- + The value the field was converted to during the downgrade. Only set for fields which + changed their type. + + When upgrading again, the tracked `value` is only applied if the field still contains this + value. Otherwise, the field was changed by a user in the older version and that change is + kept instead. + nullable: true + x-kubernetes-preserve-unknown-fields: true + jsonPath: + description: The name of the field of the custom resource this value is for. + type: string + value: + description: The value to be used when upgrading or downgrading the custom resource. + nullable: true + x-kubernetes-preserve-unknown-fields: true + required: + - jsonPath + - value + type: object + type: array + description: List of values needed when downgrading to a particular version. + type: object + upgrades: + additionalProperties: + items: + description: Contains a changed value for a single field of the CRD. + properties: + downgradedValue: + description: |- + The value the field was converted to during the downgrade. Only set for fields which + changed their type. + + When upgrading again, the tracked `value` is only applied if the field still contains this + value. Otherwise, the field was changed by a user in the older version and that change is + kept instead. + nullable: true + x-kubernetes-preserve-unknown-fields: true + jsonPath: + description: The name of the field of the custom resource this value is for. + type: string + value: + description: The value to be used when upgrading or downgrading the custom resource. + nullable: true + x-kubernetes-preserve-unknown-fields: true + required: + - jsonPath + - value + type: object + type: array + description: List of values needed when upgrading to a particular version. + type: object + type: object conditions: items: properties: diff --git a/crates/stackable-operator/src/utils/crds.rs b/crates/stackable-operator/src/utils/crds.rs index 18d6c08cf..3f5366a8e 100644 --- a/crates/stackable-operator/src/utils/crds.rs +++ b/crates/stackable-operator/src/utils/crds.rs @@ -15,6 +15,16 @@ pub fn raw_optional_object_schema(_: &mut schemars::generate::SchemaGenerator) - }) } +/// Accepts any JSON value (object, array, string, number, bool or null). +/// +/// Kubernetes allows omitting the `type` for nodes with `x-kubernetes-preserve-unknown-fields`. +pub fn raw_any_nullable_value_schema(_: &mut schemars::generate::SchemaGenerator) -> Schema { + json_schema!({ + "nullable": true, + "x-kubernetes-preserve-unknown-fields": true, + }) +} + pub fn raw_object_list_schema(_: &mut schemars::generate::SchemaGenerator) -> Schema { json_schema!({ "type": "array", diff --git a/crates/stackable-versioned-macros/src/codegen/container/struct/conversion.rs b/crates/stackable-versioned-macros/src/codegen/container/struct/conversion.rs index a87d8fbcd..1af236e50 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/struct/conversion.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/struct/conversion.rs @@ -573,6 +573,11 @@ impl Struct { versioned_path = path_to_string(versioned_path) ); + // The derive macros default to the `schemars` and `serde` crates, which are not necessarily + // direct dependencies of the user, so we have to tell them about crate overrides. + let schemars_path_string = path_to_string(schemars_path); + let serde_path_string = path_to_string(serde_path); + // TODO (@Techassi): Validate that users don't specify the status we generate let status = spec_gen_ctx .kubernetes_arguments @@ -595,7 +600,8 @@ impl Struct { #serde_path::Serialize, #schemars_path::JsonSchema )] - #[serde(rename_all = "camelCase")] + #[schemars(crate = #schemars_path_string)] + #[serde(crate = #serde_path_string, rename_all = "camelCase")] pub struct #status_ident { // Objects created directly in the storage version never pass through the conversion // webhook, so their status doesn't contain any changed values. The field must therefore diff --git a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking.rs.snap b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking.rs.snap index 9bfcd2b7e..f33e61af4 100644 --- a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking.rs.snap +++ b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking.rs.snap @@ -516,7 +516,8 @@ fn FooSpec_roundtrip_up_down() { ::serde::Serialize, ::schemars::JsonSchema )] -#[serde(rename_all = "camelCase")] +#[schemars(crate = "::schemars")] +#[serde(crate = "::serde", rename_all = "camelCase")] pub struct FooStatusWithChangedValues { #[serde( default, diff --git a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_enum.rs.snap b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_enum.rs.snap index 01c4517ce..b362ee37b 100644 --- a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_enum.rs.snap +++ b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_enum.rs.snap @@ -573,7 +573,8 @@ fn FooSpec_roundtrip_up_down() { ::serde::Serialize, ::schemars::JsonSchema )] -#[serde(rename_all = "camelCase")] +#[schemars(crate = "::schemars")] +#[serde(crate = "::serde", rename_all = "camelCase")] pub struct FooStatusWithChangedValues { #[serde( default, diff --git a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_generics.rs.snap b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_generics.rs.snap index b6e5dea90..c5e054c06 100644 --- a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_generics.rs.snap +++ b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_generics.rs.snap @@ -651,7 +651,8 @@ fn FooSpec_roundtrip_up_down() { ::serde::Serialize, ::schemars::JsonSchema )] -#[serde(rename_all = "camelCase")] +#[schemars(crate = "::schemars")] +#[serde(crate = "::serde", rename_all = "camelCase")] pub struct FooStatusWithChangedValues { #[serde( default, diff --git a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_removed.rs.snap b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_removed.rs.snap index c10d687f3..82c070ed4 100644 --- a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_removed.rs.snap +++ b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_removed.rs.snap @@ -614,7 +614,8 @@ fn FooSpec_roundtrip_up_down() { ::serde::Serialize, ::schemars::JsonSchema )] -#[serde(rename_all = "camelCase")] +#[schemars(crate = "::schemars")] +#[serde(crate = "::serde", rename_all = "camelCase")] pub struct FooStatusWithChangedValues { #[serde( default, diff --git a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_type_change.rs.snap b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_type_change.rs.snap index 562022e31..426dcdffa 100644 --- a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_type_change.rs.snap +++ b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@conversion_tracking_type_change.rs.snap @@ -573,7 +573,8 @@ fn FooSpec_roundtrip_up_down() { ::serde::Serialize, ::schemars::JsonSchema )] -#[serde(rename_all = "camelCase")] +#[schemars(crate = "::schemars")] +#[serde(crate = "::serde", rename_all = "camelCase")] pub struct FooStatusWithChangedValues { #[serde( default, diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 70f620f03..ee0b92aa7 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -17,9 +17,15 @@ All notable changes to this project will be documented in this file. version failed, as they never pass through the conversion webhook ([#1325]). We also reduced "clutter" in the CR's status by only serializing tracking information into the status in case something was actually tracked. +- The CRD schema of tracked values (`value` and `downgradedValue`) now accepts any JSON value instead + of only objects. Previously, patching the status failed for tracked non-object values, such as + strings ([#1326]). +- The generated tracking status now respects the `schemars` and `serde` crate overrides. Previously, + it failed to compile if these crates were not direct dependencies ([#1326]). [#1324]: https://github.com/stackabletech/operator-rs/pull/1324 [#1325]: https://github.com/stackabletech/operator-rs/pull/1325 +[#1326]: https://github.com/stackabletech/operator-rs/pull/1326 ## [0.14.0] - 2026-11-03 diff --git a/crates/stackable-versioned/src/lib.rs b/crates/stackable-versioned/src/lib.rs index 128061328..ff928c11d 100644 --- a/crates/stackable-versioned/src/lib.rs +++ b/crates/stackable-versioned/src/lib.rs @@ -123,7 +123,7 @@ pub struct ChangedValue { // NOTE: This needs to be a JSON value, because the status is stored as JSON. YAML values // represent enum variants as tags, which can't be deserialized again after a roundtrip // through JSON. - #[schemars(schema_with = "raw_object_schema")] + #[schemars(schema_with = "raw_any_nullable_value_schema")] pub value: serde_json::Value, /// The value the field was converted to during the downgrade. Only set for fields which @@ -133,16 +133,16 @@ pub struct ChangedValue { /// value. Otherwise, the field was changed by a user in the older version and that change is /// kept instead. #[serde(default, skip_serializing_if = "Option::is_none")] - #[schemars(schema_with = "raw_object_schema")] + #[schemars(schema_with = "raw_any_nullable_value_schema")] pub downgraded_value: Option, } // TODO (@Techassi): Think about where this should live. Basically this already exists in // stackable-operator, but we cannot use it without depending on it which I would like to // avoid. -fn raw_object_schema(_: &mut schemars::generate::SchemaGenerator) -> Schema { +fn raw_any_nullable_value_schema(_: &mut schemars::generate::SchemaGenerator) -> Schema { json_schema!({ - "type": "object", + "nullable": true, "x-kubernetes-preserve-unknown-fields": true, }) } diff --git a/crates/xtask/src/crd/dummy.rs b/crates/xtask/src/crd/dummy.rs index b6a702829..05a62b137 100644 --- a/crates/xtask/src/crd/dummy.rs +++ b/crates/xtask/src/crd/dummy.rs @@ -75,7 +75,8 @@ impl KeyValueOverridesProvider for DummyConfigOverrides { k8s_openapi = "stackable_operator::k8s_openapi", schemars = "stackable_operator::schemars", versioned = "stackable_operator::versioned" - ) + ), + options(k8s(experimental_conversion_tracking)) )] pub mod versioned { #[versioned(crd(