Skip to content

Commit a41fc34

Browse files
committed
fix: reduce RoleGroupRef usages
1 parent 227853e commit a41fc34

14 files changed

Lines changed: 192 additions & 111 deletions

File tree

rust/operator-binary/src/controller/build/container.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ use stackable_operator::{
4848
ContainerLogConfigChoice, CustomContainerLogConfig,
4949
},
5050
},
51-
role_utils::RoleGroupRef,
5251
utils::{COMMON_BASH_TRAP_FUNCTIONS, cluster_info::KubernetesClusterInfo},
5352
v2::{
5453
builder::pod::container::new_container_builder,
5554
types::{
5655
common::Port,
5756
kubernetes::{ContainerName, VolumeName},
57+
operator::RoleGroupName,
5858
},
5959
},
6060
};
@@ -90,7 +90,6 @@ use crate::{
9090
SERVICE_PORT_NAME_RPC, STACKABLE_ROOT_DATA_DIR,
9191
},
9292
storage::DataNodeStorageConfig,
93-
v1alpha1,
9493
},
9594
};
9695

@@ -212,15 +211,16 @@ impl ContainerConfig {
212211
cluster: &ValidatedCluster,
213212
cluster_info: &KubernetesClusterInfo,
214213
role: &HdfsNodeRole,
215-
rolegroup_ref: &RoleGroupRef<v1alpha1::HdfsCluster>,
214+
role_group_name: &RoleGroupName,
216215
rolegroup_config: &ValidatedRoleGroupConfig,
217216
labels: &Labels,
218217
) -> Result<(), Error> {
219218
let namenode_podrefs = build::pod_refs(cluster, &HdfsNodeRole::Name);
220219

221220
// HDFS main container
222221
let main_container_config = Self::from(*role);
223-
let object_name = rolegroup_ref.object_name();
222+
let resource_names = cluster.resource_names(role, role_group_name);
223+
let object_name = resource_names.qualified_role_group_name().to_string();
224224
let merged_config = &rolegroup_config.config;
225225

226226
pb.add_volumes(main_container_config.volumes(merged_config, &object_name, labels)?)
@@ -277,7 +277,7 @@ impl ContainerConfig {
277277
.with_pod_scope()
278278
.with_node_scope()
279279
// To scrape metrics behind TLS endpoint (without FQDN)
280-
.with_service_scope(rolegroup_ref.rolegroup_metrics_service_name())
280+
.with_service_scope(resource_names.metrics_service_name().to_string())
281281
.with_format(SecretFormat::TlsPkcs12)
282282
.with_tls_pkcs12_password(TLS_STORE_PASSWORD)
283283
.with_auto_tls_cert_lifetime(

rust/operator-binary/src/controller/build/jvm.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mod tests {
9898
use crate::{
9999
controller::build::container::ContainerConfig,
100100
crd::constants::DEFAULT_NAME_NODE_METRICS_PORT,
101-
test_support::{deserialize_and_validate_cluster, role_group_config},
101+
test_support::{deserialize_and_validate_cluster, role_group_config, role_group_name},
102102
};
103103

104104
#[test]
@@ -197,7 +197,8 @@ mod tests {
197197
let role = HdfsNodeRole::Name;
198198

199199
let validated_cluster = deserialize_and_validate_cluster(hdfs_cluster);
200-
let role_group_config = role_group_config(&validated_cluster, &role, "default");
200+
let role_group_config =
201+
role_group_config(&validated_cluster, &role, &role_group_name("default"));
201202

202203
let resources = ContainerConfig::from(role).resources(&role_group_config.config);
203204

rust/operator-binary/src/controller/build/mod.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use std::{collections::HashMap, ops::Deref};
1+
use std::collections::HashMap;
22

33
use stackable_operator::{
44
builder::meta::ObjectMetaBuilder,
55
kvp::{LabelError, Labels},
6-
role_utils::RoleGroupRef,
7-
v2::{builder::meta::ownerreference_from_resource, types::common::Port},
6+
v2::{
7+
builder::meta::ownerreference_from_resource,
8+
types::{common::Port, operator::RoleGroupName},
9+
},
810
};
911

1012
use crate::{
@@ -27,7 +29,6 @@ use crate::{
2729
SERVICE_PORT_NAME_IPC, SERVICE_PORT_NAME_JMX_METRICS, SERVICE_PORT_NAME_METRICS,
2830
SERVICE_PORT_NAME_RPC,
2931
},
30-
v1alpha1,
3132
},
3233
hdfs_controller::RESOURCE_MANAGER_HDFS_CONTROLLER,
3334
};
@@ -60,7 +61,10 @@ pub(crate) fn pod_refs(cluster: &ValidatedCluster, role: &HdfsNodeRole) -> Vec<H
6061
.into_iter()
6162
.flatten()
6263
.flat_map(|(role_group_name, role_group)| {
63-
let object_name = format!("{}-{role}-{role_group_name}", cluster.name);
64+
let object_name = cluster
65+
.resource_names(role, role_group_name)
66+
.qualified_role_group_name()
67+
.to_string();
6468
let ports = ports.clone();
6569
(0..role_group.replicas.unwrap_or(1)).map(move |i| HdfsPodRef {
6670
namespace: cluster.namespace.to_string(),
@@ -82,19 +86,25 @@ pub(crate) fn pod_refs(cluster: &ValidatedCluster, role: &HdfsNodeRole) -> Vec<H
8286
/// the owner reference nor the recommended labels can fail to build.
8387
pub(crate) fn rolegroup_metadata(
8488
cluster: &ValidatedCluster,
85-
rolegroup_ref: &RoleGroupRef<v1alpha1::HdfsCluster>,
89+
role: &HdfsNodeRole,
90+
role_group_name: &RoleGroupName,
8691
) -> ObjectMetaBuilder {
92+
let role_name = role.to_string();
8793
let mut metadata = ObjectMetaBuilder::new();
8894
metadata
8995
.name_and_namespace(cluster)
90-
.name(rolegroup_ref.object_name())
96+
.name(
97+
cluster
98+
.resource_names(role, role_group_name)
99+
.qualified_role_group_name(),
100+
)
91101
.ownerreference(ownerreference_from_resource(cluster, None, Some(true)))
92102
.with_recommended_labels(&build_recommended_labels(
93103
cluster,
94104
RESOURCE_MANAGER_HDFS_CONTROLLER,
95105
&cluster.image.app_version_label_value,
96-
&rolegroup_ref.role,
97-
&rolegroup_ref.role_group,
106+
&role_name,
107+
role_group_name.as_ref(),
98108
))
99109
.expect(
100110
"the recommended labels are valid because the ValidatedCluster uses \
@@ -104,19 +114,17 @@ pub(crate) fn rolegroup_metadata(
104114
}
105115

106116
/// The rolegroup selector labels (also used as `Service`/`StatefulSet` selectors) for
107-
/// the given `rolegroup_ref`.
117+
/// the given role group.
108118
pub(crate) fn rolegroup_selector_labels(
109119
cluster: &ValidatedCluster,
110-
rolegroup_ref: &RoleGroupRef<v1alpha1::HdfsCluster>,
120+
role: &HdfsNodeRole,
121+
role_group_name: &RoleGroupName,
111122
) -> Result<Labels, LabelError> {
112-
let mut group_labels = Labels::role_group_selector(
113-
cluster,
114-
APP_NAME,
115-
&rolegroup_ref.role,
116-
&rolegroup_ref.role_group,
117-
)?;
118-
group_labels.parse_insert(("role", rolegroup_ref.role.deref()))?;
119-
group_labels.parse_insert(("group", rolegroup_ref.role_group.deref()))?;
123+
let role_name = role.to_string();
124+
let mut group_labels =
125+
Labels::role_group_selector(cluster, APP_NAME, &role_name, role_group_name.as_ref())?;
126+
group_labels.parse_insert(("role", &role_name))?;
127+
group_labels.parse_insert(("group", role_group_name.as_ref()))?;
120128

121129
Ok(group_labels)
122130
}

rust/operator-binary/src/controller/build/properties/hdfs_site.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,15 @@ mod tests {
334334
use crate::{
335335
controller::build::properties::test_support::{cluster_info, validated_cluster},
336336
crd::HdfsNodeRole,
337-
test_support::anynode_config,
337+
test_support::{anynode_config, role_group_name},
338338
};
339339

340340
fn namenode_merged_config(validated_cluster: &ValidatedCluster) -> &AnyNodeConfig {
341-
anynode_config(validated_cluster, &HdfsNodeRole::Name, "default")
341+
anynode_config(
342+
validated_cluster,
343+
&HdfsNodeRole::Name,
344+
&role_group_name("default"),
345+
)
342346
}
343347

344348
#[test]

rust/operator-binary/src/controller/build/properties/logging.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
use std::{borrow::Cow, fmt::Display};
44

55
use stackable_operator::{
6+
kube::runtime::reflector::ObjectRef,
67
memory::{BinaryMultiple, MemoryQuantity},
78
product_logging::{
89
self,
910
spec::{ContainerLogConfig, ContainerLogConfigChoice},
1011
},
1112
role_utils::RoleGroupRef,
13+
v2::types::operator::RoleGroupName,
1214
};
1315

14-
use crate::crd::{AnyNodeConfig, DataNodeContainer, NameNodeContainer, v1alpha1};
16+
use crate::{
17+
controller::ValidatedCluster,
18+
crd::{AnyNodeConfig, DataNodeContainer, HdfsNodeRole, NameNodeContainer},
19+
};
1520

1621
pub const STACKABLE_LOG_DIR: &str = "/stackable/log";
1722
// We have a maximum of 4 continuous logging files for Namenodes. Datanodes and Journalnodes
@@ -150,7 +155,9 @@ fn add_log4j_config_if_automatic(
150155
///
151156
/// Returns `None` when the Vector agent is disabled for this role group.
152157
pub fn build_vector_config(
153-
rolegroup: &RoleGroupRef<v1alpha1::HdfsCluster>,
158+
cluster: &ValidatedCluster,
159+
role: &HdfsNodeRole,
160+
role_group_name: &RoleGroupName,
154161
merged_config: &AnyNodeConfig,
155162
) -> Option<String> {
156163
if !merged_config.vector_logging_enabled() {
@@ -167,8 +174,19 @@ pub fn build_vector_config(
167174
None
168175
};
169176

177+
// TODO: The framework's `create_vector_config` still requires a `RoleGroupRef`. We build one
178+
// over the `ValidatedCluster` (not the raw cluster) purely to satisfy this API; it only reads
179+
// the cluster name/namespace and the role/role-group strings, so the output is unchanged.
180+
// Hive ships a static `vector.yaml` instead and avoids `RoleGroupRef` entirely - we should
181+
// follow once a static config is available for HDFS, which would drop this last usage.
182+
let rolegroup = RoleGroupRef {
183+
cluster: ObjectRef::<ValidatedCluster>::from_obj(cluster),
184+
role: role.to_string(),
185+
role_group: role_group_name.to_string(),
186+
};
187+
170188
Some(product_logging::framework::create_vector_config(
171-
rolegroup,
189+
&rolegroup,
172190
vector_log_config,
173191
))
174192
}

rust/operator-binary/src/controller/build/resource/config_map.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Build the per-rolegroup `ConfigMap` for the HdfsCluster.
22
3-
use std::str::FromStr;
4-
53
use snafu::{OptionExt, ResultExt, Snafu};
64
use stackable_operator::{
7-
builder::configmap::ConfigMapBuilder, k8s_openapi::api::core::v1::ConfigMap,
8-
product_logging::framework::VECTOR_CONFIG_FILE, role_utils::RoleGroupRef,
9-
utils::cluster_info::KubernetesClusterInfo, v2::config_file_writer::PropertiesWriterError,
5+
builder::configmap::ConfigMapBuilder,
6+
k8s_openapi::api::core::v1::ConfigMap,
7+
product_logging::framework::VECTOR_CONFIG_FILE,
8+
utils::cluster_info::KubernetesClusterInfo,
9+
v2::{config_file_writer::PropertiesWriterError, types::operator::RoleGroupName},
1010
};
1111

1212
use crate::{
@@ -20,17 +20,11 @@ use crate::{
2020
},
2121
},
2222
},
23-
crd::{HdfsNodeRole, v1alpha1},
23+
crd::HdfsNodeRole,
2424
};
2525

2626
#[derive(Snafu, Debug)]
2727
pub enum Error {
28-
#[snafu(display("could not parse HDFS role [{role}]"))]
29-
UnidentifiedHdfsRole {
30-
source: strum::ParseError,
31-
role: String,
32-
},
33-
3428
#[snafu(display("the validated cluster has no role group {role_group:?} for role {role:?}"))]
3529
MissingRoleGroup { role: String, role_group: String },
3630

@@ -53,24 +47,20 @@ type Result<T, E = Error> = std::result::Result<T, E>;
5347
pub fn build_rolegroup_config_map(
5448
cluster: &ValidatedCluster,
5549
cluster_info: &KubernetesClusterInfo,
56-
rolegroup_ref: &RoleGroupRef<v1alpha1::HdfsCluster>,
50+
role: &HdfsNodeRole,
51+
role_group_name: &RoleGroupName,
5752
) -> Result<ConfigMap> {
58-
tracing::info!("Setting up ConfigMap for {:?}", rolegroup_ref);
53+
tracing::info!("Setting up ConfigMap for role {role} role group {role_group_name}");
5954

60-
let metadata = build::rolegroup_metadata(cluster, rolegroup_ref);
55+
let metadata = build::rolegroup_metadata(cluster, role, role_group_name);
6156

62-
let role = HdfsNodeRole::from_str(&rolegroup_ref.role).with_context(|_| {
63-
UnidentifiedHdfsRoleSnafu {
64-
role: rolegroup_ref.role.clone(),
65-
}
66-
})?;
6757
let rolegroup_config = cluster
6858
.role_groups
69-
.get(&role)
70-
.and_then(|role_groups| role_groups.get(&rolegroup_ref.role_group))
59+
.get(role)
60+
.and_then(|role_groups| role_groups.get(role_group_name))
7161
.with_context(|| MissingRoleGroupSnafu {
72-
role: rolegroup_ref.role.clone(),
73-
role_group: rolegroup_ref.role_group.clone(),
62+
role: role.to_string(),
63+
role_group: role_group_name.to_string(),
7464
})?;
7565
let merged_config = &rolegroup_config.config;
7666
let config_overrides = &rolegroup_config.config_overrides;
@@ -84,7 +74,7 @@ pub fn build_rolegroup_config_map(
8474
);
8575
let core_site_xml = core_site::build(
8676
cluster,
87-
role,
77+
*role,
8878
cluster_info,
8979
config_overrides.core_site_xml.clone(),
9080
);
@@ -110,20 +100,22 @@ pub fn build_rolegroup_config_map(
110100
ConfigFileName::Security.to_string(),
111101
security_properties::build(config_overrides.security_properties.clone()).with_context(
112102
|_| JvmSecurityPropertiesSnafu {
113-
rolegroup: rolegroup_ref.role_group.clone(),
103+
rolegroup: role_group_name.to_string(),
114104
},
115105
)?,
116106
);
117107

118108
for (log_config_file, log4j_config) in logging::build_log4j_configs(merged_config) {
119109
builder.add_data(log_config_file, log4j_config);
120110
}
121-
if let Some(vector_config) = logging::build_vector_config(rolegroup_ref, merged_config) {
111+
if let Some(vector_config) =
112+
logging::build_vector_config(cluster, role, role_group_name, merged_config)
113+
{
122114
builder.add_data(VECTOR_CONFIG_FILE, vector_config);
123115
}
124116

125117
builder.build().with_context(|_| AssembleSnafu {
126-
role: rolegroup_ref.role.clone(),
127-
role_group: rolegroup_ref.role_group.clone(),
118+
role: role.to_string(),
119+
role_group: role_group_name.to_string(),
128120
})
129121
}

0 commit comments

Comments
 (0)