|
| 1 | +use std::{ |
| 2 | + collections::{BTreeMap, HashMap}, |
| 3 | + str::FromStr, |
| 4 | +}; |
| 5 | + |
| 6 | +use product_config::{ProductConfigManager, types::PropertyNameKind}; |
| 7 | +use snafu::{ResultExt, Snafu}; |
| 8 | +use stackable_operator::{ |
| 9 | + commons::product_image_selection::ResolvedProductImage, |
| 10 | + product_config_utils::{transform_all_roles_to_config, validate_all_roles_and_groups_config}, |
| 11 | + role_utils::GenericRoleConfig, |
| 12 | +}; |
| 13 | + |
| 14 | +use super::dereference::DereferencedObjects; |
| 15 | +use crate::crd::{AnyServiceConfig, HbaseRole, v1alpha1}; |
| 16 | + |
| 17 | +#[derive(Snafu, Debug)] |
| 18 | +pub enum Error { |
| 19 | + #[snafu(display("invalid role properties"))] |
| 20 | + RoleProperties { source: crate::crd::Error }, |
| 21 | + |
| 22 | + #[snafu(display("failed to generate product config"))] |
| 23 | + GenerateProductConfig { |
| 24 | + source: stackable_operator::product_config_utils::Error, |
| 25 | + }, |
| 26 | + |
| 27 | + #[snafu(display("invalid product config"))] |
| 28 | + InvalidProductConfig { |
| 29 | + source: stackable_operator::product_config_utils::Error, |
| 30 | + }, |
| 31 | + |
| 32 | + #[snafu(display("could not parse Hbase role [{role}]"))] |
| 33 | + UnidentifiedHbaseRole { |
| 34 | + source: strum::ParseError, |
| 35 | + role: String, |
| 36 | + }, |
| 37 | + |
| 38 | + #[snafu(display("failed to resolve and merge config for role and role group"))] |
| 39 | + FailedToResolveConfig { source: crate::crd::Error }, |
| 40 | +} |
| 41 | + |
| 42 | +/// Per-role configuration extracted during validation. |
| 43 | +#[derive(Clone, Debug)] |
| 44 | +pub struct ValidatedRoleConfig { |
| 45 | + pub pdb: stackable_operator::commons::pdb::PdbConfig, |
| 46 | +} |
| 47 | + |
| 48 | +/// Per-rolegroup configuration: the merged CRD config plus the product-config properties. |
| 49 | +#[derive(Clone, Debug)] |
| 50 | +pub struct ValidatedRoleGroupConfig { |
| 51 | + pub merged_config: AnyServiceConfig, |
| 52 | + pub product_config_properties: HashMap<PropertyNameKind, BTreeMap<String, String>>, |
| 53 | +} |
| 54 | + |
| 55 | +/// The validated cluster: proves that product-config validation and config merging |
| 56 | +/// succeeded for every role and role group before any resources are created. |
| 57 | +#[derive(Clone, Debug)] |
| 58 | +pub struct ValidatedHbaseCluster { |
| 59 | + pub image: ResolvedProductImage, |
| 60 | + pub role_groups: BTreeMap<HbaseRole, BTreeMap<String, ValidatedRoleGroupConfig>>, |
| 61 | + pub role_configs: BTreeMap<HbaseRole, ValidatedRoleConfig>, |
| 62 | +} |
| 63 | + |
| 64 | +pub fn validate_cluster( |
| 65 | + hbase: &v1alpha1::HbaseCluster, |
| 66 | + dereferenced: &DereferencedObjects, |
| 67 | + product_config_manager: &ProductConfigManager, |
| 68 | +) -> Result<ValidatedHbaseCluster, Error> { |
| 69 | + let roles = hbase.build_role_properties().context(RolePropertiesSnafu)?; |
| 70 | + |
| 71 | + let validated_config = validate_all_roles_and_groups_config( |
| 72 | + &dereferenced.resolved_product_image.app_version_label_value, |
| 73 | + &transform_all_roles_to_config(hbase, &roles).context(GenerateProductConfigSnafu)?, |
| 74 | + product_config_manager, |
| 75 | + false, |
| 76 | + false, |
| 77 | + ) |
| 78 | + .context(InvalidProductConfigSnafu)?; |
| 79 | + |
| 80 | + let mut role_groups = BTreeMap::new(); |
| 81 | + let mut role_configs = BTreeMap::new(); |
| 82 | + |
| 83 | + for (role_name, group_config) in validated_config.iter() { |
| 84 | + let hbase_role = HbaseRole::from_str(role_name).context(UnidentifiedHbaseRoleSnafu { |
| 85 | + role: role_name.to_string(), |
| 86 | + })?; |
| 87 | + |
| 88 | + if let Some(GenericRoleConfig { |
| 89 | + pod_disruption_budget: pdb, |
| 90 | + }) = hbase.role_config(&hbase_role) |
| 91 | + { |
| 92 | + role_configs.insert(hbase_role.clone(), ValidatedRoleConfig { pdb: pdb.clone() }); |
| 93 | + } |
| 94 | + |
| 95 | + let mut group_configs = BTreeMap::new(); |
| 96 | + for (rolegroup_name, rolegroup_config) in group_config.iter() { |
| 97 | + let rolegroup = hbase.server_rolegroup_ref(role_name, rolegroup_name); |
| 98 | + |
| 99 | + let merged_config = hbase |
| 100 | + .merged_config( |
| 101 | + &hbase_role, |
| 102 | + &rolegroup.role_group, |
| 103 | + &hbase.spec.cluster_config.hdfs_config_map_name, |
| 104 | + ) |
| 105 | + .context(FailedToResolveConfigSnafu)?; |
| 106 | + |
| 107 | + group_configs.insert( |
| 108 | + rolegroup_name.clone(), |
| 109 | + ValidatedRoleGroupConfig { |
| 110 | + merged_config, |
| 111 | + product_config_properties: rolegroup_config.clone(), |
| 112 | + }, |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + role_groups.insert(hbase_role, group_configs); |
| 117 | + } |
| 118 | + |
| 119 | + Ok(ValidatedHbaseCluster { |
| 120 | + image: dereferenced.resolved_product_image.clone(), |
| 121 | + role_groups, |
| 122 | + role_configs, |
| 123 | + }) |
| 124 | +} |
0 commit comments