Skip to content

Commit b19ac59

Browse files
committed
refactor: tighten constant visibility
1 parent c8a1ae9 commit b19ac59

4 files changed

Lines changed: 44 additions & 19 deletions

File tree

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ use super::{
1414
use crate::{
1515
controller::{
1616
NifiRoleGroupConfig, ValidatedCluster,
17-
build::{HTTPS_PORT, NIFI_CONFIG_DIRECTORY, NIFI_PYTHON_WORKING_DIRECTORY, PROTOCOL_PORT},
17+
build::{
18+
HTTPS_PORT, NIFI_CONFIG_DIRECTORY, NIFI_PYTHON_WORKING_DIRECTORY, PROTOCOL_PORT,
19+
resource::statefulset::{
20+
NODE_ADDRESS_ENV, STACKLET_NAME_ENV, ZOOKEEPER_CHROOT_ENV, ZOOKEEPER_HOSTS_ENV,
21+
},
22+
},
1823
},
1924
crd::{storage::NifiRepository, v1alpha1},
2025
security::{
@@ -405,7 +410,7 @@ pub fn build(
405410
//#############################################
406411
properties.insert(
407412
"nifi.web.https.host".to_string(),
408-
env_reference("NODE_ADDRESS"),
413+
env_reference(NODE_ADDRESS_ENV),
409414
);
410415
properties.insert("nifi.web.https.port".to_string(), HTTPS_PORT.to_string());
411416
properties.insert(
@@ -515,7 +520,7 @@ pub fn build(
515520
properties.insert("nifi.cluster.is.node".to_string(), "true".to_string());
516521
properties.insert(
517522
"nifi.cluster.node.address".to_string(),
518-
env_reference("NODE_ADDRESS"),
523+
env_reference(NODE_ADDRESS_ENV),
519524
);
520525
properties.insert(
521526
"nifi.cluster.node.protocol.port".to_string(),
@@ -536,13 +541,13 @@ pub fn build(
536541
// this will be replaced via a container command script
537542
properties.insert(
538543
"nifi.zookeeper.connect.string".to_string(),
539-
env_reference("ZOOKEEPER_HOSTS"),
544+
env_reference(ZOOKEEPER_HOSTS_ENV),
540545
);
541546

542547
// this will be replaced via a container command script
543548
properties.insert(
544549
"nifi.zookeeper.root.node".to_string(),
545-
env_reference("ZOOKEEPER_CHROOT"),
550+
env_reference(ZOOKEEPER_CHROOT_ENV),
546551
);
547552
}
548553

@@ -557,7 +562,7 @@ pub fn build(
557562
// this will be replaced via a container command script
558563
properties.insert(
559564
"nifi.cluster.leader.election.kubernetes.lease.prefix".to_string(),
560-
env_reference("STACKLET_NAME"),
565+
env_reference(STACKLET_NAME_ENV),
561566
);
562567
}
563568
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ mod tests {
5252

5353
use stackable_operator::v2::types::kubernetes::ConfigMapName;
5454

55-
use super::*;
55+
use super::{super::env_reference, *};
56+
use crate::controller::build::resource::statefulset::{
57+
STACKLET_NAME_ENV, ZOOKEEPER_CHROOT_ENV, ZOOKEEPER_HOSTS_ENV,
58+
};
5659

5760
#[test]
5861
fn test_build_state_management_xml_kubernetes() {
5962
let xml = build(&NifiClusteringBackend::Kubernetes {});
6063
assert!(xml.contains(KUBERNETES_STATE_PROVIDER_ID));
6164
assert!(xml.contains("KubernetesConfigMapStateProvider"));
62-
assert!(xml.contains("${env:STACKLET_NAME}"));
65+
assert!(xml.contains(&env_reference(STACKLET_NAME_ENV)));
6366
assert!(!xml.contains(ZOOKEEPER_STATE_PROVIDER_ID));
6467
}
6568

@@ -71,8 +74,8 @@ mod tests {
7174
});
7275
assert!(xml.contains(ZOOKEEPER_STATE_PROVIDER_ID));
7376
assert!(xml.contains("ZooKeeperStateProvider"));
74-
assert!(xml.contains("${env:ZOOKEEPER_HOSTS}"));
75-
assert!(xml.contains("${env:ZOOKEEPER_CHROOT}"));
77+
assert!(xml.contains(&env_reference(ZOOKEEPER_HOSTS_ENV)));
78+
assert!(xml.contains(&env_reference(ZOOKEEPER_CHROOT_ENV)));
7679
assert!(!xml.contains(KUBERNETES_STATE_PROVIDER_ID));
7780
}
7881
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ use stackable_operator::utils::cluster_info::KubernetesClusterInfo;
66

77
use crate::controller::{
88
ValidatedCluster,
9-
build::{HTTPS_PORT, properties::env_reference, resource::reporting_task},
9+
build::{
10+
HTTPS_PORT,
11+
properties::env_reference,
12+
resource::{
13+
reporting_task,
14+
statefulset::{LISTENER_DEFAULT_ADDRESS_ENV, LISTENER_DEFAULT_PORT_HTTPS_ENV},
15+
},
16+
},
1017
};
1118

1219
/// Computes the comma-separated NiFi proxy hosts, or `"*"` if `hostHeaderCheck.allowAll` is set.
@@ -35,8 +42,8 @@ pub fn compute_proxy_hosts(
3542
// Address and port are injected from the listener volume during the prepare container
3643
let mut proxy_hosts = HashSet::from([format!(
3744
"{address}:{port}",
38-
address = env_reference("LISTENER_DEFAULT_ADDRESS"),
39-
port = env_reference("LISTENER_DEFAULT_PORT_HTTPS")
45+
address = env_reference(LISTENER_DEFAULT_ADDRESS_ENV),
46+
port = env_reference(LISTENER_DEFAULT_PORT_HTTPS_ENV)
4047
)]);
4148
proxy_hosts.extend(host_header_check.additional_allowed_hosts.iter().cloned());
4249

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ stackable_operator::constant!(VECTOR_CONTAINER_NAME: ContainerName = "vector");
154154
stackable_operator::constant!(VECTOR_LOG_CONFIG_VOLUME_NAME: VolumeName = "config");
155155
stackable_operator::constant!(VECTOR_LOG_VOLUME_NAME: VolumeName = "log");
156156

157+
// Names of the environment variables injected into the NiFi container here (or exported by the
158+
// prepare init-container) and referenced as `${env:...}` placeholders by the config-file builders
159+
// (`nifi.properties`, `state-management.xml`, proxy hosts). The two sides must agree.
160+
pub(crate) const STACKLET_NAME_ENV: &str = "STACKLET_NAME";
161+
pub(crate) const NODE_ADDRESS_ENV: &str = "NODE_ADDRESS";
162+
pub(crate) const ZOOKEEPER_HOSTS_ENV: &str = "ZOOKEEPER_HOSTS";
163+
pub(crate) const ZOOKEEPER_CHROOT_ENV: &str = "ZOOKEEPER_CHROOT";
164+
pub(crate) const LISTENER_DEFAULT_ADDRESS_ENV: &str = "LISTENER_DEFAULT_ADDRESS";
165+
pub(crate) const LISTENER_DEFAULT_PORT_HTTPS_ENV: &str = "LISTENER_DEFAULT_PORT_HTTPS";
166+
157167
/// The rolegroup [`StatefulSet`] runs the rolegroup, as configured by the administrator.
158168
///
159169
/// The [`Pod`](`stackable_operator::k8s_openapi::api::core::v1::Pod`)s are accessible through the
@@ -207,7 +217,7 @@ pub(crate) async fn build_node_rolegroup_statefulset(
207217
});
208218

209219
env_vars.push(EnvVar {
210-
name: "STACKLET_NAME".to_string(),
220+
name: STACKLET_NAME_ENV.to_string(),
211221
value: Some(cluster.name.to_string()),
212222
..Default::default()
213223
});
@@ -228,8 +238,8 @@ pub(crate) async fn build_node_rolegroup_statefulset(
228238
}),
229239
..EnvVar::default()
230240
};
231-
env_vars.push(zookeeper_env_var("ZOOKEEPER_HOSTS"));
232-
env_vars.push(zookeeper_env_var("ZOOKEEPER_CHROOT"));
241+
env_vars.push(zookeeper_env_var(ZOOKEEPER_HOSTS_ENV));
242+
env_vars.push(zookeeper_env_var(ZOOKEEPER_CHROOT_ENV));
233243
}
234244
v1alpha1::NifiClusteringBackend::Kubernetes {} => {}
235245
}
@@ -289,7 +299,7 @@ pub(crate) async fn build_node_rolegroup_statefulset(
289299
"test -L {NIFI_CONFIG_DIRECTORY}/{logback} || ln -sf {STACKABLE_LOG_CONFIG_DIR}/{logback} {NIFI_CONFIG_DIRECTORY}/{logback}",
290300
logback = ConfigFileName::Logback
291301
),
292-
format!(r#"export NODE_ADDRESS="{node_address}""#),
302+
format!(r#"export {NODE_ADDRESS_ENV}="{node_address}""#),
293303
]);
294304

295305
// This commands needs to go first, as they might set env variables needed by the templating
@@ -308,10 +318,10 @@ pub(crate) async fn build_node_rolegroup_statefulset(
308318
}
309319

310320
prepare_args.push(format!(
311-
"export LISTENER_DEFAULT_ADDRESS=$(cat {LISTENER_VOLUME_DIR}/default-address/address)"
321+
"export {LISTENER_DEFAULT_ADDRESS_ENV}=$(cat {LISTENER_VOLUME_DIR}/default-address/address)"
312322
));
313323
prepare_args.push(format!(
314-
"export LISTENER_DEFAULT_PORT_HTTPS=$(cat {LISTENER_VOLUME_DIR}/default-address/ports/https)"
324+
"export {LISTENER_DEFAULT_PORT_HTTPS_ENV}=$(cat {LISTENER_VOLUME_DIR}/default-address/ports/https)"
315325
));
316326

317327
// Template the config files that contain `${env:...}`/`${file:...}` placeholders, in a fixed

0 commit comments

Comments
 (0)