Skip to content

Commit ce93c80

Browse files
committed
refactor: move to v2 pdb and clusteresources
1 parent b5cb26c commit ce93c80

4 files changed

Lines changed: 83 additions & 59 deletions

File tree

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
1-
use std::cmp::max;
1+
use std::{cmp::max, str::FromStr};
22

3-
use snafu::{ResultExt, Snafu};
43
use stackable_operator::{
5-
builder::pdb::PodDisruptionBudgetBuilder, commons::pdb::PdbConfig,
4+
commons::pdb::PdbConfig,
65
k8s_openapi::api::policy::v1::PodDisruptionBudget,
6+
v2::{builder::pdb::pod_disruption_budget_builder_with_role, types::operator::RoleName},
77
};
88

99
use crate::{
10-
crd::{APP_NAME, TrinoRole, v1alpha1},
11-
trino_controller::{CONTROLLER_NAME, OPERATOR_NAME},
10+
controller::{ValidatedCluster, controller_name, operator_name, product_name},
11+
crd::TrinoRole,
1212
};
1313

14-
#[derive(Snafu, Debug)]
15-
pub enum Error {
16-
#[snafu(display("cannot create PodDisruptionBudget for role [{role}]"))]
17-
CreatePdb {
18-
source: stackable_operator::builder::pdb::Error,
19-
role: String,
20-
},
21-
}
22-
2314
/// Builds the [`PodDisruptionBudget`] for the given `role`, or `None` if PDBs are disabled.
2415
///
2516
/// The reconciler applies the returned object; this function does not touch the cluster.
2617
pub fn build_pdb(
2718
pdb: &PdbConfig,
28-
trino: &v1alpha1::TrinoCluster,
19+
cluster: &ValidatedCluster,
2920
role: &TrinoRole,
30-
) -> Result<Option<PodDisruptionBudget>, Error> {
21+
) -> Option<PodDisruptionBudget> {
3122
if !pdb.enabled {
32-
return Ok(None);
23+
return None;
3324
}
3425
let max_unavailable = pdb.max_unavailable.unwrap_or(match role {
3526
TrinoRole::Coordinator => max_unavailable_coordinators(),
36-
TrinoRole::Worker => max_unavailable_workers(trino.num_workers()),
27+
TrinoRole::Worker => max_unavailable_workers(worker_count(cluster)),
3728
});
38-
let pdb = PodDisruptionBudgetBuilder::new_with_role(
39-
trino,
40-
APP_NAME,
41-
&role.to_string(),
42-
OPERATOR_NAME,
43-
CONTROLLER_NAME,
29+
let role_name =
30+
RoleName::from_str(&role.to_string()).expect("a TrinoRole is a valid RFC 1123 role name");
31+
let pdb = pod_disruption_budget_builder_with_role(
32+
cluster,
33+
&product_name(),
34+
&role_name,
35+
&operator_name(),
36+
&controller_name(),
4437
)
45-
.with_context(|_| CreatePdbSnafu {
46-
role: role.to_string(),
47-
})?
4838
.with_max_unavailable(max_unavailable)
4939
.build();
5040

51-
Ok(Some(pdb))
41+
Some(pdb)
42+
}
43+
44+
/// Total number of worker replicas across all worker role groups.
45+
fn worker_count(cluster: &ValidatedCluster) -> u16 {
46+
cluster
47+
.role_group_configs
48+
.get(&TrinoRole::Worker)
49+
.into_iter()
50+
.flat_map(|groups| groups.values())
51+
.map(|rg| rg.replicas)
52+
.sum()
5253
}
5354

5455
fn max_unavailable_coordinators() -> u16 {

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ use stackable_operator::{
1212
kube::{Resource, api::ObjectMeta},
1313
shared::time::Duration,
1414
v2::{
15+
HasName, HasUid, NameIsValidLabelValue,
1516
role_group_utils::ResourceNames,
1617
types::{
1718
kubernetes::{NamespaceName, Uid},
18-
operator::{ClusterName, RoleGroupName as RoleGroupNameV2, RoleName},
19+
operator::{
20+
ClusterName, ControllerName, OperatorName, ProductName,
21+
RoleGroupName as RoleGroupNameV2, RoleName,
22+
},
1923
},
2024
},
2125
};
@@ -29,9 +33,10 @@ use crate::{
2933
fault_tolerant_execution::ResolvedFaultTolerantExecutionConfig,
3034
},
3135
crd::{
32-
HTTP_PORT, HTTP_PORT_NAME, HTTPS_PORT, HTTPS_PORT_NAME, TrinoRole, discovery::TrinoPodRef,
33-
v1alpha1,
36+
APP_NAME, HTTP_PORT, HTTP_PORT_NAME, HTTPS_PORT, HTTPS_PORT_NAME, TrinoRole,
37+
discovery::TrinoPodRef, v1alpha1,
3438
},
39+
trino_controller::{CONTROLLER_NAME, OPERATOR_NAME},
3540
};
3641

3742
pub(crate) mod build;
@@ -204,6 +209,39 @@ impl Resource for ValidatedCluster {
204209
}
205210
}
206211

