Skip to content

Commit 8630c12

Browse files
committed
change evaluation order of product configurations
1 parent a796eed commit 8630c12

File tree

1 file changed

+63
-50
lines changed

1 file changed

+63
-50
lines changed

crates/stackable-operator/src/product_config_utils.rs

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ fn process_validation_result(
345345
/// with the highest priority. The merge priority chain looks like this where '->' means
346346
/// "overwrites if existing or adds":
347347
///
348-
/// group overrides -> group config -> role overrides -> role config (TODO: -> common_config)
348+
/// group overrides -> role overrides -> group config -> role config (TODO: -> common_config)
349349
///
350350
/// The output is a map where the [`crate::role_utils::RoleGroup] name points to another map of
351351
/// [`product_config::types::PropertyValidationResult`] that points to the mapped configuration
@@ -368,24 +368,43 @@ where
368368
{
369369
let mut result = HashMap::new();
370370

371+
// Properties from the role have the lowest priority, so they are computed first...
371372
let role_properties = parse_role_config(resource, role_name, &role.config, property_kinds)?;
373+
let role_overrides = parse_role_overrides(&role.config, property_kinds)?;
372374

373375
// for each role group ...
374376
for (role_group_name, role_group) in &role.role_groups {
375-
// ... compute the group properties ...
377+
let mut rg_properties_merged = role_properties.clone();
378+
379+
// ... compute the group properties and merge them into role properties.
376380
let role_group_properties =
377381
parse_role_config(resource, role_name, &role_group.config, property_kinds)?;
378-
379-
// ... and merge them with the role properties.
380-
let mut role_properties_copy = role_properties.clone();
381382
for (property_kind, properties) in role_group_properties {
382-
role_properties_copy
383+
rg_properties_merged
383384
.entry(property_kind)
384385
.or_default()
385386
.extend(properties);
386387
}
387388

388-
result.insert(role_group_name.clone(), role_properties_copy);
389+
// ... copy role overrides and merge them into `rg_properties_merged`.
390+
let role_overrides_copy = role_overrides.clone();
391+
for (property_kind, property_overrides) in role_overrides_copy {
392+
rg_properties_merged
393+
.entry(property_kind)
394+
.or_default()
395+
.extend(property_overrides);
396+
}
397+
398+
// ... compute the role group overrides and merge them into `rg_properties_merged`.
399+
let role_group_overrides = parse_role_overrides(&role_group.config, property_kinds)?;
400+
for (property_kind, property_overrides) in role_group_overrides {
401+
rg_properties_merged
402+
.entry(property_kind)
403+
.or_default()
404+
.extend(property_overrides);
405+
}
406+
407+
result.insert(role_group_name.clone(), rg_properties_merged);
389408
}
390409

391410
Ok(result)
@@ -411,86 +430,80 @@ where
411430
T: Configuration,
412431
{
413432
let mut result = HashMap::new();
414-
415433
for property_kind in property_kinds {
416434
match property_kind {
417435
PropertyNameKind::File(file) => result.insert(
418436
property_kind.clone(),
419-
parse_file_properties(resource, role_name, config, file)?,
437+
config.config.compute_files(resource, role_name, file)?,
420438
),
421439
PropertyNameKind::Env => result.insert(
422440
property_kind.clone(),
423-
parse_env_properties(resource, role_name, config)?,
441+
config.config.compute_env(resource, role_name)?,
424442
),
425443
PropertyNameKind::Cli => result.insert(
426444
property_kind.clone(),
427-
parse_cli_properties(resource, role_name, config)?,
445+
config.config.compute_cli(resource, role_name)?,
428446
),
429447
};
430448
}
431449

432450
Ok(result)
433451
}
434452

435-
fn parse_cli_properties<T>(
436-
resource: &<T as Configuration>::Configurable,
437-
role_name: &str,
453+
fn parse_role_overrides<T>(
438454
config: &CommonConfiguration<T>,
439-
) -> Result<BTreeMap<String, Option<String>>>
440-
where
441-
T: Configuration,
442-
{
443-
// Properties from the role have the lowest priority, so they are computed and added first...
444-
let mut final_properties = config.config.compute_cli(resource, role_name)?;
445-
446-
// ...followed by config_overrides from the role
447-
for (key, value) in &config.cli_overrides {
448-
final_properties.insert(key.clone(), Some(value.clone()));
449-
}
450-
451-
Ok(final_properties)
452-
}
453-
454-
fn parse_env_properties<T>(
455-
resource: &<T as Configuration>::Configurable,
456-
role_name: &str,
457-
config: &CommonConfiguration<T>,
458-
) -> Result<BTreeMap<String, Option<String>>>
455+
property_kinds: &[PropertyNameKind],
456+
) -> Result<HashMap<PropertyNameKind, BTreeMap<String, Option<String>>>>
459457
where
460458
T: Configuration,
461459
{
462-
// Properties from the role have the lowest priority, so they are computed and added first...
463-
let mut final_properties = config.config.compute_env(resource, role_name)?;
464-
465-
// ...followed by config_overrides from the role
466-
for (key, value) in &config.env_overrides {
467-
final_properties.insert(key.clone(), Some(value.clone()));
460+
let mut result = HashMap::new();
461+
for property_kind in property_kinds {
462+
match property_kind {
463+
PropertyNameKind::File(file) => {
464+
result.insert(property_kind.clone(), parse_file_overrides(config, file)?)
465+
}
466+
PropertyNameKind::Env => result.insert(
467+
property_kind.clone(),
468+
config
469+
.env_overrides
470+
.clone()
471+
.into_iter()
472+
.map(|(k, v)| (k, Some(v)))
473+
.collect(),
474+
),
475+
PropertyNameKind::Cli => result.insert(
476+
property_kind.clone(),
477+
config
478+
.cli_overrides
479+
.clone()
480+
.into_iter()
481+
.map(|(k, v)| (k, Some(v)))
482+
.collect(),
483+
),
484+
};
468485
}
469486

470-
Ok(final_properties)
487+
Ok(result)
471488
}
472489

473-
fn parse_file_properties<T>(
474-
resource: &<T as Configuration>::Configurable,
475-
role_name: &str,
490+
fn parse_file_overrides<T>(
476491
config: &CommonConfiguration<T>,
477492
file: &str,
478493
) -> Result<BTreeMap<String, Option<String>>>
479494
where
480495
T: Configuration,
481496
{
482-
// Properties from the role have the lowest priority, so they are computed and added first...
483-
let mut final_properties = config.config.compute_files(resource, role_name, file)?;
497+
let mut final_overrides: BTreeMap<String, Option<String>> = BTreeMap::new();
484498

485-
// ...followed by config_overrides from the role
486499
// For Conf files only process overrides that match our file name
487500
if let Some(config) = config.config_overrides.get(file) {
488501
for (key, value) in config {
489-
final_properties.insert(key.clone(), Some(value.clone()));
502+
final_overrides.insert(key.clone(), Some(value.clone()));
490503
}
491504
}
492505

493-
Ok(final_properties)
506+
Ok(final_overrides)
494507
}
495508

496509
#[cfg(test)]
@@ -1085,7 +1098,7 @@ mod tests {
10851098
),
10861099
PropertyNameKind::Cli =>
10871100
collection!(
1088-
"cli".to_string() => Some(GROUP_CLI.to_string()),
1101+
"cli".to_string() => Some("cli".to_string()),
10891102
),
10901103
}
10911104
};

0 commit comments

Comments
 (0)