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

[Merged by Bors] - Make pod overrides usable without roles. #616

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ All notable changes to this project will be documented in this file.

- Secrets can now be requested in a custom format ([#610]).

### Changed

- Make pod overrides usable independently of roles (like in the case of the Spark operator) ([#616])

[#610]: https://github.com/stackabletech/operator-rs/pull/610
[#616]: https://github.com/stackabletech/operator-rs/pull/616

## [0.42.2] - 2023-06-27

Expand Down
12 changes: 12 additions & 0 deletions src/config/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::merge::Merge;
#[cfg(doc)]
use crate::role_utils::{Role, RoleGroup};

use k8s_openapi::api::core::v1::PodTemplateSpec;
use snafu::Snafu;

pub use stackable_operator_derive::Fragment;
Expand Down Expand Up @@ -192,6 +193,17 @@ impl<T: FromFragment> FromFragment for Option<T> {
}
}
}
impl FromFragment for PodTemplateSpec {
type Fragment = PodTemplateSpec;
type RequiredFragment = PodTemplateSpec;

fn from_fragment(
fragment: Self::Fragment,
_validator: Validator,
) -> Result<Self, ValidationError> {
Ok(fragment)
}
}

/// Validates a [`Fragment`](`FromFragment::Fragment`), and turns it into its corresponding [`FromFragment`] type if successful.
///
Expand Down
64 changes: 63 additions & 1 deletion src/config/merge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use k8s_openapi::{
api::core::v1::{NodeAffinity, PodAffinity, PodAntiAffinity},
api::core::v1::{NodeAffinity, PodAffinity, PodAntiAffinity, PodTemplateSpec},
apimachinery::pkg::{api::resource::Quantity, apis::meta::v1::LabelSelector},
DeepMerge,
};
use std::{
collections::{btree_map, hash_map, BTreeMap, HashMap},
Expand Down Expand Up @@ -83,6 +84,11 @@ impl<K: Hash + Eq + Clone, V: Merge + Clone> Merge for HashMap<K, V> {
}
}
}
impl Merge for PodTemplateSpec {
fn merge(&mut self, defaults: &Self) {
self.merge_from(defaults.clone())
}
}

/// Moving version of [`Merge::merge`], to produce slightly nicer test output
pub fn merge<T: Merge>(mut overrides: T, defaults: &T) -> T {
Expand Down Expand Up @@ -151,6 +157,7 @@ impl<T: Atomic> Merge for Option<T> {

#[cfg(test)]
mod tests {
use k8s_openapi::api::core::v1::{PodSpec, PodTemplateSpec};
use std::collections::{BTreeMap, HashMap};

use super::{merge, Merge};
Expand Down Expand Up @@ -425,4 +432,59 @@ mod tests {
BTreeMap::from([("a", Acc(4)), ("b", Acc(2)), ("c", Acc(5))])
);
}

#[test]
fn merge_pod_template_spec() {
assert_eq!(
merge(
PodTemplateSpec {
spec: Some(PodSpec {
service_account_name: Some("my-sa".to_string()),
..PodSpec::default()
}),
..PodTemplateSpec::default()
},
&PodTemplateSpec {
spec: Some(PodSpec {
priority: Some(3000),
..PodSpec::default()
}),
..PodTemplateSpec::default()
}
),
PodTemplateSpec {
spec: Some(PodSpec {
service_account_name: Some("my-sa".to_string()),
priority: Some(3000),
..PodSpec::default()
}),
..PodTemplateSpec::default()
}
);
assert_eq!(
merge(
PodTemplateSpec {
spec: Some(PodSpec {
service_account_name: Some("sa-to-be-overridden".to_string()),
..PodSpec::default()
}),
..PodTemplateSpec::default()
},
&PodTemplateSpec {
spec: Some(PodSpec {
service_account_name: Some("sa-override".to_string()),
..PodSpec::default()
}),
..PodTemplateSpec::default()
}
),
PodTemplateSpec {
spec: Some(PodSpec {
service_account_name: Some("sa-override".to_string()),
..PodSpec::default()
}),
..PodTemplateSpec::default()
}
);
}
}
2 changes: 1 addition & 1 deletion src/role_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub struct CommonConfiguration<T> {
/// Additionally all docs are removed, as the resulting Stackable CRD objects where to big for Kubernetes.
/// E.g. the HdfsCluster CRD increased to ~3.2 MB (which is over the limit of 3MB), after stripping
/// the docs it went down to ~1.3 MiB.
fn pod_overrides_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
pub fn pod_overrides_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
let mut schema = PodTemplateSpec::json_schema(gen);
SimplifyOverrideSchema.visit_schema(&mut schema);
if let Schema::Object(schema) = &mut schema {
Expand Down