212+
impl HasName for ValidatedCluster {
213+
fn to_name(&self) -> String {
214+
self.name.to_string()
215+
}
216+
}
217+
218+
impl HasUid for ValidatedCluster {
219+
fn to_uid(&self) -> Uid {
220+
self.uid.clone()
221+
}
222+
}
223+
224+
impl NameIsValidLabelValue for ValidatedCluster {
225+
fn to_label_value(&self) -> String {
226+
self.name.to_label_value()
227+
}
228+
}
229+
230+
/// The product name (`trino`) as a type-safe label value.
231+
pub(crate) fn product_name() -> ProductName {
232+
ProductName::from_str(APP_NAME).expect("'trino' is a valid product name")
233+
}
234+
235+
/// The operator name as a type-safe label value.
236+
pub(crate) fn operator_name() -> OperatorName {
237+
OperatorName::from_str(OPERATOR_NAME).expect("the operator name is a valid label value")
238+
}
239+
240+
/// The controller name as a type-safe label value.
241+
pub(crate) fn controller_name() -> ControllerName {
242+
ControllerName::from_str(CONTROLLER_NAME).expect("the controller name is a valid label value")
243+
}
244+
207245
/// A minimal, valid TrinoCluster spec shared across unit tests.
208246
#[cfg(test)]
209247
pub(crate) const MINIMAL_TRINO_YAML: &str = r#"

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,15 +546,6 @@ impl v1alpha1::TrinoCluster {
546546
}
547547
}
548548

549-
pub fn num_workers(&self) -> u16 {
550-
self.spec
551-
.workers
552-
.iter()
553-
.flat_map(|w| w.role_groups.values())
554-
.map(|rg| rg.replicas.unwrap_or(1))
555-
.sum()
556-
}
557-
558549
/// List all coordinator pods expected to form the cluster
559550
///
560551
/// We try to predict the pods here rather than looking at the current cluster state in order to

rust/operator-binary/src/trino_controller.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use const_format::concatcp;
55
use snafu::{ResultExt, Snafu};
66
use stackable_operator::{
77
cli::OperatorEnvironmentOptions,
8-
cluster_resources::{ClusterResourceApplyStrategy, ClusterResources},
8+
cluster_resources::ClusterResourceApplyStrategy,
99
commons::{random_secret_creation, rbac::build_rbac_resources},
1010
kube::{
11-
Resource, ResourceExt,
11+
ResourceExt,
1212
core::{DeserializeGuard, error_boundary},
1313
runtime::controller::Action,
1414
},
@@ -21,6 +21,7 @@ use stackable_operator::{
2121
compute_conditions, operations::ClusterOperationsConditionBuilder,
2222
statefulset::StatefulSetConditionBuilder,
2323
},
24+
v2::cluster_resources::cluster_resources_new,
2425
};
2526
use strum::{EnumDiscriminants, IntoStaticStr};
2627

@@ -35,7 +36,7 @@ use crate::{
3536
headless_service_ports,
3637
},
3738
},
38-
dereference, validate,
39+
controller_name, dereference, operator_name, product_name, validate,
3940
},
4041
crd::{APP_NAME, ENV_INTERNAL_SECRET, ENV_SPOOLING_SECRET, v1alpha1},
4142
};
@@ -63,11 +64,6 @@ pub(crate) const CONTAINER_IMAGE_BASE_NAME: &str = "trino";
6364
#[strum_discriminants(derive(IntoStaticStr))]
6465
#[allow(clippy::enum_variant_names)]
6566
pub enum Error {
66-
#[snafu(display("failed to create cluster resources"))]
67-
CreateClusterResources {
68-
source: stackable_operator::cluster_resources::Error,
69-
},
70-
7167
#[snafu(display("failed to delete orphaned resources"))]
7268
DeleteOrphanedResources {
7369
source: stackable_operator::cluster_resources::Error,
@@ -123,9 +119,6 @@ pub enum Error {
123119
source: stackable_operator::commons::rbac::Error,
124120
},
125121

126-
#[snafu(display("failed to create PodDisruptionBudget"))]
127-
FailedToCreatePdb { source: build::resource::pdb::Error },
128-
129122
#[snafu(display("failed to apply PodDisruptionBudget"))]
130123
ApplyPdb {
131124
source: stackable_operator::cluster_resources::Error,
@@ -211,15 +204,16 @@ pub async fn reconcile_trino(
211204
"Validated TrinoCluster"
212205
);
213206

214-
let mut cluster_resources = ClusterResources::new(
215-
APP_NAME,
216-
OPERATOR_NAME,
217-
CONTROLLER_NAME,
218-
&trino.object_ref(&()),
207+
let mut cluster_resources = cluster_resources_new(
208+
&product_name(),
209+
&operator_name(),
210+
&controller_name(),
211+
&validated_cluster.name,
212+
&validated_cluster.namespace,
213+
&validated_cluster.uid,
219214
ClusterResourceApplyStrategy::from(&trino.spec.cluster_operation),
220215
&trino.spec.object_overrides,
221-
)
222-
.context(CreateClusterResourcesSnafu)?;
216+
);
223217

224218
let (rbac_sa, rbac_rolebinding) = build_rbac_resources(
225219
trino,
@@ -407,7 +401,7 @@ pub async fn reconcile_trino(
407401
if let Some(GenericRoleConfig {
408402
pod_disruption_budget: pdb,
409403
}) = role_config
410-
&& let Some(pdb) = build_pdb(pdb, trino, trino_role).context(FailedToCreatePdbSnafu)?
404+
&& let Some(pdb) = build_pdb(pdb, &validated_cluster, trino_role)
411405
{
412406
cluster_resources
413407
.add(client, pdb)

0 commit comments

Comments
 (0)