Skip to content

Commit 0ffac00

Browse files
adwk67claude
andcommitted
refactor: extract dereference and validate steps into controller modules
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5207e39 commit 0ffac00

7 files changed

Lines changed: 234 additions & 90 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use snafu::{ResultExt, Snafu};
2+
3+
use crate::{crd::v1alpha1, security::opa::HdfsOpaConfig};
4+
5+
#[derive(Snafu, Debug)]
6+
pub enum Error {
7+
#[snafu(display("invalid OPA configuration"))]
8+
InvalidOpaConfig { source: crate::security::opa::Error },
9+
}
10+
11+
/// External references resolved during the dereference step.
12+
pub struct DereferencedObjects {
13+
pub hdfs_opa_config: Option<HdfsOpaConfig>,
14+
}
15+
16+
pub async fn dereference(
17+
client: &stackable_operator::client::Client,
18+
hdfs: &v1alpha1::HdfsCluster,
19+
) -> Result<DereferencedObjects, Error> {
20+
let hdfs_opa_config = match &hdfs.spec.cluster_config.authorization {
21+
Some(opa_config) => Some(
22+
HdfsOpaConfig::from_opa_config(client, hdfs, opa_config)
23+
.await
24+
.context(InvalidOpaConfigSnafu)?,
25+
),
26+
None => None,
27+
};
28+
29+
Ok(DereferencedObjects { hdfs_opa_config })
30+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod dereference;
2+
pub mod validate;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

rust/operator-binary/src/crd/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ pub struct CommonNodeConfig {
993993
}
994994

995995
/// Configuration for a rolegroup of an unknown type.
996-
#[derive(Debug)]
996+
#[derive(Clone, Debug)]
997997
pub enum AnyNodeConfig {
998998
Name(NameNodeConfig),
999999
Data(DataNodeConfig),
@@ -1087,7 +1087,9 @@ impl AnyNodeConfig {
10871087
Eq,
10881088
Hash,
10891089
JsonSchema,
1090+
Ord,
10901091
PartialEq,
1092+
PartialOrd,
10911093
Serialize,
10921094
)]
10931095
pub enum HdfsNodeRole {

0 commit comments

Comments
 (0)