Skip to content

Commit dede2c0

Browse files
committed
refactor: extract update_status step
1 parent b513919 commit dede2c0

3 files changed

Lines changed: 65 additions & 27 deletions

File tree

rust/operator-binary/src/airflow_controller.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@ use stackable_operator::{
1616
},
1717
logging::controller::ReconcilerError,
1818
shared::time::Duration,
19-
status::condition::{
20-
compute_conditions, operations::ClusterOperationsConditionBuilder,
21-
statefulset::StatefulSetConditionBuilder,
22-
},
2319
};
2420
use strum::{EnumDiscriminants, IntoStaticStr};
2521

2622
use crate::{
2723
controller::{
2824
apply::{self, Applier, ensure_random_secrets},
2925
build,
26+
update_status::{self, update_status},
3027
},
31-
crd::{AirflowClusterStatus, OPERATOR_NAME, v1alpha2},
28+
crd::{OPERATOR_NAME, v1alpha2},
3229
};
3330

3431
pub const AIRFLOW_CONTROLLER_NAME: &str = "airflowcluster";
@@ -55,10 +52,8 @@ pub enum Error {
5552
#[snafu(display("failed to ensure the shared random Secrets exist"))]
5653
EnsureSecrets { source: apply::Error },
5754

58-
#[snafu(display("failed to update status"))]
59-
ApplyStatus {
60-
source: stackable_operator::client::Error,
61-
},
55+
#[snafu(display("failed to update the cluster status"))]
56+
UpdateStatus { source: update_status::Error },
6257

6358
#[snafu(display("failed to dereference cluster resources"))]
6459
Dereference {
@@ -102,9 +97,6 @@ pub async fn reconcile_airflow(
10297
.await
10398
.context(DereferenceSnafu)?;
10499

105-
let cluster_operation_cond_builder =
106-
ClusterOperationsConditionBuilder::new(&airflow.spec.cluster_operation);
107-
108100
let validated_cluster = crate::controller::validate::validate_cluster(
109101
airflow,
110102
&ctx.operator_environment.image_repository,
@@ -127,22 +119,9 @@ pub async fn reconcile_airflow(
127119
.await
128120
.context(ApplyResourcesSnafu)?;
129121

130-
let mut ss_cond_builder = StatefulSetConditionBuilder::default();
131-
for statefulset in applied.stateful_sets {
132-
ss_cond_builder.add(statefulset);
133-
}
134-
135-
let status = AirflowClusterStatus {
136-
conditions: compute_conditions(
137-
airflow,
138-
&[&ss_cond_builder, &cluster_operation_cond_builder],
139-
),
140-
};
141-
142-
client
143-
.apply_patch_status(OPERATOR_NAME, airflow, &status)
122+
update_status(client, airflow, &applied)
144123
.await
145-
.context(ApplyStatusSnafu)?;
124+
.context(UpdateStatusSnafu)?;
146125

147126
Ok(Action::await_change())
148127
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use crate::{
6565
pub mod apply;
6666
pub mod build;
6767
pub mod dereference;
68+
pub mod update_status;
6869
pub mod validate;
6970

7071
// Placeholder version label value for resources whose labels must not change after deployment.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//! The update_status step in the AirflowCluster controller.
2+
3+
use snafu::{ResultExt, Snafu};
4+
use stackable_operator::{
5+
client::Client,
6+
status::condition::{
7+
compute_conditions, operations::ClusterOperationsConditionBuilder,
8+
statefulset::StatefulSetConditionBuilder,
9+
},
10+
};
11+
use strum::{EnumDiscriminants, IntoStaticStr};
12+
13+
use crate::{
14+
controller::{Applied, KubernetesResources},
15+
crd::{AirflowClusterStatus, OPERATOR_NAME, v1alpha2},
16+
};
17+
18+
#[derive(Snafu, Debug, EnumDiscriminants)]
19+
#[strum_discriminants(derive(IntoStaticStr))]
20+
pub enum Error {
21+
#[snafu(display("failed to update status"))]
22+
ApplyStatus {
23+
source: stackable_operator::client::Error,
24+
},
25+
}
26+
27+
type Result<T, E = Error> = std::result::Result<T, E>;
28+
29+
/// Computes the cluster status from the applied resources and patches it onto the
30+
/// [`v1alpha2::AirflowCluster`]. Takes [`KubernetesResources<Applied>`] so the type system
31+
/// proves the status derives from applied resources, not merely built ones.
32+
pub async fn update_status(
33+
client: &Client,
34+
airflow: &v1alpha2::AirflowCluster,
35+
applied: &KubernetesResources<Applied>,
36+
) -> Result<()> {
37+
let mut ss_cond_builder = StatefulSetConditionBuilder::default();
38+
for stateful_set in &applied.stateful_sets {
39+
ss_cond_builder.add(stateful_set.clone());
40+
}
41+
42+
let cluster_operation_cond_builder =
43+
ClusterOperationsConditionBuilder::new(&airflow.spec.cluster_operation);
44+
45+
let status = AirflowClusterStatus {
46+
conditions: compute_conditions(
47+
airflow,
48+
&[&ss_cond_builder, &cluster_operation_cond_builder],
49+
),
50+
};
51+
52+
client
53+
.apply_patch_status(OPERATOR_NAME, airflow, &status)
54+
.await
55+
.context(ApplyStatusSnafu)?;
56+
57+
Ok(())
58+
}

0 commit comments

Comments
 (0)