Skip to content

Commit f223e90

Browse files
committed
refactor: improve shutdown config, fix securitiy config exposed_ports
1 parent dab9448 commit f223e90

4 files changed

Lines changed: 52 additions & 63 deletions

File tree

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,29 @@ pub fn add_graceful_shutdown_config(
2626
// This must be always set by the merge mechanism, as we provide a default value,
2727
// users can not disable graceful shutdown.
2828
if let Some(termination_grace_period) = graceful_shutdown_timeout {
29-
match role {
30-
DruidRole::Coordinator
31-
| DruidRole::Broker
32-
| DruidRole::Historical
33-
| DruidRole::Router => {
34-
pod_builder
35-
.termination_grace_period(&termination_grace_period)
36-
.context(SetTerminationGracePeriodSnafu)?;
37-
}
38-
DruidRole::MiddleManager => {
39-
pod_builder
40-
.termination_grace_period(&termination_grace_period)
41-
.context(SetTerminationGracePeriodSnafu)?;
29+
// Every role gets the termination grace period; only the MiddleManager additionally needs a
30+
// pre-stop hook that drains running tasks before the period elapses.
31+
pod_builder
32+
.termination_grace_period(&termination_grace_period)
33+
.context(SetTerminationGracePeriodSnafu)?;
4234

43-
let (protocol, port) = if tls_security.tls_enabled() {
44-
("https", role.get_https_port())
45-
} else {
46-
("http", role.get_http_port())
47-
};
35+
if *role == DruidRole::MiddleManager {
36+
let (protocol, port) = if tls_security.tls_enabled() {
37+
("https", role.get_https_port())
38+
} else {
39+
("http", role.get_http_port())
40+
};
4841

49-
let middle_manager_host = format!("{protocol}://127.0.0.1:{port}");
50-
let debug_message =
51-
"$(date --utc +%FT%T,%3N) INFO [stackable_lifecycle_pre_stop] -";
52-
let sleep_interval = 2;
42+
let middle_manager_host = format!("{protocol}://127.0.0.1:{port}");
43+
let debug_message = "$(date --utc +%FT%T,%3N) INFO [stackable_lifecycle_pre_stop] -";
44+
let sleep_interval = 2;
5345

54-
// The middle manager can be terminated gracefully by disabling it, meaning
55-
// the overlord will not send any new tasks and it will terminate after
56-
// all tasks are finished or the termination grace period is exceeded.
57-
// See: https://druid.apache.org/docs/latest/operations/rolling-updates/#rolling-restart-graceful-termination-based
58-
// The DRUID_PID is set in the crd/src/lib.rs `main_container_start_command` method.
59-
druid_builder.lifecycle_pre_stop(LifecycleHandler {
46+
// The middle manager can be terminated gracefully by disabling it, meaning
47+
// the overlord will not send any new tasks and it will terminate after
48+
// all tasks are finished or the termination grace period is exceeded.
49+
// See: https://druid.apache.org/docs/latest/operations/rolling-updates/#rolling-restart-graceful-termination-based
50+
// The DRUID_PID is set in the crd/src/lib.rs `main_container_start_command` method.
51+
druid_builder.lifecycle_pre_stop(LifecycleHandler {
6052
exec: Some(ExecAction {
6153
command: Some(vec![
6254
"/bin/bash".to_string(),
@@ -103,7 +95,6 @@ pub fn add_graceful_shutdown_config(
10395
}),
10496
..Default::default()
10597
});
106-
}
10798
}
10899
}
109100

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const DEFAULT_NETWORKADDRESS_CACHE_NEGATIVE_TTL: &str = "0";
1313
/// `overrides` are the user's merged (role <- rolegroup) `security.properties`
1414
/// config overrides, as returned by `DruidConfigOverrides::get_key_value_overrides`.
1515
/// Override values win.
16-
pub fn build(overrides: &BTreeMap<String, String>) -> BTreeMap<String, String> {
16+
pub fn build(overrides: BTreeMap<String, String>) -> BTreeMap<String, String> {
1717
let mut props: BTreeMap<String, String> = BTreeMap::new();
1818
props.insert(
1919
NETWORKADDRESS_CACHE_TTL.to_string(),
@@ -23,7 +23,7 @@ pub fn build(overrides: &BTreeMap<String, String>) -> BTreeMap<String, String> {
2323
NETWORKADDRESS_CACHE_NEGATIVE_TTL.to_string(),
2424
DEFAULT_NETWORKADDRESS_CACHE_NEGATIVE_TTL.to_string(),
2525
);
26-
props.extend(overrides.iter().map(|(k, v)| (k.clone(), v.clone())));
26+
props.extend(overrides);
2727
props
2828
}
2929

@@ -37,7 +37,7 @@ mod tests {
3737
// networkaddress.cache.ttl=30
3838
#[test]
3939
fn defaults_match_snapshot() {
40-
let props = build(&BTreeMap::new());
40+
let props = build(BTreeMap::new());
4141
assert_eq!(props["networkaddress.cache.ttl"], "30".to_string());
4242
assert_eq!(props["networkaddress.cache.negative.ttl"], "0".to_string());
4343
assert_eq!(props.len(), 2);
@@ -46,7 +46,7 @@ mod tests {
4646
#[test]
4747
fn override_wins() {
4848
let ov = BTreeMap::from([("networkaddress.cache.ttl".to_string(), "60".to_string())]);
49-
let props = build(&ov);
49+
let props = build(ov);
5050
assert_eq!(props["networkaddress.cache.ttl"], "60".to_string());
5151
}
5252
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ pub fn build_rolegroup_config_map(
314314
}
315315
let security_overrides =
316316
key_value_overrides(&rg.config_overrides, ConfigFileName::SecurityProperties);
317-
security_config.extend(security_properties::build(&security_overrides));
317+
security_config.extend(security_properties::build(security_overrides));
318318

319319
cm_conf_data.insert(
320320
ConfigFileName::SecurityProperties.to_string(),

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ const AUTH_TRUST_STORE_PASSWORD: &str = "druid.auth.basic.ssl.trustStorePassword
8787
const TRUST_STORE_FILE: &str = "truststore.p12";
8888
const KEY_STORE_FILE: &str = "keystore.p12";
8989

90+
// The layer-4 protocol used by all of Druid's exposed ports.
91+
const TCP_PROTOCOL: &str = "TCP";
92+
9093
// directories
9194
const STACKABLE_MOUNT_TLS_DIR: &str = "/stackable/mount_tls";
9295

@@ -95,48 +98,43 @@ stackable_operator::constant!(TLS_VOLUME_NAME: VolumeName = "tls");
9598
stackable_operator::constant!(TLS_MOUNT_VOLUME_NAME: VolumeName = "tls-mount");
9699

97100
pub fn container_ports(tls: &DruidTlsSecurity, role: &DruidRole) -> Vec<ContainerPort> {
98-
exposed_ports(tls, role)
99-
.into_iter()
100-
.map(|(name, val)| ContainerPort {
101-
name: Some(name),
102-
container_port: val.into(),
103-
protocol: Some("TCP".to_string()),
104-
..ContainerPort::default()
105-
})
106-
.collect()
101+
let (name, port) = exposed_port(tls, role);
102+
vec![ContainerPort {
103+
name: Some(name.to_string()),
104+
container_port: port.into(),
105+
protocol: Some(TCP_PROTOCOL.to_string()),
106+
..ContainerPort::default()
107+
}]
107108
}
108109

109110
pub fn service_ports(tls: &DruidTlsSecurity, role: &DruidRole) -> Vec<ServicePort> {
110-
exposed_ports(tls, role)
111-
.into_iter()
112-
.map(|(name, val)| ServicePort {
113-
name: Some(name),
114-
port: val.into(),
115-
protocol: Some("TCP".to_string()),
116-
..ServicePort::default()
117-
})
118-
.collect()
111+
let (name, port) = exposed_port(tls, role);
112+
vec![ServicePort {
113+
name: Some(name.to_string()),
114+
port: port.into(),
115+
protocol: Some(TCP_PROTOCOL.to_string()),
116+
..ServicePort::default()
117+
}]
119118
}
120119

121120
pub fn listener_ports(
122121
tls: &DruidTlsSecurity,
123122
role: &DruidRole,
124123
) -> Vec<listener::v1alpha1::ListenerPort> {
125-
exposed_ports(tls, role)
126-
.into_iter()
127-
.map(|(name, val)| listener::v1alpha1::ListenerPort {
128-
name,
129-
port: val.into(),
130-
protocol: Some("TCP".to_string()),
131-
})
132-
.collect()
124+
let (name, port) = exposed_port(tls, role);
125+
vec![listener::v1alpha1::ListenerPort {
126+
name: name.to_string(),
127+
port: port.into(),
128+
protocol: Some(TCP_PROTOCOL.to_string()),
129+
}]
133130
}
134131

135-
fn exposed_ports(tls: &DruidTlsSecurity, role: &DruidRole) -> Vec<(String, Port)> {
132+
/// The single port (TLS or plaintext, depending on the TLS decision) Druid exposes for the role.
133+
fn exposed_port(tls: &DruidTlsSecurity, role: &DruidRole) -> (&'static str, Port) {
136134
if tls.tls_enabled() {
137-
vec![(TLS_PORT_NAME.to_string(), role.get_https_port())]
135+
(TLS_PORT_NAME, role.get_https_port())
138136
} else {
139-
vec![(PLAINTEXT_PORT_NAME.to_string(), role.get_http_port())]
137+
(PLAINTEXT_PORT_NAME, role.get_http_port())
140138
}
141139
}
142140

0 commit comments

Comments
 (0)