Skip to content

Commit 4e8723c

Browse files
committed
refactor: cleanup build_statefulset and tls_volume parameters
1 parent 3032234 commit 4e8723c

7 files changed

Lines changed: 113 additions & 124 deletions

File tree

rust/operator-binary/src/command.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use crate::{
1010
authentication::TrinoAuthenticationConfig,
1111
catalog::config::CatalogConfig,
1212
config::{client_protocol, fault_tolerant_execution},
13-
controller::ValidatedTrinoConfig,
13+
controller::{ValidatedCluster, ValidatedTrinoConfig},
1414
crd::{
1515
CONFIG_DIR_NAME, Container, RW_CONFIG_DIR_NAME, STACKABLE_CLIENT_TLS_DIR,
1616
STACKABLE_INTERNAL_TLS_DIR, STACKABLE_MOUNT_INTERNAL_TLS_DIR,
1717
STACKABLE_MOUNT_SERVER_TLS_DIR, STACKABLE_SERVER_TLS_DIR, STACKABLE_TLS_STORE_PASSWORD,
18-
TrinoRole, v1alpha1,
18+
TrinoRole,
1919
},
2020
trino_controller::{STACKABLE_LOG_CONFIG_DIR, STACKABLE_LOG_DIR},
2121
};
@@ -27,7 +27,7 @@ const EXCHANGE_MANAGER_PROPERTIES: &str = "exchange-manager.properties";
2727
const SPOOLING_MANAGER_PROPERTIES: &str = "spooling-manager.properties";
2828

