Skip to content

Commit c72aa85

Browse files
committed
refactor: make better use of ValidatedCluster in property file builders
1 parent ef12f34 commit c72aa85

4 files changed

Lines changed: 33 additions & 43 deletions

File tree

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use crate::{
1313
build::properties::logging::role_group_config_map_data,
1414
},
1515
crd::{
16-
ConfigFileName, MetadataManager, STACKABLE_LISTENER_BOOTSTRAP_DIR,
17-
STACKABLE_LISTENER_BROKER_DIR,
16+
ConfigFileName, STACKABLE_LISTENER_BOOTSTRAP_DIR, STACKABLE_LISTENER_BROKER_DIR,
1817
listener::{KafkaListenerConfig, node_address_cmd},
1918
role::AnyConfig,
2019
v1alpha1,
@@ -63,7 +62,8 @@ pub fn build_rolegroup_config_map(
6362
validated_rg: &ValidatedRoleGroupConfig,
6463
listener_config: &KafkaListenerConfig,
6564
) -> Result<ConfigMap, Error> {
66-
let kafka_security = &validated_cluster.cluster_config.kafka_security;
65+
let cluster_config = &validated_cluster.cluster_config;
66+
let kafka_security = &cluster_config.kafka_security;
6767
let resolved_product_image = &validated_cluster.image;
6868
let kafka_config_file_name = validated_rg.config.config_file_name().to_string();
6969
let config_overrides = validated_rg
@@ -72,36 +72,20 @@ pub fn build_rolegroup_config_map(
7272
.overrides
7373
.clone();
7474

75-
let opa_connect = validated_cluster
76-
.cluster_config
77-
.authorization_config
78-
.as_ref()
79-
.map(|auth_config| auth_config.opa_connect.clone());
80-
81-
let kraft_mode = validated_cluster.cluster_config.metadata_manager == MetadataManager::KRaft;
82-
83-
if kraft_mode && validated_cluster.cluster_config.pod_descriptors.is_empty() {
75+
if cluster_config.is_kraft_mode() && cluster_config.pod_descriptors.is_empty() {
8476
return NoKraftControllersFoundSnafu.fail();
8577
}
8678

8779
let kafka_config = match &validated_rg.config {
8880
AnyConfig::Broker(_) => crate::controller::build::properties::broker_properties::build(
89-
kafka_security,
81+
cluster_config,
9082
listener_config,
91-
&validated_cluster.cluster_config.pod_descriptors,
92-
opa_connect.as_deref(),
93-
kraft_mode,
94-
validated_cluster
95-
.cluster_config
96-
.disable_broker_id_generation,
9783
config_overrides,
9884
),
9985
AnyConfig::Controller(_) => {
10086
crate::controller::build::properties::controller_properties::build(
101-
kafka_security,
87+
cluster_config,
10288
listener_config,
103-
&validated_cluster.cluster_config.pod_descriptors,
104-
kraft_mode,
10589
config_overrides,
10690
)
10791
}

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@ use std::collections::BTreeMap;
22

33
use super::kraft_controllers;
44
use crate::{
5+
controller::ValidatedClusterConfig,
56
crd::{
6-
KafkaPodDescriptor,
77
listener::{KafkaListenerConfig, KafkaListenerName},
88
role::{
99
KAFKA_ADVERTISED_LISTENERS, KAFKA_BROKER_ID, KAFKA_CONTROLLER_QUORUM_BOOTSTRAP_SERVERS,
1010
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP, KAFKA_LISTENERS, KAFKA_LOG_DIRS, KAFKA_NODE_ID,
1111
KAFKA_PROCESS_ROLES, KafkaRole,
1212
},
13-
security::KafkaTlsSecurity,
1413
},
1514
operations::graceful_shutdown::graceful_shutdown_config_properties,
1615
};
1716

1817
pub fn build(
19-
kafka_security: &KafkaTlsSecurity,
18+
cluster_config: &ValidatedClusterConfig,
2019
listener_config: &KafkaListenerConfig,
21-
pod_descriptors: &[KafkaPodDescriptor],
22-
opa_connect_string: Option<&str>,
23-
kraft_mode: bool,
24-
disable_broker_id_generation: bool,
2520
overrides: BTreeMap<String, String>,
2621
) -> BTreeMap<String, String> {
27-
let kraft_controllers = kraft_controllers(pod_descriptors);
22+
let kraft_controllers = kraft_controllers(&cluster_config.pod_descriptors);
2823

2924
let mut result = BTreeMap::from([
3025
(
@@ -46,7 +41,7 @@ pub fn build(
4641
),
4742
]);
4843

49-
if kraft_mode {
44+
if cluster_config.is_kraft_mode() {
5045
let kraft_controllers = kraft_controllers.join(",");
5146

5247
// Running in KRaft mode
@@ -79,7 +74,7 @@ pub fn build(
7974
// so we disable automatic id generation.
8075
// This check ensures that existing clusters running in ZooKeeper mode do not
8176
// suddenly break after the introduction of this change.
82-
if disable_broker_id_generation {
77+
if cluster_config.disable_broker_id_generation {
8378
result.extend([
8479
(
8580
"broker.id.generation.enable".to_string(),
@@ -91,7 +86,7 @@ pub fn build(
9186
}
9287

9388
// Enable OPA authorization
94-
if opa_connect_string.is_some() {
89+
if let Some(opa_connect_string) = cluster_config.opa_connect() {
9590
result.extend([
9691
(
9792
"authorizer.class.name".to_string(),
@@ -103,12 +98,12 @@ pub fn build(
10398
),
10499
(
105100
"opa.authorizer.url".to_string(),
106-
opa_connect_string.unwrap_or_default().to_string(),
101+
opa_connect_string.to_string(),
107102
),
108103
]);
109104
}
110105

111-
result.extend(kafka_security.broker_config_settings());
106+
result.extend(cluster_config.kafka_security.broker_config_settings());
112107
result.extend(graceful_shutdown_config_properties());
113108
result.extend(overrides);
114109

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@ use std::collections::BTreeMap;
22

33
use super::kraft_controllers;
44
use crate::{
5+
controller::ValidatedClusterConfig,
56
crd::{
6-
KafkaPodDescriptor,
77
listener::{KafkaListenerConfig, KafkaListenerName},
88
role::{
99
KAFKA_CONTROLLER_QUORUM_BOOTSTRAP_SERVERS, KAFKA_LISTENER_SECURITY_PROTOCOL_MAP,
1010
KAFKA_LISTENERS, KAFKA_LOG_DIRS, KAFKA_NODE_ID, KAFKA_PROCESS_ROLES, KafkaRole,
1111
},
12-
security::KafkaTlsSecurity,
1312
},
1413
operations::graceful_shutdown::graceful_shutdown_config_properties,
1514
};
1615

1716
pub fn build(
18-
kafka_security: &KafkaTlsSecurity,
17+
cluster_config: &ValidatedClusterConfig,
1918
listener_config: &KafkaListenerConfig,
20-
pod_descriptors: &[KafkaPodDescriptor],
21-
kraft_mode: bool,
2219
overrides: BTreeMap<String, String>,
2320
) -> BTreeMap<String, String> {
24-
let kraft_controllers = kraft_controllers(pod_descriptors).join(",");
21+
let kraft_controllers = kraft_controllers(&cluster_config.pod_descriptors).join(",");
2522

2623
let mut result = BTreeMap::from([
2724
(
@@ -58,14 +55,14 @@ pub fn build(
5855

5956
// The ZooKeeper connection is needed for migration from ZooKeeper to KRaft mode.
6057
// It is not needed once the controller is fully running in KRaft mode.
61-
if !kraft_mode {
58+
if !cluster_config.is_kraft_mode() {
6259
result.insert(
6360
"zookeeper.connect".to_string(),
6461
"${env:ZOOKEEPER}".to_string(),
6562
);
6663
}
6764

68-
result.extend(kafka_security.controller_config_settings());
65+
result.extend(cluster_config.kafka_security.controller_config_settings());
6966
result.extend(graceful_shutdown_config_properties());
7067
result.extend(overrides);
7168

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ pub struct ValidatedClusterConfig {
9292
pub disable_broker_id_generation: bool,
9393
}
9494

95+
impl ValidatedClusterConfig {
96+
/// Whether the cluster runs in KRaft mode (as opposed to ZooKeeper mode).
97+
pub fn is_kraft_mode(&self) -> bool {
98+
self.metadata_manager == MetadataManager::KRaft
99+
}
100+
101+
/// The OPA connect string, if OPA authorization is configured.
102+
pub fn opa_connect(&self) -> Option<&str> {
103+
self.authorization_config
104+
.as_ref()
105+
.map(|auth_config| auth_config.opa_connect.as_str())
106+
}
107+
}
108+
95109
/// Lets [`ValidatedCluster`] act as the owner [`Resource`] for child objects, so owner
96110
/// references are built from it (via the captured `metadata`) rather than the raw CR.
97111
impl Resource for ValidatedCluster {

0 commit comments

Comments
 (0)