Skip to content

Commit cc6f5ae

Browse files
committed
error handling
1 parent 8e773ee commit cc6f5ae

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

crates/stackable-operator/src/builder/pod/container.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use k8s_openapi::api::core::v1::{
99
LifecycleHandler, ObjectFieldSelector, Probe, ResourceRequirements, SecretKeySelector,
1010
SecurityContext, VolumeMount,
1111
};
12-
use snafu::{ensure, ResultExt as _, Snafu};
12+
use snafu::{ResultExt as _, Snafu};
13+
use tracing::instrument;
1314

1415
use crate::{
1516
commons::product_image_selection::ResolvedProductImage,
@@ -27,14 +28,11 @@ pub enum Error {
2728
},
2829

2930
#[snafu(display(
30-
"The volumeMount is clashing with an already existing volumeMount with the same mountPath but a different content. \
31-
The clashing mountPath is {clashing_mount_path:?}, \
32-
the existing mount's volume name is {existing_volume_name:?} \
33-
and the new mount's volume name is {new_volume_name:?}. \
34-
Please have a look at the log/traces for details"
31+
"Colliding mountPath {mount_path:?} in volumeMounts with different content. \
32+
Existing volume name {existing_volume_name:?}, new volume name {new_volume_name:?}"
3533
))]
36-
ClashingMountPath {
37-
clashing_mount_path: String,
34+
MountPathCollision {
35+
mount_path: String,
3836
existing_volume_name: String,
3937
new_volume_name: String,
4038
},
@@ -219,25 +217,24 @@ impl ContainerBuilder {
219217
/// Historically this function unconditionally added volumeMounts, which resulted in invalid
220218
/// [`k8s_openapi::api::core::v1::PodSpec`]s, as volumeMounts where added multiple times - think of Trino using the same [`crate::commons::s3::S3Connection`]
221219
/// two times, resulting in e.g. the s3 credentials being mounted twice as the same volumeMount.
220+
#[instrument(skip(self))]
222221
fn add_volume_mount_impl(&mut self, volume_mount: VolumeMount) -> Result<&mut Self> {
223222
if let Some(existing_volume_mount) = self.volume_mounts.get(&volume_mount.mount_path) {
224-
// We don't want to include the details in the error message, but instead trace them
225223
if existing_volume_mount != &volume_mount {
224+
let colliding_mount_path = &volume_mount.mount_path;
225+
// We don't want to include the details in the error message, but instead trace them
226226
tracing::error!(
227-
clashing_mount_path = &volume_mount.mount_path,
227+
colliding_mount_path,
228228
?existing_volume_mount,
229-
new_volume_mount = ?volume_mount,
230-
"The volumeMount is clashing with an already existing volumeMount with the same mountPath but a different content"
229+
"Colliding mountPath {colliding_mount_path:?} in volumeMounts with different content"
231230
);
232231
}
233-
ensure!(
234-
existing_volume_mount == &volume_mount,
235-
ClashingMountPathSnafu {
236-
clashing_mount_path: volume_mount.mount_path,
237-
existing_volume_name: existing_volume_mount.name.clone(),
238-
new_volume_name: volume_mount.name,
239-
}
240-
);
232+
MountPathCollisionSnafu {
233+
mount_path: &volume_mount.mount_path,
234+
existing_volume_name: &existing_volume_mount.name,
235+
new_volume_name: &volume_mount.name,
236+
}
237+
.fail()?;
241238
} else {
242239
self.volume_mounts
243240
.insert(volume_mount.mount_path.clone(), volume_mount);

crates/stackable-operator/src/builder/pod/mod.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use k8s_openapi::{
99
},
1010
apimachinery::pkg::{api::resource::Quantity, apis::meta::v1::ObjectMeta},
1111
};
12-
use snafu::{ensure, OptionExt, ResultExt, Snafu};
13-
use tracing::warn;
12+
use snafu::{OptionExt, ResultExt, Snafu};
13+
use tracing::{instrument, warn};
1414

1515
use crate::kvp::Labels;
1616
use crate::{
@@ -52,9 +52,8 @@ pub enum Error {
5252
#[snafu(display("object is missing key {key:?}"))]
5353
MissingObjectKey { key: &'static str },
5454

55-
#[snafu(display("The volume {clashing_volume_name:?} clashes with an existing volume of the same name but with different content. \
56-
Please have a look at the log/traces for details."))]
57-
ClashingVolumeName { clashing_volume_name: String },
55+
#[snafu(display("Colliding volume name {volume_name:?} in volumes with different content"))]
56+
VolumeNameCollision { volume_name: String },
5857
}
5958

6059
/// A builder to build [`Pod`] or [`PodTemplateSpec`] objects.
@@ -286,24 +285,23 @@ impl PodBuilder {
286285
/// Historically this function unconditionally added volumes, which resulted in invalid [`PodSpec`]s, as volumes
287286
/// where added multiple times - think of Trino using the same [`crate::commons::s3::S3Connection`] two times,
288287
/// resulting in e.g. the s3 credentials being mounted twice as the sake volume.
288+
#[instrument(skip(self))]
289289
pub fn add_volume(&mut self, volume: Volume) -> Result<&mut Self> {
290290
if let Some(existing_volume) = self.volumes.get(&volume.name) {
291-
// We don't want to include the details in the error message, but instead trace them
292291
if existing_volume != &volume {
293-
let clashing_name = &volume.name;
292+
let colliding_name = &volume.name;
293+
// We don't want to include the details in the error message, but instead trace them
294294
tracing::error!(
295-
clashing_name,
295+
colliding_name,
296296
?existing_volume,
297-
new_volume = ?volume,
298-
"The volume {clashing_name:?} clashes with an existing volume of the same name but with different content",
297+
"Colliding volume name {colliding_name:?} in volumes with different content"
299298
);
300-
}
301-
ensure!(
302-
existing_volume == &volume,
303-
ClashingVolumeNameSnafu {
304-
clashing_volume_name: volume.name.clone(),
299+
300+
VolumeNameCollisionSnafu {
301+
volume_name: &volume.name,
305302
}
306-
);
303+
.fail()?;
304+
}
307305
} else {
308306
self.volumes.insert(volume.name.clone(), volume);
309307
}

0 commit comments

Comments
 (0)