2929
pub fn container_prepare_args(
30-
trino: &v1alpha1::TrinoCluster,
30+
cluster: &ValidatedCluster,
3131
catalogs: &[CatalogConfig],
3232
merged_config: &ValidatedTrinoConfig,
3333
resolved_fte_config: &Option<fault_tolerant_execution::ResolvedFaultTolerantExecutionConfig>,
@@ -54,15 +54,15 @@ pub fn container_prepare_args(
5454
// S3, LDAP, OIDC, FTE or whatnot.
5555
args.push(format!("cert-tools generate-pkcs12-truststore --pem /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem --out {STACKABLE_CLIENT_TLS_DIR}/truststore.p12 --out-password {STACKABLE_TLS_STORE_PASSWORD}"));
5656

57-
if trino.tls_enabled() {
57+
if cluster.tls_enabled() {
5858
args.push(format!("cp {STACKABLE_MOUNT_SERVER_TLS_DIR}/truststore.p12 {STACKABLE_SERVER_TLS_DIR}/truststore.p12"));
5959
args.push(format!("cp {STACKABLE_MOUNT_SERVER_TLS_DIR}/keystore.p12 {STACKABLE_SERVER_TLS_DIR}/keystore.p12"));
6060
}
6161

62-
if trino.get_internal_tls().is_some() {
62+
if cluster.get_internal_tls().is_some() {
6363
args.push(format!("cp {STACKABLE_MOUNT_INTERNAL_TLS_DIR}/truststore.p12 {STACKABLE_INTERNAL_TLS_DIR}/truststore.p12"));
6464
args.push(format!("cp {STACKABLE_MOUNT_INTERNAL_TLS_DIR}/keystore.p12 {STACKABLE_INTERNAL_TLS_DIR}/keystore.p12"));
65-
if trino.tls_enabled() {
65+
if cluster.tls_enabled() {
6666
args.push(format!("cert-tools generate-pkcs12-truststore --pkcs12 {STACKABLE_MOUNT_SERVER_TLS_DIR}/truststore.p12:{STACKABLE_TLS_STORE_PASSWORD} --pkcs12 {STACKABLE_INTERNAL_TLS_DIR}/truststore.p12:{STACKABLE_TLS_STORE_PASSWORD} --out {STACKABLE_INTERNAL_TLS_DIR}/truststore.p12 --out-password {STACKABLE_TLS_STORE_PASSWORD}"));
6767
}
6868
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ use stackable_operator::{
66
},
77
crd::listener::v1alpha1::{Listener, ListenerPort, ListenerSpec},
88
k8s_openapi::api::core::v1::PersistentVolumeClaim,
9-
kube::ResourceExt,
109
kvp::Labels,
1110
v2::builder::meta::ownerreference_from_resource,
1211
};
1312

14-
use crate::{
15-
controller::ValidatedCluster,
16-
crd::{TrinoRole, v1alpha1},
17-
};
13+
use crate::{controller::ValidatedCluster, crd::TrinoRole};
1814

1915
pub const LISTENER_VOLUME_NAME: &str = "listener";
2016
pub const LISTENER_VOLUME_DIR: &str = "/stackable/listener";
@@ -64,11 +60,11 @@ pub fn build_group_listener_pvc(
6460
/// The name of the group-listener provided for a specific role-group.
6561
/// Coordinator(s) will use this group listener so that only one load balancer
6662
/// is needed (per role group).
67-
pub fn group_listener_name(trino: &v1alpha1::TrinoCluster, role: &TrinoRole) -> Option<String> {
63+
pub fn group_listener_name(cluster: &ValidatedCluster, role: &TrinoRole) -> Option<String> {
6864
match role {
6965
TrinoRole::Coordinator => Some(format!(
7066
"{cluster_name}-{role}",
71-
cluster_name = trino.name_any()
67+
cluster_name = cluster.name
7268
)),
7369
TrinoRole::Worker => None,
7470
}

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

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ use stackable_operator::{
4141
};
4242

4343
use crate::{
44-
authorization::opa::{OPA_TLS_VOLUME_NAME, TrinoOpaConfig},
45-
catalog::config::CatalogConfig,
44+
authorization::opa::OPA_TLS_VOLUME_NAME,
4645
command,
47-
config::{client_protocol, fault_tolerant_execution},
4846
controller::{
4947
TrinoRoleGroupConfig, ValidatedCluster, build,
5048
build::resource::listener::{
@@ -58,7 +56,7 @@ use crate::{
5856
METRICS_PORT_NAME, RW_CONFIG_DIR_NAME, STACKABLE_CLIENT_TLS_DIR,
5957
STACKABLE_INTERNAL_TLS_DIR, STACKABLE_MOUNT_INTERNAL_TLS_DIR,
6058
STACKABLE_MOUNT_SERVER_TLS_DIR, STACKABLE_SERVER_TLS_DIR, STACKABLE_TLS_STORE_PASSWORD,
61-
TrinoRole, v1alpha1,
59+
TrinoRole,
6260
},
6361
trino_controller::{
6462
MAX_PREPARE_LOG_FILE_SIZE, STACKABLE_LOG_CONFIG_DIR, STACKABLE_LOG_DIR,
@@ -77,9 +75,6 @@ pub enum Error {
7775
#[snafu(display("missing secret lifetime"))]
7876
MissingSecretLifetime,
7977

80-
#[snafu(display("internal operator failure: {source}"))]
81-
InternalOperatorFailure { source: crate::crd::Error },
82-
8378
#[snafu(display("illegal container name: [{container_name}]"))]
8479
IllegalContainerName {
8580
source: stackable_operator::builder::pod::container::Error,
@@ -140,7 +135,6 @@ type Result<T, E = Error> = std::result::Result<T, E>;
140135
/// corresponding [`stackable_operator::k8s_openapi::api::core::v1::Service`] (from
141136
/// [`build_rolegroup_headless_service`](super::service::build_rolegroup_headless_service)).
142137
pub fn build_rolegroup_statefulset(
143-
trino: &v1alpha1::TrinoCluster,
144138
cluster: &ValidatedCluster,
145139
trino_role: &TrinoRole,
146140
role_group_name: &str,
@@ -158,13 +152,6 @@ pub fn build_rolegroup_statefulset(
158152
let env_overrides = &role_group_config.env_overrides;
159153
let merged_config = &role_group_config.config;
160154

161-
let role = trino
162-
.role(trino_role)
163-
.context(InternalOperatorFailureSnafu)?;
164-
let rolegroup = trino
165-
.rolegroup(trino_role, role_group_name)
166-
.context(InternalOperatorFailureSnafu)?;
167-
168155
let resource_names = cluster.resource_names(trino_role, role_group_name);
169156
let config_map_name = resource_names.role_group_config_map().to_string();
170157

@@ -187,14 +174,14 @@ pub fn build_rolegroup_statefulset(
187174
// additional authentication env vars
188175
let mut env = trino_authentication_config.env_vars(trino_role, &Container::Trino);
189176

190-
let internal_secret_name = shared_internal_secret_name(trino);
177+
let internal_secret_name = shared_internal_secret_name(&cluster.name);
191178
env.push(env_var_from_secret(
192179
&internal_secret_name,
193180
None,
194181
ENV_INTERNAL_SECRET,
195182
));
196183

197-
let spooling_secret_name = shared_spooling_secret_name(trino);
184+
let spooling_secret_name = shared_spooling_secret_name(&cluster.name);
198185
env.push(env_var_from_secret(
199186
&spooling_secret_name,
200187
None,
@@ -244,16 +231,12 @@ pub fn build_rolegroup_statefulset(
244231

245232
// add volume mounts depending on the client tls, internal tls, catalogs and authentication
246233
tls_volume_mounts(
247-
trino,
234+
cluster,
248235
trino_role,
249236
&mut pod_builder,
250237
&mut cb_prepare,
251238
&mut cb_trino,
252-
catalogs,
253239
&requested_secret_lifetime,
254-
resolved_fte_config,
255-
resolved_spooling_config,
256-
trino_opa_config,
257240
)?;
258241

259242
let mut prepare_args = vec![];
@@ -268,7 +251,7 @@ pub fn build_rolegroup_statefulset(
268251
}
269252

270253
prepare_args.extend(command::container_prepare_args(
271-
trino,
254+
cluster,
272255
catalogs,
273256
merged_config,
274257
resolved_fte_config,
@@ -317,7 +300,7 @@ pub fn build_rolegroup_statefulset(
317300

318301
let mut persistent_volume_claims = vec![];
319302
// Add listener
320-
if let Some(group_listener_name) = group_listener_name(trino, trino_role) {
303+
if let Some(group_listener_name) = group_listener_name(cluster, trino_role) {
321304
cb_trino
322305
.add_volume_mount(LISTENER_VOLUME_NAME, LISTENER_VOLUME_DIR)
323306
.context(AddVolumeMountSnafu)?;
@@ -442,8 +425,10 @@ pub fn build_rolegroup_statefulset(
442425
.security_context(PodSecurityContextBuilder::new().fs_group(1000).build());
443426

444427
let mut pod_template = pod_builder.build_template();
445-
pod_template.merge_from(role.config.pod_overrides.clone());
446-
pod_template.merge_from(rolegroup.config.pod_overrides.clone());
428+
// `pod_overrides` already carries the merged role + role-group overrides (see
429+
// `with_validated_config`), so a single merge here is equivalent to the previous
430+
// role-then-role-group sequence.
431+
pod_template.merge_from(role_group_config.pod_overrides.clone());
447432

448433
let annotations = restarter_ignore_secret_annotations(
449434
trino_authentication_config
@@ -454,7 +439,7 @@ pub fn build_rolegroup_statefulset(
454439

455440
Ok(StatefulSet {
456441
metadata: ObjectMetaBuilder::new()
457-
.name_and_namespace(trino)
442+
.name_and_namespace(cluster)
458443
.name(resource_names.stateful_set_name().to_string())
459444
.ownerreference(ownerreference_from_resource(cluster, None, Some(true)))
460445
.with_labels(cluster.recommended_labels(trino_role, role_group_name))
@@ -463,7 +448,7 @@ pub fn build_rolegroup_statefulset(
463448
.build(),
464449
spec: Some(StatefulSetSpec {
465450
pod_management_policy: Some("Parallel".to_string()),
466-
replicas: rolegroup.replicas.map(i32::from),
451+
replicas: Some(i32::from(role_group_config.replicas)),
467452
selector: LabelSelector {
468453
match_labels: Some(
469454
cluster
@@ -635,20 +620,20 @@ fn create_tls_volume(
635620
.build())
636621
}
637622

638-
#[allow(clippy::too_many_arguments)]
639623
fn tls_volume_mounts(
640-
trino: &v1alpha1::TrinoCluster,
624+
cluster: &ValidatedCluster,
641625
trino_role: &TrinoRole,
642626
pod_builder: &mut PodBuilder,
643627
cb_prepare: &mut ContainerBuilder,
644628
cb_trino: &mut ContainerBuilder,
645-
catalogs: &[CatalogConfig],
646629
requested_secret_lifetime: &Duration,
647-
resolved_fte_config: &Option<fault_tolerant_execution::ResolvedFaultTolerantExecutionConfig>,
648-
resolved_spooling_config: &Option<client_protocol::ResolvedClientProtocolConfig>,
649-
trino_opa_config: &Option<TrinoOpaConfig>,
650630
) -> Result<()> {
651-
if let Some(server_tls) = trino.get_server_tls() {
631+
let catalogs = cluster.cluster_config.catalogs.as_slice();
632+
let resolved_fte_config = &cluster.cluster_config.fault_tolerant_execution;
633+
let resolved_spooling_config = &cluster.cluster_config.client_protocol;
634+
let trino_opa_config = &cluster.cluster_config.authorization;
635+
636+
if let Some(server_tls) = cluster.get_server_tls() {
652637
cb_prepare
653638
.add_volume_mount("server-tls-mount", STACKABLE_MOUNT_SERVER_TLS_DIR)
654639
.context(AddVolumeMountSnafu)?;
@@ -686,7 +671,7 @@ fn tls_volume_mounts(
686671
.add_empty_dir_volume("client-tls", None)
687672
.context(AddVolumeSnafu)?;
688673

689-
if let Some(internal_tls) = trino.get_internal_tls() {
674+
if let Some(internal_tls) = cluster.get_internal_tls() {
690675
cb_prepare
691676
.add_volume_mount("internal-tls-mount", STACKABLE_MOUNT_INTERNAL_TLS_DIR)
692677
.context(AddVolumeMountSnafu)?;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ impl ValidatedCluster {
149149
self.cluster_config.tls.server.is_some()
150150
}
151151

152+
/// The user-provided server TLS SecretClass, if any.
153+
///
154+
/// Mirrors [`v1alpha1::TrinoCluster::get_server_tls`] but reads from the validated config so
155+
/// build steps don't need the raw cluster.
156+
pub fn get_server_tls(&self) -> Option<&str> {
157+
self.cluster_config.tls.server.as_deref()
158+
}
159+
160+
/// The user-provided internal TLS SecretClass, if any.
161+
pub fn get_internal_tls(&self) -> Option<&str> {
162+
self.cluster_config.tls.internal.as_deref()
163+
}
164+
165+
/// Whether client TLS should be set, depending on authentication and server TLS settings.
166+
pub fn tls_enabled(&self) -> bool {
167+
self.cluster_config.authentication_enabled || self.server_tls_enabled()
168+
}
169+
152170
/// The client-facing port Trino exposes: HTTPS when server TLS is enabled, otherwise HTTP.
153171
///
154172
/// Replaces `v1alpha1::TrinoCluster::exposed_port`, derived here from the validated TLS config

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ pub fn validate(
241241
role_group_configs.insert(trino_role, groups);
242242
}
243243

244+
let tls = &trino.spec.cluster_config.tls;
244245
let cluster_config = ValidatedClusterConfig {
245246
tls: ValidatedTls {
246-
server: trino.get_server_tls().map(String::from),
247-
internal: trino.get_internal_tls().map(String::from),
247+
server: tls.server_secret_class.as_ref().map(ToString::to_string),
248+
internal: tls.internal_secret_class.as_ref().map(ToString::to_string),
248249
},
249250
authentication,
250251
authentication_enabled: trino.authentication_enabled(),

0 commit comments

Comments
 (0)