Skip to content

Commit b2084d2

Browse files
committed
refactor: use v2 VolumeName
1 parent 9a6d111 commit b2084d2

4 files changed

Lines changed: 49 additions & 29 deletions

File tree

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::BTreeMap;
1+
use std::{collections::BTreeMap, str::FromStr};
22

33
use snafu::{ResultExt, Snafu};
44
use stackable_operator::{
@@ -13,6 +13,7 @@ use stackable_operator::{
1313
commons::secret_class::SecretClassVolumeProvisionParts,
1414
shared::time::Duration,
1515
utils::cluster_info::KubernetesClusterInfo,
16+
v2::types::kubernetes::VolumeName,
1617
};
1718

1819
use crate::{
@@ -25,8 +26,8 @@ pub const STACKABLE_KERBEROS_DIR: &str = "/stackable/kerberos";
2526
/// Path of the `krb5.conf` rendered into the Kerberos secret volume. Referenced both here (the
2627
/// `KRB5_CONFIG` env var) and by the JVM args builder.
2728
pub const KRB5_CONFIG_PATH: &str = const_format::concatcp!(STACKABLE_KERBEROS_DIR, "/krb5.conf");
28-
/// Name of the Kerberos secret volume.
29-
const KERBEROS_VOLUME_NAME: &str = "kerberos";
29+
// Name of the Kerberos secret volume.
30+
stackable_operator::constant!(KERBEROS_VOLUME_NAME: VolumeName = "kerberos");
3031
/// The RPC/data-transfer quality-of-protection level used when Kerberos is enabled.
3132
const PROTECTION_PRIVACY: &str = "privacy";
3233

@@ -242,12 +243,12 @@ pub fn add_kerberos_pod_config(
242243
.build()
243244
.context(BuildKerberosSecretVolumeSnafu)?;
244245
pb.add_volume(
245-
VolumeBuilder::new(KERBEROS_VOLUME_NAME)
246+
VolumeBuilder::new(&*KERBEROS_VOLUME_NAME)
246247
.ephemeral(kerberos_secret_operator_volume)
247248
.build(),
248249
)
249250
.context(AddVolumeSnafu)?;
250-
cb.add_volume_mount(KERBEROS_VOLUME_NAME, STACKABLE_KERBEROS_DIR)
251+
cb.add_volume_mount(&*KERBEROS_VOLUME_NAME, STACKABLE_KERBEROS_DIR)
251252
.context(AddVolumeMountSnafu)?;
252253

253254
// Needed env vars
@@ -257,7 +258,7 @@ pub fn add_kerberos_pod_config(
257258
if let Some(https_secret_class) = cluster.cluster_config.https_secret_class.clone() {
258259
// Mount TLS keystore
259260
pb.add_volume(
260-
VolumeBuilder::new(TLS_STORE_VOLUME_NAME)
261+
VolumeBuilder::new(&*TLS_STORE_VOLUME_NAME)
261262
.ephemeral(
262263
SecretOperatorVolumeSourceBuilder::new(
263264
https_secret_class,
@@ -278,7 +279,7 @@ pub fn add_kerberos_pod_config(
278279
.build(),
279280
)
280281
.context(AddVolumeSnafu)?;
281-
cb.add_volume_mount(TLS_STORE_VOLUME_NAME, TLS_STORE_DIR)
282+
cb.add_volume_mount(&*TLS_STORE_VOLUME_NAME, TLS_STORE_DIR)
282283
.context(AddVolumeMountSnafu)?;
283284
}
284285
Ok(())

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Build the listener `Volume`/`PersistentVolumeClaim` exposing a rolegroup.
22
3-
use std::str::FromStr;
3+
use std::{str::FromStr, sync::LazyLock};
44

55
use snafu::{ResultExt, Snafu};
66
use stackable_operator::{
@@ -21,6 +21,13 @@ use stackable_operator::{
2121

2222
use crate::crd::{AnyServiceConfig, HbaseRole, LISTENER_VOLUME_NAME};
2323

24+
/// The rest servers' listener `PersistentVolumeClaim` reuses the listener volume name
25+
/// ([`LISTENER_VOLUME_NAME`]); the claim and the volume must share a name.
26+
static LISTENER_PVC_NAME: LazyLock<PersistentVolumeClaimName> = LazyLock::new(|| {
27+
PersistentVolumeClaimName::from_str(LISTENER_VOLUME_NAME)
28+
.expect("LISTENER_VOLUME_NAME is a valid PersistentVolumeClaim name")
29+
});
30+
2431
#[derive(Snafu, Debug)]
2532
pub enum Error {
2633
#[snafu(display("failed to build listener volume"))]
@@ -72,8 +79,7 @@ pub fn build_listener_pvc(
7279
HbaseRole::RestServer => Some(vec![listener_operator_volume_source_builder_build_pvc(
7380
&TypedListenerReference::ListenerClass(merged_config.listener_class()),
7481
recommended_labels,
75-
&PersistentVolumeClaimName::from_str(LISTENER_VOLUME_NAME)
76-
.expect("LISTENER_VOLUME_NAME is a valid PersistentVolumeClaim name"),
82+
&LISTENER_PVC_NAME,
7783
)]),
7884
}
7985
}

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ stackable_operator::constant!(HDFS_DISCOVERY_VOLUME_NAME: VolumeName = "hdfs-dis
5757
stackable_operator::constant!(LOG_CONFIG_VOLUME_NAME: VolumeName = "log-config");
5858
stackable_operator::constant!(LOG_VOLUME_NAME: VolumeName = "log");
5959

60+
// Environment variable names set on the HBase container. Declared as typed constants (instead of
61+
// `EnvVarName::from_str_unsafe` at the use site) and validated by `env_var_names_are_valid`.
62+
stackable_operator::constant!(HBASE_CONF_DIR_ENV: EnvVarName = "HBASE_CONF_DIR");
63+
stackable_operator::constant!(HADOOP_CONF_DIR_ENV: EnvVarName = "HADOOP_CONF_DIR");
64+
stackable_operator::constant!(REGION_MOVER_OPTS_ENV: EnvVarName = "REGION_MOVER_OPTS");
65+
stackable_operator::constant!(RUN_REGION_MOVER_ENV: EnvVarName = "RUN_REGION_MOVER");
66+
stackable_operator::constant!(STACKABLE_LOG_DIR_ENV: EnvVarName = "STACKABLE_LOG_DIR");
67+
6068
pub static CONTAINERDEBUG_LOG_DIRECTORY: std::sync::LazyLock<String> =
6169
std::sync::LazyLock::new(|| format!("{STACKABLE_LOG_DIR}/containerdebug"));
6270

@@ -148,29 +156,17 @@ pub fn build_rolegroup_statefulset(
148156
};
149157

150158
let merged_env = EnvVarSet::new()
151-
.with_value(
152-
&EnvVarName::from_str_unsafe("HBASE_CONF_DIR"),
153-
CONFIG_DIR_NAME,
154-
)
159+
.with_value(&HBASE_CONF_DIR_ENV, CONFIG_DIR_NAME)
155160
// required by phoenix (for cases where Kerberos is enabled): see https://issues.apache.org/jira/browse/PHOENIX-2369
156-
.with_value(
157-
&EnvVarName::from_str_unsafe("HADOOP_CONF_DIR"),
158-
CONFIG_DIR_NAME,
159-
)
161+
.with_value(&HADOOP_CONF_DIR_ENV, CONFIG_DIR_NAME)
160162
.merge(validated_rg_config.env_overrides.clone())
161163
// These env vars are set for all roles to avoid bash's "unbound variable" errors.
164+
.with_value(&REGION_MOVER_OPTS_ENV, merged_config.region_mover_args())
162165
.with_value(
163-
&EnvVarName::from_str_unsafe("REGION_MOVER_OPTS"),
164-
merged_config.region_mover_args(),
165-
)
166-
.with_value(
167-
&EnvVarName::from_str_unsafe("RUN_REGION_MOVER"),
166+
&RUN_REGION_MOVER_ENV,
168167
merged_config.run_region_mover().to_string(),
169168
)
170-
.with_value(
171-
&EnvVarName::from_str_unsafe("STACKABLE_LOG_DIR"),
172-
STACKABLE_LOG_DIR,
173-
);
169+
.with_value(&STACKABLE_LOG_DIR_ENV, STACKABLE_LOG_DIR);
174170

175171
let role_name = hbase_role.cli_role_name();
176172
let mut hbase_container = new_container_builder(&HBASE_CONTAINER_NAME);
@@ -353,3 +349,20 @@ fn command() -> Vec<String> {
353349
"-c".to_string(),
354350
]
355351
}
352+
353+
#[cfg(test)]
354+
mod tests {
355+
use super::*;
356+
357+
/// The env-var-name constants are built with `EnvVarName::from_str`, which panics on an invalid
358+
/// name. This test forces every constant to be evaluated so a typo is caught at test time rather
359+
/// than during reconciliation.
360+
#[test]
361+
fn env_var_names_are_valid() {
362+
assert_eq!(HBASE_CONF_DIR_ENV.to_string(), "HBASE_CONF_DIR");
363+
assert_eq!(HADOOP_CONF_DIR_ENV.to_string(), "HADOOP_CONF_DIR");
364+
assert_eq!(REGION_MOVER_OPTS_ENV.to_string(), "REGION_MOVER_OPTS");
365+
assert_eq!(RUN_REGION_MOVER_ENV.to_string(), "RUN_REGION_MOVER");
366+
assert_eq!(STACKABLE_LOG_DIR_ENV.to_string(), "STACKABLE_LOG_DIR");
367+
}
368+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use stackable_operator::{
2929
role_utils::JavaCommonConfig,
3030
types::{
3131
common::Port,
32-
kubernetes::{ConfigMapName, ListenerClassName, SecretClassName},
32+
kubernetes::{ConfigMapName, ListenerClassName, SecretClassName, VolumeName},
3333
},
3434
},
3535
versioned::versioned,
@@ -50,7 +50,7 @@ pub const OPERATOR_NAME: &str = "hbase.stackable.com";
5050
pub const CONFIG_DIR_NAME: &str = "/stackable/conf";
5151

5252
pub const TLS_STORE_DIR: &str = "/stackable/tls";
53-
pub const TLS_STORE_VOLUME_NAME: &str = "tls";
53+
stackable_operator::constant!(pub TLS_STORE_VOLUME_NAME: VolumeName = "tls");
5454
pub const TLS_STORE_PASSWORD: &str = "changeit";
5555
/// The key- and truststore type used for all HBase TLS stores.
5656
pub const TLS_STORE_TYPE: &str = "pkcs12";

0 commit comments

Comments
 (0)