Skip to content

Commit 9bbf093

Browse files
sbernauerTechassi
andauthored
feat(stackable-operator)!: Reduce CRD size by omitting podOverride and affinities schemas (#821)
* feat!: Reduce CRD size by omitting podOverride and affinities schemas * changelog * changelog * Add raw_object_list_schema * Update crates/stackable-operator/CHANGELOG.md Co-authored-by: Techassi <git@techassi.dev> * fix: Add missing full stops in changelogs * changelog * newlines --------- Co-authored-by: Techassi <git@techassi.dev> Co-authored-by: Techassi <sascha.lautenschlaeger@stackable.tech>
1 parent f15a9a3 commit 9bbf093

File tree

12 files changed

+60
-54
lines changed

12 files changed

+60
-54
lines changed

crates/k8s-version/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ All notable changes to this project will be documented in this file.
1414

1515
### Changed
1616

17-
- Bump rust-toolchain to 1.79.0 ([#822])
17+
- Bump rust-toolchain to 1.79.0 ([#822]).
1818

1919
[#822]: https://github.com/stackabletech/operator-rs/pull/822
2020

crates/stackable-certs/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
### Changed
1010

11-
- Bump rust-toolchain to 1.79.0 ([#822])
11+
- Bump rust-toolchain to 1.79.0 ([#822]).
1212

1313
[#822]: https://github.com/stackabletech/operator-rs/pull/822
1414

@@ -34,7 +34,7 @@ All notable changes to this project will be documented in this file.
3434
### Changed
3535

3636
- Implement `PartialEq` for most _Snafu_ Error enums ([#757]).
37-
- Update Rust to 1.77 ([#759])
37+
- Update Rust to 1.77 ([#759]).
3838

3939
[#757]: https://github.com/stackabletech/operator-rs/pull/757
4040
[#759]: https://github.com/stackabletech/operator-rs/pull/759

crates/stackable-operator-derive/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
### Changed
1010

11-
- Bump rust-toolchain to 1.79.0 ([#822])
11+
- Bump rust-toolchain to 1.79.0 ([#822]).
1212

1313
[#822]: https://github.com/stackabletech/operator-rs/pull/822
1414

@@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file.
2424

2525
### Changed
2626

27-
- Update Rust to 1.77 ([#759])
27+
- Update Rust to 1.77 ([#759]).
2828
- Bump GitHub workflow actions ([#772]).
2929
- Revert `zeroize` version bump ([#772]).
3030

crates/stackable-operator/CHANGELOG.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ All notable changes to this project will be documented in this file.
77
### Changed
88

99
- BREAKING: Replace `lazy_static` with `std::cell::LazyCell` ([#827]).
10-
10+
- BREAKING: Convert `podOverrides` and `affinity` fields to take any arbitrary
11+
YAML input, rather than using the underlying schema. With this change, one of
12+
the larger CRDs, like the Druid CRD went down in size from `2.4MB` to `288K`
13+
(a 88% reduction). One downside is that user input is not checked to be a
14+
valid `PodTemplateSpec`, `PodAffinity`, `PodAntiAffinity` and `NodeAffinity`
15+
any more. However, checks can be re-added by using validation webhooks if
16+
needed. This change should not be breaking for the user and is a preparation
17+
for CRD versioning. ([#821]).
18+
19+
[#821]: https://github.com/stackabletech/operator-rs/pull/821
1120
[#827]: https://github.com/stackabletech/operator-rs/pull/827
1221

1322
## [0.71.0] - 2024-07-29
@@ -37,7 +46,7 @@ All notable changes to this project will be documented in this file.
3746
- [kube#1494](https://github.com/kube-rs/kube/pull/1494)
3847
- [kube#1504](https://github.com/kube-rs/kube/pull/1504)
3948
- Upgrade opentelemetry crates ([#811]).
40-
- Bump rust-toolchain to 1.79.0 ([#822])
49+
- Bump rust-toolchain to 1.79.0 ([#822]).
4150

4251
### Fixed
4352

crates/stackable-operator/src/commons/affinity.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use stackable_operator_derive::Fragment;
1313
use crate::{
1414
config::merge::{Atomic, Merge},
1515
kvp::consts::{K8S_APP_COMPONENT_KEY, K8S_APP_INSTANCE_KEY, K8S_APP_NAME_KEY},
16+
utils::crds::raw_object_schema,
1617
};
1718

1819
pub const TOPOLOGY_KEY_HOSTNAME: &str = "kubernetes.io/hostname";
@@ -36,9 +37,19 @@ pub const TOPOLOGY_KEY_HOSTNAME: &str = "kubernetes.io/hostname";
3637
serde(rename_all = "camelCase")
3738
)]
3839
pub struct StackableAffinity {
40+
/// Same as the `spec.affinity.podAffinity` field on the Pod, see the [Kubernetes docs](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node)
41+
#[fragment_attrs(schemars(schema_with = "raw_object_schema"))]
3942
pub pod_affinity: Option<PodAffinity>,
43+
44+
/// Same as the `spec.affinity.podAntiAffinity` field on the Pod, see the [Kubernetes docs](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node)
45+
#[fragment_attrs(schemars(schema_with = "raw_object_schema"))]
4046
pub pod_anti_affinity: Option<PodAntiAffinity>,
47+
48+
/// Same as the `spec.affinity.nodeAffinity` field on the Pod, see the [Kubernetes docs](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node)
49+
#[fragment_attrs(schemars(schema_with = "raw_object_schema"))]
4150
pub node_affinity: Option<NodeAffinity>,
51+
52+
// This schema isn't big, so it can stay
4253
pub node_selector: Option<StackableNodeSelector>,
4354
}
4455

@@ -51,6 +62,7 @@ pub struct StackableAffinity {
5162
// FIXME: The generated JsonSchema will be wrong, so until https://github.com/GREsau/schemars/issues/259 is fixed, we
5263
// need to use `#[schemars(deny_unknown_fields)]`.
5364
// See https://github.com/stackabletech/operator-rs/pull/752#issuecomment-2017630433 for details.
65+
/// Simple key-value pairs forming a nodeSelector, see the [Kubernetes docs](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node)
5466
#[derive(Clone, Debug, Eq, Deserialize, JsonSchema, PartialEq, Serialize)]
5567
#[serde(rename_all = "camelCase")]
5668
#[schemars(deny_unknown_fields)]

crates/stackable-operator/src/commons/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub mod cluster_operation;
66
pub mod listener;
77
pub mod opa;
88
pub mod pdb;
9-
pub mod pod_overrides;
109
pub mod product_image_selection;
1110
pub mod rbac;
1211
pub mod resources;

crates/stackable-operator/src/role_utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ use std::{
8686
};
8787

8888
use crate::{
89-
commons::{pdb::PdbConfig, pod_overrides::pod_overrides_schema},
89+
commons::pdb::PdbConfig,
9090
config::{
9191
fragment::{self, FromFragment},
9292
merge::Merge,
9393
},
9494
product_config_utils::Configuration,
95+
utils::crds::raw_object_schema,
9596
};
9697
use derivative::Derivative;
9798
use k8s_openapi::api::core::v1::PodTemplateSpec;
@@ -111,13 +112,15 @@ pub struct CommonConfiguration<T> {
111112
// does not support specifying custom bounds.
112113
#[schemars(default = "config_schema_default")]
113114
pub config: T,
115+
114116
/// The `configOverrides` can be used to configure properties in product config files
115117
/// that are not exposed in the CRD. Read the
116118
/// [config overrides documentation](DOCS_BASE_URL_PLACEHOLDER/concepts/overrides#config-overrides)
117119
/// and consult the operator specific usage guide documentation for details on the
118120
/// available config files and settings for the specific product.
119121
#[serde(default)]
120122
pub config_overrides: HashMap<String, HashMap<String, String>>,
123+
121124
/// `envOverrides` configure environment variables to be set in the Pods.
122125
/// It is a map from strings to strings - environment variables and the value to set.
123126
/// Read the
@@ -126,18 +129,20 @@ pub struct CommonConfiguration<T> {
126129
/// the product specific environment variables that are available.
127130
#[serde(default)]
128131
pub env_overrides: HashMap<String, String>,
132+
129133
// BTreeMap to keep some order with the cli arguments.
130134
// TODO add documentation.
131135
#[serde(default)]
132136
pub cli_overrides: BTreeMap<String, String>,
137+
133138
/// In the `podOverrides` property you can define a
134139
/// [PodTemplateSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#podtemplatespec-v1-core)
135140
/// to override any property that can be set on a Kubernetes Pod.
136141
/// Read the
137142
/// [Pod overrides documentation](DOCS_BASE_URL_PLACEHOLDER/concepts/overrides#pod-overrides)
138143
/// for more information.
139144
#[serde(default)]
140-
#[schemars(schema_with = "pod_overrides_schema")]
145+
#[schemars(schema_with = "raw_object_schema")]
141146
pub pod_overrides: PodTemplateSpec,
142147
}
143148

crates/stackable-operator/src/commons/pod_overrides.rs renamed to crates/stackable-operator/src/utils/crds.rs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,36 @@
1-
use k8s_openapi::api::core::v1::PodTemplateSpec;
2-
use schemars::{schema::Schema, visit::Visitor, JsonSchema};
3-
4-
/// Simplified schema for PodTemplateSpec without mandatory fields (e.g. `containers`) or documentation.
5-
///
6-
/// The normal PodTemplateSpec requires you to specify `containers` as an `Vec<Container>`.
7-
/// Often times the user want's to overwrite/add stuff not related to a container
8-
/// (e.g. tolerations or a ServiceAccount), so it's annoying that he always needs to
9-
/// specify an empty array for `containers`.
10-
///
11-
/// Additionally all docs are removed, as the resulting Stackable CRD objects where to big for Kubernetes.
12-
/// E.g. the HdfsCluster CRD increased to ~3.2 MB (which is over the limit of 3MB), after stripping
13-
/// the docs it went down to ~1.3 MiB.
14-
pub fn pod_overrides_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
15-
let mut schema = PodTemplateSpec::json_schema(gen);
16-
SimplifyOverrideSchema.visit_schema(&mut schema);
17-
18-
if let Schema::Object(schema) = &mut schema {
19-
let meta = schema.metadata.get_or_insert_with(Default::default);
20-
meta.description = Some("See PodTemplateSpec (https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#podtemplatespec-v1-core) for more details".to_string());
21-
}
22-
23-
schema
1+
use schemars::schema::Schema;
2+
3+
pub fn raw_object_schema(_: &mut schemars::gen::SchemaGenerator) -> Schema {
4+
serde_json::from_value(serde_json::json!({
5+
"type": "object",
6+
"x-kubernetes-preserve-unknown-fields": true,
7+
}))
8+
.expect("Failed to parse JSON of custom raw object schema")
249
}
2510

26-
struct SimplifyOverrideSchema;
27-
impl schemars::visit::Visitor for SimplifyOverrideSchema {
28-
fn visit_schema_object(&mut self, schema: &mut schemars::schema::SchemaObject) {
29-
// Strip docs to make the schema more compact
30-
if let Some(meta) = &mut schema.metadata {
31-
meta.description = None;
32-
meta.examples.clear();
11+
pub fn raw_object_list_schema(_: &mut schemars::gen::SchemaGenerator) -> Schema {
12+
serde_json::from_value(serde_json::json!({
13+
"type": "array",
14+
"items": {
15+
"type": "object",
16+
"x-kubernetes-preserve-unknown-fields": true,
3317
}
34-
35-
// Make all options optional
36-
if let Some(object) = &mut schema.object {
37-
object.required.clear();
38-
}
39-
40-
schemars::visit::visit_schema_object(self, schema);
41-
}
18+
}))
19+
.expect("Failed to parse JSON of custom raw object list schema")
4220
}
4321

4422
#[cfg(test)]
4523
mod tests {
24+
use k8s_openapi::api::core::v1::PodTemplateSpec;
25+
use schemars::JsonSchema;
4626
use serde::{Deserialize, Serialize};
4727

4828
use super::*;
4929

5030
#[derive(Clone, Debug, Default, Deserialize, JsonSchema, PartialEq, Serialize)]
5131
#[serde(rename_all = "camelCase")]
5232
struct Test {
53-
#[schemars(schema_with = "pod_overrides_schema")]
33+
#[schemars(schema_with = "raw_object_schema")]
5434
pub pod_overrides: PodTemplateSpec,
5535
}
5636

@@ -130,7 +110,7 @@ mod tests {
130110
- image: docker.stackable.tech/stackable/nifi:1.23.2-stackable23.11.0
131111
"#;
132112

133-
// FIXME: Ideally we would require the names of the containers to be set. We had users using podOverrides
113+
// FIXME: Ideally we would require the names of the containers to be set. We had users using podOverrides
134114
// without setting the name of the container and wondering why it didn't work.
135115
serde_yaml::from_str::<Test>(input).expect("Failed to parse valid podOverride");
136116
}

crates/stackable-operator/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod bash;
2+
pub mod crds;
23
pub mod logging;
34
mod option;
45
mod url;

crates/stackable-telemetry/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ All notable changes to this project will be documented in this file.
1212
- Use OpenTelemetry Context in Axum instrumentation layer, adjust log and span level, simplify trace config ([#811]).
1313
- tracing: Upgrade opentelemetry crates, simplify trace config, fix shutdown conditions, use new way to shutdown LoggerProvider.
1414
- instrumentation/axum: demote event severity for errors easily caused by clients, replace parent span context if given in http header and link to previous trace contexts.
15-
- Bump rust-toolchain to 1.79.0 ([#822])
15+
- Bump rust-toolchain to 1.79.0 ([#822]).
1616

1717
[#801]: https://github.com/stackabletech/operator-rs/pull/801
1818
[#811]: https://github.com/stackabletech/operator-rs/pull/811

0 commit comments

Comments
 (0)