Skip to content

Commit 8a5057d

Browse files
committed
refactor: consolidate error handling
1 parent c2ce137 commit 8a5057d

4 files changed

Lines changed: 82 additions & 62 deletions

File tree

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

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::sync::Arc;
22

33
use snafu::{ResultExt, Snafu};
44
use stackable_operator::{
5-
builder::{self},
65
cluster_resources::ClusterResourceApplyStrategy,
76
commons::rbac::build_rbac_resources,
87
crd::listener,
@@ -18,16 +17,13 @@ use stackable_operator::{
1817
},
1918
logging::controller::ReconcilerError,
2019
shared::time::Duration,
21-
v2::{cluster_resources::cluster_resources_new, config_file_writer::PropertiesWriterError},
20+
v2::cluster_resources::cluster_resources_new,
2221
};
2322
use strum::{EnumDiscriminants, IntoStaticStr};
2423

2524
use crate::{
2625
Ctx,
27-
crd::{
28-
constants::{HISTORY_APP_NAME, JVM_SECURITY_PROPERTIES_FILE},
29-
history::v1alpha1,
30-
},
26+
crd::{constants::HISTORY_APP_NAME, history::v1alpha1},
3127
};
3228

3329
pub mod build;
@@ -43,14 +39,8 @@ pub enum Error {
4339
source: stackable_operator::commons::rbac::Error,
4440
},
4541

46-
#[snafu(display("missing secret lifetime"))]
47-
MissingSecretLifetime,
48-
49-
#[snafu(display("invalid config map {name}"))]
50-
InvalidConfigMap {
51-
source: stackable_operator::builder::configmap::Error,
52-
name: String,
53-
},
42+
#[snafu(display("failed to build SparkHistoryServer resources"))]
43+
BuildSparkHistoryServer { source: build::Error },
5444

5545
#[snafu(display("failed to apply Kubernetes resource"))]
5646
ApplyResource {
@@ -78,41 +68,18 @@ pub enum Error {
7868
source: stackable_operator::cluster_resources::Error,
7969
},
8070

81-
#[snafu(display(
82-
"History server : failed to serialize [{JVM_SECURITY_PROPERTIES_FILE}] for group {}",
83-
rolegroup
84-
))]
85-
JvmSecurityProperties {
86-
source: PropertiesWriterError,
87-
rolegroup: String,
88-
},
89-
9071
#[snafu(display("failed to get required Labels"))]
9172
GetRequiredLabels {
9273
source:
9374
stackable_operator::kvp::KeyValuePairError<stackable_operator::kvp::LabelValueError>,
9475
},
9576

96-
#[snafu(display("failed to create the log dir volumes specification"))]
97-
CreateLogDirVolumesSpec { source: crate::crd::logdir::Error },
98-
99-
#[snafu(display("failed to add needed volume"))]
100-
AddVolume { source: builder::pod::Error },
101-
102-
#[snafu(display("failed to add needed volumeMount"))]
103-
AddVolumeMount {
104-
source: builder::pod::container::Error,
105-
},
106-
10777
#[snafu(display("SparkHistoryServer object is invalid"))]
10878
InvalidSparkHistoryServer {
10979
// boxed because otherwise Clippy warns about a large enum variant
11080
#[snafu(source(from(error_boundary::InvalidObject, Box::new)))]
11181
source: Box<error_boundary::InvalidObject>,
11282
},
113-
114-
#[snafu(display("failed to serialize Spark default properties"))]
115-
InvalidSparkDefaults { source: PropertiesWriterError },
11683
}
11784

