Skip to content

Commit c94054b

Browse files
committed
refactor: remove remaining raw cluster references in resource builders
1 parent 9b21372 commit c94054b

7 files changed

Lines changed: 84 additions & 41 deletions

File tree

rust/operator-binary/src/controller.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use crate::{
5050
service::{build_rolegroup_headless_service, build_rolegroup_metrics_service},
5151
},
5252
},
53-
crd::{APP_NAME, HiveClusterStatus, HiveRole, MetaStoreConfig, v1alpha1},
53+
crd::{APP_NAME, HdfsConnection, HiveClusterStatus, HiveRole, MetaStoreConfig, v1alpha1},
5454
};
5555

5656
pub const HIVE_CONTROLLER_NAME: &str = "hivecluster";
@@ -284,6 +284,14 @@ impl ValidatedCluster {
284284
role_group_selector(self, &product_name(), &Self::role_name(), role_group_name)
285285
}
286286

287+
/// Whether Kerberos is enabled for this cluster (a Kerberos `SecretClass` was configured).
288+
///
289+
/// Mirrors [`v1alpha1::HiveCluster::has_kerberos_enabled`], derived here from the validated
290+
/// config so build steps don't have to re-read the raw cluster.
291+
pub fn has_kerberos_enabled(&self) -> bool {
292+
self.cluster_config.kerberos_secret_class.is_some()
293+
}
294+
287295
/// The name of the per-role [`Listener`] object.
288296
///
289297
/// Must stay in sync with [`v1alpha1::HiveCluster::role_listener_name`], which derives the
@@ -392,8 +400,14 @@ pub struct ValidatedClusterConfig {
392400
pub metadata_database_connection_details: JdbcDatabaseConnectionDetails,
393401
/// The resolved JDBC driver class (Derby version special-casing already applied).
394402
pub connection_driver: String,
403+
/// The database type passed to Hive via the `--db-type` CLI argument (e.g. `derby`).
404+
pub db_type: String,
405+
/// The HDFS connection (discovery ConfigMap reference), if an HDFS backend is configured.
406+
pub hdfs: Option<HdfsConnection>,
395407
pub s3_connection_spec: Option<s3::v1alpha1::ConnectionSpec>,
396408
pub hive_opa_config: Option<HiveOpaConfig>,
409+
/// The Kerberos `SecretClass` name, if Kerberos is enabled.
410+
pub kerberos_secret_class: Option<String>,
397411
/// Kerberos-related `hive-site.xml` entries (empty when Kerberos is disabled).
398412
pub kerberos_config: BTreeMap<String, String>,
399413
/// Whether a `core-site.xml` with `hadoop.security.authentication=kerberos` is
@@ -492,7 +506,6 @@ pub async fn reconcile_hive(
492506

493507
let rg_statefulset =
494508
build::resource::statefulset::build_metastore_rolegroup_statefulset(
495-
hive,
496509
hive_role,
497510
&validated_cluster,
498511
role_group_name,

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ use stackable_operator::crd::s3;
33
use super::{
44
opa::HiveOpaConfig, properties::ConfigFileName, resource::statefulset::HDFS_CONFIG_MOUNT_DIR,
55
};
6-
use crate::crd::{
7-
STACKABLE_CONFIG_DIR, STACKABLE_CONFIG_MOUNT_DIR, STACKABLE_LOG_CONFIG_MOUNT_DIR,
8-
STACKABLE_TRUST_STORE, STACKABLE_TRUST_STORE_PASSWORD, v1alpha1,
6+
use crate::{
7+
controller::ValidatedCluster,
8+
crd::{
9+
STACKABLE_CONFIG_DIR, STACKABLE_CONFIG_MOUNT_DIR, STACKABLE_LOG_CONFIG_MOUNT_DIR,
10+
STACKABLE_TRUST_STORE, STACKABLE_TRUST_STORE_PASSWORD,
11+
},
912
};
1013

1114
pub fn build_container_command_args(
12-
hive: &v1alpha1::HiveCluster,
15+
cluster: &ValidatedCluster,
1316
start_command: String,
1417
s3_connection_spec: Option<&s3::v1alpha1::ConnectionSpec>,
1518
hive_opa_config: Option<&HiveOpaConfig>,
@@ -40,7 +43,7 @@ pub fn build_container_command_args(
4043
),
4144
];
4245

43-
if hive.spec.cluster_config.hdfs.is_some() {
46+
if cluster.cluster_config.hdfs.is_some() {
4447
args.extend([
4548
format!("echo copying {HDFS_CONFIG_MOUNT_DIR} to {STACKABLE_CONFIG_DIR}"),
4649
format!("cp -RL {HDFS_CONFIG_MOUNT_DIR}/* {STACKABLE_CONFIG_DIR}"),

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use stackable_operator::memory::{BinaryMultiple, MemoryQuantity};
33

44
use super::{kerberos::STACKABLE_KERBEROS_DIR, properties::ConfigFileName};
55
use crate::{
6-
controller::HiveRoleGroupConfig,
6+
controller::{HiveRoleGroupConfig, ValidatedCluster},
77
crd::{
88
METRICS_PORT, MetaStoreConfig, STACKABLE_CONFIG_DIR, STACKABLE_TRUST_STORE,
9-
STACKABLE_TRUST_STORE_PASSWORD, v1alpha1::HiveCluster,
9+
STACKABLE_TRUST_STORE_PASSWORD,
1010
},
1111
};
1212

@@ -24,7 +24,7 @@ pub enum Error {
2424
}
2525

2626
/// All JVM arguments.
27-
fn construct_jvm_args(hive: &HiveCluster, rg: &HiveRoleGroupConfig) -> Vec<String> {
27+
fn construct_jvm_args(cluster: &ValidatedCluster, rg: &HiveRoleGroupConfig) -> Vec<String> {
2828
let security_properties = ConfigFileName::Security;
2929
let mut jvm_args = vec![
3030
format!("-Djava.security.properties={STACKABLE_CONFIG_DIR}/{security_properties}"),
@@ -36,7 +36,7 @@ fn construct_jvm_args(hive: &HiveCluster, rg: &HiveRoleGroupConfig) -> Vec<Strin
3636
format!("-Djavax.net.ssl.trustStoreType=pkcs12"),
3737
];
3838

39-
if hive.has_kerberos_enabled() {
39+
if cluster.has_kerberos_enabled() {
4040
jvm_args.push(format!(
4141
"-Djava.security.krb5.conf={STACKABLE_KERBEROS_DIR}/krb5.conf"
4242
));
@@ -49,8 +49,8 @@ fn construct_jvm_args(hive: &HiveCluster, rg: &HiveRoleGroupConfig) -> Vec<Strin
4949

5050
/// Arguments that go into `HADOOP_OPTS`, so *not* the heap settings (which you can get using
5151
/// [`construct_hadoop_heapsize_env`]).
52-
pub fn construct_non_heap_jvm_args(hive: &HiveCluster, rg: &HiveRoleGroupConfig) -> String {
53-
let mut jvm_args = construct_jvm_args(hive, rg);
52+
pub fn construct_non_heap_jvm_args(cluster: &ValidatedCluster, rg: &HiveRoleGroupConfig) -> String {
53+
let mut jvm_args = construct_jvm_args(cluster, rg);
5454
jvm_args.retain(|arg| !is_heap_jvm_argument(arg));
5555

5656
jvm_args.join(" ")
@@ -88,14 +88,17 @@ mod tests {
8888
crd::HiveRole,
8989
};
9090

91-
fn metastore_default(hive: &crate::crd::v1alpha1::HiveCluster) -> HiveRoleGroupConfig {
91+
fn metastore_default(
92+
hive: &crate::crd::v1alpha1::HiveCluster,
93+
) -> (ValidatedCluster, HiveRoleGroupConfig) {
9294
let validated = validated_cluster(hive);
93-
validated
95+
let rg = validated
9496
.role_group_configs
9597
.get(&HiveRole::MetaStore)
9698
.and_then(|groups| groups.get(&"default".parse().expect("valid role group name")))
9799
.expect("metastore default role group should exist")
98-
.clone()
100+
.clone();
101+
(validated, rg)
99102
}
100103

101104
#[test]
@@ -119,8 +122,8 @@ mod tests {
119122
replicas: 1
120123
"#;
121124
let hive = minimal_hive(input);
122-
let rg = metastore_default(&hive);
123-
let non_heap_jvm_args = construct_non_heap_jvm_args(&hive, &rg);
125+
let (cluster, rg) = metastore_default(&hive);
126+
let non_heap_jvm_args = construct_non_heap_jvm_args(&cluster, &rg);
124127
let hadoop_heapsize_env = construct_hadoop_heapsize_env(&rg.config).unwrap();
125128

126129
assert_eq!(
@@ -174,8 +177,8 @@ mod tests {
174177
- -Dhttps.proxyPort=1234
175178
"#;
176179
let hive = minimal_hive(input);
177-
let rg = metastore_default(&hive);
178-
let non_heap_jvm_args = construct_non_heap_jvm_args(&hive, &rg);
180+
let (cluster, rg) = metastore_default(&hive);
181+
let non_heap_jvm_args = construct_non_heap_jvm_args(&cluster, &rg);
179182
let hadoop_heapsize_env = construct_hadoop_heapsize_env(&rg.config).unwrap();
180183

181184
assert_eq!(

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ use stackable_operator::{
1515
},
1616
},
1717
commons::secret_class::SecretClassVolumeProvisionParts,
18-
kube::ResourceExt,
1918
utils::cluster_info::KubernetesClusterInfo,
2019
v2::types::kubernetes::VolumeName,
2120
};
2221

2322
use super::properties::ConfigFileName;
24-
use crate::crd::{HiveRole, STACKABLE_CONFIG_DIR, v1alpha1};
23+
use crate::{
24+
controller::ValidatedCluster,
25+
crd::{HiveRole, STACKABLE_CONFIG_DIR},
26+
};
2527

2628
// Typed name for the Kerberos secret-operator volume, reusing the existing `"kerberos"` string
2729
// value so the produced volume/mount name is unchanged.
@@ -49,19 +51,19 @@ pub enum Error {
4951
}
5052

5153
pub fn add_kerberos_pod_config(
52-
hive: &v1alpha1::HiveCluster,
54+
cluster: &ValidatedCluster,
5355
role: &HiveRole,
5456
cb: &mut ContainerBuilder,
5557
pb: &mut PodBuilder,
5658
) -> Result<(), Error> {
57-
if let Some(kerberos_secret_class) = hive.kerberos_secret_class() {
59+
if let Some(kerberos_secret_class) = &cluster.cluster_config.kerberos_secret_class {
5860
// Mount keytab
5961
let kerberos_secret_operator_volume = SecretOperatorVolumeSourceBuilder::new(
60-
kerberos_secret_class,
62+
kerberos_secret_class.clone(),
6163
// We need both public (krb5.conf) and private (keytab) parts.
6264
SecretClassVolumeProvisionParts::PublicPrivate,
6365
)
64-
.with_service_scope(hive.name_any())
66+
.with_service_scope(cluster.name.to_string())
6567
.with_kerberos_service_name(role.kerberos_service_name())
6668
.build()
6769
.context(AddKerberosSecretVolumeSnafu)?;
@@ -115,8 +117,8 @@ pub fn kerberos_config_properties(
115117
])
116118
}
117119

118-
pub fn kerberos_container_start_commands(hive: &v1alpha1::HiveCluster) -> String {
119-
if !hive.has_kerberos_enabled() {
120+
pub fn kerberos_container_start_commands(cluster: &ValidatedCluster) -> String {
121+
if !cluster.has_kerberos_enabled() {
120122
return String::new();
121123
}
122124

@@ -126,7 +128,7 @@ pub fn kerberos_container_start_commands(hive: &v1alpha1::HiveCluster) -> String
126128
sed -i -e 's/${{env.KERBEROS_REALM}}/'\"$KERBEROS_REALM/g\" {STACKABLE_CONFIG_DIR}/{hive_site_xml}",
127129
}];
128130

129-
if hive.spec.cluster_config.hdfs.is_some() {
131+
if cluster.cluster_config.hdfs.is_some() {
130132
let core_site_xml = ConfigFileName::CoreSite;
131133
let hdfs_site_xml = ConfigFileName::HdfsSite;
132134
args.extend([

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,20 @@ pub(crate) mod test_support {
6464
MetadataDatabaseConnection::Derby(_) => derby_driver_class("4.0.0").to_owned(),
6565
_ => metadata_database_connection_details.driver.clone(),
6666
};
67+
let db_type = hive
68+
.spec
69+
.cluster_config
70+
.metadata_database
71+
.as_hive_db_type()
72+
.to_owned();
6773
ValidatedClusterConfig {
6874
metadata_database_connection_details,
6975
connection_driver,
76+
db_type,
77+
hdfs: None,
7078
s3_connection_spec: None,
7179
hive_opa_config: None,
80+
kerberos_secret_class: None,
7281
kerberos_config: BTreeMap::new(),
7382
needs_kerberos_core_site: false,
7483
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::{
6161
HIVE_PORT, HIVE_PORT_NAME, HiveRole, METRICS_PORT, METRICS_PORT_NAME, STACKABLE_CONFIG_DIR,
6262
STACKABLE_CONFIG_DIR_NAME, STACKABLE_CONFIG_MOUNT_DIR, STACKABLE_CONFIG_MOUNT_DIR_NAME,
6363
STACKABLE_LOG_CONFIG_MOUNT_DIR, STACKABLE_LOG_CONFIG_MOUNT_DIR_NAME,
64-
STACKABLE_LOG_DIR_NAME, v1alpha1,
64+
STACKABLE_LOG_DIR_NAME,
6565
},
6666
};
6767

@@ -144,7 +144,6 @@ pub(crate) const HDFS_CONFIG_MOUNT_DIR: &str = "/stackable/mount/hdfs-config";
144144
/// The [`Pod`](`stackable_operator::k8s_openapi::api::core::v1::Pod`)s are accessible through the
145145
/// corresponding [`Service`](`stackable_operator::k8s_openapi::api::core::v1::Service`) (via [`build_rolegroup_headless_service`](super::service::build_rolegroup_headless_service) and metrics from [`build_rolegroup_metrics_service`](super::service::build_rolegroup_metrics_service)).
146146
pub(crate) fn build_metastore_rolegroup_statefulset(
147-
hive: &v1alpha1::HiveCluster,
148147
hive_role: &HiveRole,
149148
cluster: &ValidatedCluster,
150149
role_group_name: &RoleGroupName,
@@ -165,7 +164,7 @@ pub(crate) fn build_metastore_rolegroup_statefulset(
165164
"HADOOP_HEAPSIZE",
166165
construct_hadoop_heapsize_env(merged_config).context(ConstructJvmArgumentsSnafu)?,
167166
)
168-
.add_env_var("HADOOP_OPTS", construct_non_heap_jvm_args(hive, rg))
167+
.add_env_var("HADOOP_OPTS", construct_non_heap_jvm_args(cluster, rg))
169168
.add_env_var(
170169
"CONTAINERDEBUG_LOG_DIRECTORY",
171170
format!("{STACKABLE_LOG_DIR}/containerdebug"),
@@ -178,7 +177,7 @@ pub(crate) fn build_metastore_rolegroup_statefulset(
178177

179178
let mut pod_builder = PodBuilder::new();
180179

181-
if let Some(hdfs) = &hive.spec.cluster_config.hdfs {
180+
if let Some(hdfs) = &cluster.cluster_config.hdfs {
182181
pod_builder
183182
.add_volume(
184183
VolumeBuilder::new(&*HDFS_DISCOVERY_VOLUME_NAME)
@@ -230,7 +229,7 @@ pub(crate) fn build_metastore_rolegroup_statefulset(
230229
.context(AddVolumeSnafu)?;
231230
}
232231

233-
let db_type = hive.spec.cluster_config.metadata_database.as_hive_db_type();
232+
let db_type = &cluster.cluster_config.db_type;
234233
let start_command = if resolved_product_image.product_version.starts_with("3.") {
235234
// The schematool version in 3.1.x does *not* support the `-initOrUpgradeSchema` flag yet, so we can not use that.
236235
// As we *only* support HMS 3.1.x (or newer) since SDP release 23.11, we can safely assume we are always coming
@@ -262,7 +261,7 @@ pub(crate) fn build_metastore_rolegroup_statefulset(
262261
"-c".to_string(),
263262
])
264263
.args(build_container_command_args(
265-
hive,
264+
cluster,
266265
formatdoc! {"
267266
{kerberos_container_start_commands}
268267
@@ -274,7 +273,7 @@ pub(crate) fn build_metastore_rolegroup_statefulset(
274273
wait_for_termination $!
275274
{create_vector_shutdown_file_command}
276275
",
277-
kerberos_container_start_commands = kerberos_container_start_commands(hive),
276+
kerberos_container_start_commands = kerberos_container_start_commands(cluster),
278277
remove_vector_shutdown_file_command =
279278
remove_vector_shutdown_file_command(STACKABLE_LOG_DIR),
280279
create_vector_shutdown_file_command =
@@ -403,8 +402,8 @@ pub(crate) fn build_metastore_rolegroup_statefulset(
403402

404403
add_graceful_shutdown_config(merged_config, &mut pod_builder).context(GracefulShutdownSnafu)?;
405404

406-
if hive.has_kerberos_enabled() {
407-
add_kerberos_pod_config(hive, hive_role, container_builder, &mut pod_builder)
405+
if cluster.has_kerberos_enabled() {
406+
add_kerberos_pod_config(cluster, hive_role, container_builder, &mut pod_builder)
408407
.context(AddKerberosConfigSnafu)?;
409408
}
410409

rust/operator-binary/src/controller/validate.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,28 @@ pub fn validate_cluster(
228228
_ => metadata_database_connection_details.driver.clone(),
229229
};
230230

231+
// The database type passed to Hive via the `--db-type` CLI argument.
232+
let db_type = hive
233+
.spec
234+
.cluster_config
235+
.metadata_database
236+
.as_hive_db_type()
237+
.to_owned();
238+
239+
let hdfs = hive.spec.cluster_config.hdfs.clone();
240+
241+
let kerberos_secret_class = hive.kerberos_secret_class();
242+
231243
// Kerberos-related `hive-site.xml` entries (empty when Kerberos is disabled).
232-
let kerberos_config = if hive.has_kerberos_enabled() {
244+
let kerberos_config = if kerberos_secret_class.is_some() {
233245
kerberos_config_properties(name.as_ref(), namespace.as_ref(), cluster_info)
234246
} else {
235247
BTreeMap::new()
236248
};
237249

238250
// A `core-site.xml` with `hadoop.security.authentication=kerberos` is required when
239251
// Kerberos is enabled and there is no HDFS backend (i.e. S3).
240-
let needs_kerberos_core_site =
241-
hive.has_kerberos_enabled() && hive.spec.cluster_config.hdfs.is_none();
252+
let needs_kerberos_core_site = kerberos_secret_class.is_some() && hdfs.is_none();
242253

243254
Ok(ValidatedCluster::new(
244255
name,
@@ -249,8 +260,11 @@ pub fn validate_cluster(
249260
ValidatedClusterConfig {
250261
metadata_database_connection_details,
251262
connection_driver,
263+
db_type,
264+
hdfs,
252265
s3_connection_spec: dereferenced_objects.s3_connection_spec,
253266
hive_opa_config: dereferenced_objects.hive_opa_config,
267+
kerberos_secret_class,
254268
kerberos_config,
255269
needs_kerberos_core_site,
256270
},

0 commit comments

Comments
 (0)