|
| 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, |
| 10 | + product_config_utils::{transform_all_roles_to_config, validate_all_roles_and_groups_config}, |
| 11 | + role_utils::GenericRoleConfig, |
| 12 | +}; |
| 13 | + |
| 14 | +use crate::{ |
| 15 | + crd::{AnyNodeConfig, HdfsNodeRole, v1alpha1}, |
| 16 | + hdfs_controller::{CONTAINER_IMAGE_BASE_NAME, ValidatedCluster}, |
| 17 | + security::opa::HdfsOpaConfig, |
| 18 | +}; |
| 19 | + |
| 20 | +#[derive(Snafu, Debug)] |
| 21 | +pub enum Error { |
| 22 | + #[snafu(display("failed to resolve product image"))] |
| 23 | + ResolveProductImage { |
| 24 | + source: product_image_selection::Error, |
| 25 | + }, |
| 26 | + |
| 27 | + #[snafu(display("invalid role properties"))] |
| 28 | + RoleProperties { source: crate::crd::Error }, |
| 29 | + |
| 30 | + #[snafu(display("failed to generate product config"))] |
| 31 | + GenerateProductConfig { |
| 32 | + source: stackable_operator::product_config_utils::Error, |
| 33 | + }, |
| 34 | + |
| 35 | + #[snafu(display("invalid product configuration"))] |
| 36 | + InvalidProductConfig { |
| 37 | + source: stackable_operator::product_config_utils::Error, |
| 38 | + }, |
| 39 | + |
| 40 | + #[snafu(display("could not parse HDFS role [{role}]"))] |
| 41 | + UnidentifiedHdfsRole { |
| 42 | + source: strum::ParseError, |
| 43 | + role: String, |
| 44 | + }, |
| 45 | + |
| 46 | + #[snafu(display("failed to resolve and merge config for role and role group"))] |
| 47 | + FailedToResolveConfig { source: crate::crd::Error }, |
| 48 | +} |
| 49 | + |
| 50 | +/// Per-role configuration extracted during validation. |
| 51 | +#[derive(Clone, Debug)] |
| 52 | +pub struct ValidatedRoleConfig { |
| 53 | + pub pdb: stackable_operator::commons::pdb::PdbConfig, |
| 54 | +} |
| 55 | + |
| 56 | +/// Per-rolegroup configuration: the merged CRD config plus the product-config properties. |
| 57 | +#[derive(Clone, Debug)] |
| 58 | +pub struct ValidatedRoleGroupConfig { |
| 59 | + pub merged_config: AnyNodeConfig, |
| 60 | + pub product_config_properties: HashMap<PropertyNameKind, BTreeMap<String, String>>, |
| 61 | +} |
| 62 | + |
| 63 | +pub fn validate_cluster( |
| 64 | + hdfs: &v1alpha1::HdfsCluster, |
| 65 | + image_repository: &str, |
| 66 | + product_config_manager: &ProductConfigManager, |
| 67 | + hdfs_opa_config: Option<HdfsOpaConfig>, |
| 68 | +) -> Result<ValidatedCluster, Error> { |
| 69 | + let resolved_product_image = hdfs |
| 70 | + .spec |
| 71 | + .image |
| 72 | + .resolve( |
| 73 | + CONTAINER_IMAGE_BASE_NAME, |
| 74 | + image_repository, |
| 75 | + crate::built_info::PKG_VERSION, |
| 76 | + ) |
| 77 | + .context(ResolveProductImageSnafu)?; |
| 78 | + |
| 79 | + let roles = hdfs |
| 80 | + .build_role_properties() |
| 81 | + .context(RolePropertiesSnafu)?; |
| 82 | + |
| 83 | + let validated_config = validate_all_roles_and_groups_config( |
| 84 | + &resolved_product_image.product_version, |
| 85 | + &transform_all_roles_to_config(hdfs, &roles).context(GenerateProductConfigSnafu)?, |
| 86 | + product_config_manager, |
| 87 | + false, |
| 88 | + false, |
| 89 | + ) |
| 90 | + .context(InvalidProductConfigSnafu)?; |
| 91 | + |
| 92 | + let mut role_groups = BTreeMap::new(); |
| 93 | + let mut role_configs = BTreeMap::new(); |
| 94 | + |
| 95 | + for (role_name, group_config) in validated_config.iter() { |
| 96 | + let hdfs_role = HdfsNodeRole::from_str(role_name).context(UnidentifiedHdfsRoleSnafu { |
| 97 | + role: role_name.to_string(), |
| 98 | + })?; |
| 99 | + |
| 100 | + if let Some(GenericRoleConfig { |
| 101 | + pod_disruption_budget: pdb, |
| 102 | + }) = hdfs.role_config(&hdfs_role) |
| 103 | + { |
| 104 | + role_configs.insert(hdfs_role, ValidatedRoleConfig { pdb: pdb.clone() }); |
| 105 | + } |
| 106 | + |
| 107 | + let mut group_configs = BTreeMap::new(); |
| 108 | + for (rolegroup_name, rolegroup_config) in group_config.iter() { |
| 109 | + let merged_config = hdfs_role |
| 110 | + .merged_config(hdfs, rolegroup_name) |
| 111 | + .context(FailedToResolveConfigSnafu)?; |
| 112 | + |
| 113 | + group_configs.insert( |
| 114 | + rolegroup_name.clone(), |
| 115 | + ValidatedRoleGroupConfig { |
| 116 | + merged_config, |
| 117 | + product_config_properties: rolegroup_config.clone(), |
| 118 | + }, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + role_groups.insert(hdfs_role, group_configs); |
| 123 | + } |
| 124 | + |
| 125 | + Ok(ValidatedCluster { |
| 126 | + image: resolved_product_image, |
| 127 | + role_groups, |
| 128 | + role_configs, |
| 129 | + hdfs_opa_config, |
| 130 | + }) |
| 131 | +} |
0 commit comments