11885
impl ReconcilerError for Error {
@@ -180,7 +147,8 @@ pub async fn reconcile(
180147
)
181148
.context(BuildRbacResourcesSnafu)?;
182149

183-
let resources = build::build(&validated, service_account, role_binding)?;
150+
let resources = build::build(&validated, service_account, role_binding)
151+
.context(BuildSparkHistoryServerSnafu)?;
184152

185153
// Apply order: ServiceAccount and RoleBinding first, then the ConfigMaps, metrics Services,
186154
// Listener and PodDisruptionBudget, and finally the StatefulSets (they mount the ConfigMaps

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,54 @@
11
pub mod resource;
22

3+
use snafu::{ResultExt, Snafu};
34
use stackable_operator::k8s_openapi::api::{core::v1::ServiceAccount, rbac::v1::RoleBinding};
45

56
use crate::{
67
crd::constants::HISTORY_ROLE_NAME,
78
history::controller::{
8-
Error, SparkHistoryResources,
9+
SparkHistoryResources,
910
build::resource::{
10-
config_map::build_config_map, listener::build_group_listener, pdb::build_pdb,
11-
service::build_rolegroup_metrics_service, statefulset::build_stateful_set,
11+
config_map::{self, build_config_map},
12+
listener::build_group_listener,
13+
pdb::build_pdb,
14+
service::build_rolegroup_metrics_service,
15+
statefulset::{self, build_stateful_set},
1216
},
1317
validate::ValidatedSparkHistoryServer,
1418
},
1519
};
1620

21+
#[derive(Snafu, Debug)]
22+
pub enum Error {
23+
#[snafu(display("failed to build ConfigMap"))]
24+
BuildConfigMap { source: config_map::Error },
25+
26+
#[snafu(display("failed to build StatefulSet"))]
27+
BuildStatefulSet { source: statefulset::Error },
28+
}
29+
30+
type Result<T, E = Error> = std::result::Result<T, E>;
31+
1732
/// Builds every Kubernetes resource for the given validated SparkHistoryServer.
1833
pub fn build(
1934
validated: &ValidatedSparkHistoryServer,
2035
service_account: ServiceAccount,
2136
role_binding: RoleBinding,
22-
) -> Result<SparkHistoryResources, Error> {
37+
) -> Result<SparkHistoryResources> {
2338
let log_dir = &validated.cluster_config.log_dir;
2439

2540
let mut config_maps = vec![];
2641
let mut metrics_services = vec![];
2742
let mut stateful_sets = vec![];
2843

2944
for (role_group_name, rg) in &validated.role_groups {
30-
config_maps.push(build_config_map(validated, role_group_name, rg)?);
45+
config_maps
46+
.push(build_config_map(validated, role_group_name, rg).context(BuildConfigMapSnafu)?);
3147
metrics_services.push(build_rolegroup_metrics_service(validated, role_group_name));
32-
stateful_sets.push(build_stateful_set(
33-
validated,
34-
role_group_name,
35-
rg,
36-
log_dir,
37-
&service_account,
38-
)?);
48+
stateful_sets.push(
49+
build_stateful_set(validated, role_group_name, rg, log_dir, &service_account)
50+
.context(BuildStatefulSetSnafu)?,
51+
);
3952
}
4053

4154
let listener = build_group_listener(

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::BTreeMap;
22

3-
use snafu::ResultExt;
3+
use snafu::{ResultExt, Snafu};
44
use stackable_operator::{
55
builder::{configmap::ConfigMapBuilder, meta::ObjectMetaBuilder},
66
k8s_openapi::api::core::v1::ConfigMap,
@@ -20,19 +20,40 @@ use crate::{
2020
history::SparkHistoryServerContainer,
2121
to_spark_env_sh_string,
2222
},
23-
history::controller::{
24-
Error, InvalidConfigMapSnafu, InvalidSparkDefaultsSnafu, JvmSecurityPropertiesSnafu,
25-
validate::{self, ValidatedHistoryRoleGroup},
26-
},
23+
history::controller::validate::{self, ValidatedHistoryRoleGroup},
2724
product_logging::{self},
2825
};
2926

30-
#[allow(clippy::result_large_err)]
27+
#[derive(Snafu, Debug)]
28+
pub enum Error {
29+
#[snafu(display("invalid config map {name}"))]
30+
InvalidConfigMap {
31+
source: stackable_operator::builder::configmap::Error,
32+
name: String,
33+
},
34+
35+
#[snafu(display(
36+
"History server : failed to serialize [{JVM_SECURITY_PROPERTIES_FILE}] for group {}",
37+
rolegroup
38+
))]
39+
JvmSecurityProperties {
40+
source: stackable_operator::v2::config_file_writer::PropertiesWriterError,
41+
rolegroup: String,
42+
},
43+
44+
#[snafu(display("failed to serialize Spark default properties"))]
45+
InvalidSparkDefaults {
46+
source: stackable_operator::v2::config_file_writer::PropertiesWriterError,
47+
},
48+
}
49+
50+
type Result<T, E = Error> = std::result::Result<T, E>;
51+
3152
pub(crate) fn build_config_map(
3253
validated: &validate::ValidatedSparkHistoryServer,
3354
role_group_name: &RoleGroupName,
3455
rg: &ValidatedHistoryRoleGroup,
35-
) -> Result<ConfigMap, Error> {
56+
) -> Result<ConfigMap> {
3657
let cm_name = validated
3758
.resource_names(role_group_name)
3859
.role_group_config_map()

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::str::FromStr;
22

3-
use snafu::{OptionExt, ResultExt};
3+
use snafu::{OptionExt, ResultExt, Snafu};
44
use stackable_operator::{
55
builder::{
66
meta::ObjectMetaBuilder,
@@ -52,22 +52,40 @@ use crate::{
5252
history::{
5353
config::jvm::construct_history_jvm_args,
5454
controller::{
55-
AddVolumeMountSnafu, AddVolumeSnafu, CreateLogDirVolumesSpecSnafu, Error,
56-
MissingSecretLifetimeSnafu,
5755
build::resource::listener::group_listener_name,
5856
validate::{self, ValidatedHistoryRoleGroup},
5957
},
6058
},
6159
};
6260

63-
#[allow(clippy::result_large_err)]
61+
#[derive(Snafu, Debug)]
62+
pub enum Error {
63+
#[snafu(display("missing secret lifetime"))]
64+
MissingSecretLifetime,
65+
66+
#[snafu(display("failed to create the log dir volumes specification"))]
67+
CreateLogDirVolumesSpec { source: crate::crd::logdir::Error },
68+
69+
#[snafu(display("failed to add needed volume"))]
70+
AddVolume {
71+
source: stackable_operator::builder::pod::Error,
72+
},
73+
74+
#[snafu(display("failed to add needed volumeMount"))]
75+
AddVolumeMount {
76+
source: stackable_operator::builder::pod::container::Error,
77+
},
78+
}
79+
80+
type Result<T, E = Error> = std::result::Result<T, E>;
81+
6482
pub(crate) fn build_stateful_set(
6583
validated: &validate::ValidatedSparkHistoryServer,
6684
role_group_name: &RoleGroupName,
6785
rg: &ValidatedHistoryRoleGroup,
6886
log_dir: &ResolvedLogDir,
6987
serviceaccount: &ServiceAccount,
70-
) -> Result<StatefulSet, Error> {
88+
) -> Result<StatefulSet> {
7189
let resolved_product_image = &validated.resolved_product_image;
7290
let resource_names = validated.resource_names(role_group_name);
7391

0 commit comments

Comments
 (0)