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

feat: Make roleConfig customizable #661

Merged
merged 6 commits into from
Oct 5, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Changed

- BREAKING: Make roleConfig customizable by making the `Role` struct generic over the `roleConfig` ([#661]).

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

## [0.51.1] - 2023-09-26

### Fixed
Expand All @@ -14,6 +20,8 @@ All notable changes to this project will be documented in this file.

## [0.51.0] - 2023-09-25

### Added

- Add `PdbConfig` struct and `PodDisruptionBudgetBuilder` ([#653]).

[#653]: https://github.com/stackabletech/operator-rs/pull/653
Expand Down Expand Up @@ -99,7 +107,7 @@ All notable changes to this project will be documented in this file.

### Changed

- [BREAKING] ProductImageSelection now defaults `stackableVersion` to
- BREAKING: ProductImageSelection now defaults `stackableVersion` to
operator version ([#619]).
- Default `pullPolicy` to operator `Always` ([#619]).
- BREAKING: Assume that the Vector executable is located in a directory
Expand Down Expand Up @@ -207,7 +215,7 @@ All notable changes to this project will be documented in this file.

### Added

- [BREAKING]: Added ownerreferences and labels to `build_rbac_resources` ([#579]).
- BREAKING: Added ownerreferences and labels to `build_rbac_resources` ([#579]).

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

Expand Down Expand Up @@ -302,7 +310,7 @@ All notable changes to this project will be documented in this file.
### Changed

- Deprecated `to_java_heap` and `to_java_heap_value` ([#544]).
- [BREAKING]: For all products using logback. Added additional optional parameter to `create_logback_config()` to supply custom configurations not covered via the standard log configuration ([#546]).
- BREAKING: For all products using logback. Added additional optional parameter to `create_logback_config()` to supply custom configurations not covered via the standard log configuration ([#546]).

[#544]: https://github.com/stackabletech/operator-rs/pull/544
[#546]: https://github.com/stackabletech/operator-rs/pull/546
Expand Down
73 changes: 44 additions & 29 deletions src/product_config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::error::*;
use crate::role_utils::{CommonConfiguration, Role};
use product_config::types::PropertyNameKind;
use product_config::{ProductConfigManager, PropertyValidationResult};
use schemars::JsonSchema;
use serde::Serialize;
use std::collections::{BTreeMap, HashMap};
use thiserror::Error;
use tracing::{debug, error, warn};
Expand Down Expand Up @@ -147,12 +149,13 @@ pub fn config_for_role_and_group<'a>(
/// - `resource` - Not used directly. It's passed on to the `Configuration::compute_*` calls.
/// - `roles` - A map keyed by role names. The value is a tuple of a vector of `PropertyNameKind`
/// like (Cli, Env or Files) and [`crate::role_utils::Role`] with a boxed [`Configuration`].
pub fn transform_all_roles_to_config<T>(
pub fn transform_all_roles_to_config<T, U>(
resource: &T::Configurable,
roles: HashMap<String, (Vec<PropertyNameKind>, Role<T>)>,
roles: HashMap<String, (Vec<PropertyNameKind>, Role<T, U>)>,
) -> ConfigResult<RoleConfigByPropertyKind>
where
T: Configuration,
U: Default + JsonSchema + Serialize,
{
let mut result = HashMap::new();

Expand Down Expand Up @@ -340,14 +343,15 @@ fn process_validation_result(
/// - `role_name` - The name of the role.
/// - `role` - The role for which to transform the configuration parameters.
/// - `property_kinds` - Used as "buckets" to partition the configuration properties by.
fn transform_role_to_config<T>(
fn transform_role_to_config<T, U>(
resource: &T::Configurable,
role_name: &str,
role: &Role<T>,
role: &Role<T, U>,
property_kinds: &[PropertyNameKind],
) -> ConfigResult<RoleGroupConfigByPropertyKind>
where
T: Configuration,
U: Default + JsonSchema + Serialize,
{
let mut result = HashMap::new();

Expand Down Expand Up @@ -490,7 +494,7 @@ mod tests {
}

use super::*;
use crate::role_utils::{Role, RoleConfig, RoleGroup};
use crate::role_utils::{Role, RoleGroup};
use k8s_openapi::api::core::v1::PodTemplateSpec;
use rstest::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -521,6 +525,9 @@ mod tests {
pub cli: Option<String>,
}

#[derive(Clone, Default, Debug, PartialEq, JsonSchema, Serialize)]
struct TestRoleConfig {}

impl Configuration for TestConfig {
type Configurable = String;

Expand Down Expand Up @@ -607,7 +614,7 @@ mod tests {
group_config: bool,
role_overrides: bool,
group_overrides: bool,
) -> Role<Box<TestConfig>> {
) -> Role<Box<TestConfig>, TestRoleConfig> {
let role_group = ROLE_GROUP.to_string();
let file_name = "foo.conf";

Expand All @@ -619,7 +626,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -637,7 +644,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -652,7 +659,7 @@ mod tests {
None,
None,
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -670,7 +677,7 @@ mod tests {
None,
None,
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -688,7 +695,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -706,7 +713,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: CommonConfiguration::default(),
Expand All @@ -720,7 +727,7 @@ mod tests {
None,
None,
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -739,7 +746,7 @@ mod tests {
None,
None,
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: CommonConfiguration::default(),
Expand All @@ -753,7 +760,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -771,7 +778,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -784,7 +791,7 @@ mod tests {
},
(false, true, false, true) => Role {
config: CommonConfiguration::default(),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -797,7 +804,7 @@ mod tests {
},
(false, true, false, false) => Role {
config: CommonConfiguration::default(),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -815,7 +822,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -833,7 +840,7 @@ mod tests {
build_env_override(ROLE_ENV_OVERRIDE),
build_cli_override(ROLE_CLI_OVERRIDE),
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: CommonConfiguration::default(),
Expand All @@ -842,7 +849,7 @@ mod tests {
},
(false, false, false, true) => Role {
config: CommonConfiguration::default(),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand All @@ -855,7 +862,7 @@ mod tests {
},
(false, false, false, false) => Role {
config: CommonConfiguration::default(),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group => RoleGroup {
replicas: Some(1),
config: CommonConfiguration::default(),
Expand Down Expand Up @@ -1055,7 +1062,7 @@ mod tests {
// should override
build_cli_override("cli"),
),
role_config: RoleConfig::default(),
role_config: TestRoleConfig::default(),
role_groups: collection! {role_group.to_string() => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand Down Expand Up @@ -1107,15 +1114,19 @@ mod tests {
let role_group_2 = "role_group_2";
let file_name = "foo.bar";

let roles: HashMap<String, (Vec<PropertyNameKind>, Role<Box<TestConfig>>)> = collection! {
#[allow(clippy::type_complexity)]
let roles: HashMap<
String,
(Vec<PropertyNameKind>, Role<Box<TestConfig>, TestRoleConfig>),
> = collection! {
fhennig marked this conversation as resolved.
Show resolved Hide resolved
role_1.to_string() => (vec![PropertyNameKind::File(file_name.to_string()), PropertyNameKind::Env], Role {
config: build_common_config(
build_test_config(ROLE_CONFIG, ROLE_ENV, ROLE_CLI),
None,
None,
None,
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group_1.to_string() => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand Down Expand Up @@ -1144,7 +1155,7 @@ mod tests {
None,
None,
),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {role_group_1.to_string() => RoleGroup {
replicas: Some(1),
config: build_common_config(
Expand Down Expand Up @@ -1203,10 +1214,14 @@ mod tests {
let pc_bad_version = "pc_bad_version";
let pc_bad_version_value = "pc_bad_version_value";

let roles: HashMap<String, (Vec<PropertyNameKind>, Role<Box<TestConfig>>)> = collection! {
#[allow(clippy::type_complexity)]
let roles: HashMap<
String,
(Vec<PropertyNameKind>, Role<Box<TestConfig>, TestRoleConfig>),
> = collection! {
role_1.to_string() => (vec![PropertyNameKind::File(file_name.to_string()), PropertyNameKind::Env], Role {
config: CommonConfiguration::default(),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {
role_group_1.to_string() => RoleGroup {
replicas: Some(1),
Expand All @@ -1222,7 +1237,7 @@ mod tests {
),
role_2.to_string() => (vec![PropertyNameKind::File(file_name.to_string())], Role {
config: CommonConfiguration::default(),
role_config: RoleConfig::default(),
role_config: Default::default(),
role_groups: collection! {
role_group_2.to_string() => RoleGroup {
replicas: Some(1),
Expand Down
Loading