Skip to content

Commit ba1b2e2

Browse files
sbernauernightkr
andauthored
refactor!: Change PodBuilder::termination_grace_period to take Duration struct (#672)
* reafctor!: Change PodBuilder::termination_grace_period to take Duration struct * changelog * take by reference * Update CHANGELOG.md Co-authored-by: Natalie <[email protected]> * rework error handling * Update src/builder/pod/mod.rs Co-authored-by: Natalie <[email protected]> * Update src/builder/pod/mod.rs Co-authored-by: Natalie <[email protected]> * rename error to TerminationGracePeriodTooLong --------- Co-authored-by: Natalie <[email protected]>
1 parent 476ea94 commit ba1b2e2

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ All notable changes to this project will be documented in this file.
2727
### Changed
2828

2929
- Convert the format of the Vector configuration from TOML to YAML ([#670]).
30+
- BREAKING: Rename `PodBuilder::termination_grace_period_seconds` to `termination_grace_period`, and change it to take `Duration` struct ([#672]).
3031

3132
[#670]: https://github.com/stackabletech/operator-rs/pull/670
33+
[#672]: https://github.com/stackabletech/operator-rs/pull/672
3234

3335
## [0.54.0] - 2023-10-10
3436

src/builder/pod/mod.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod security;
44
pub mod volume;
55

66
use std::collections::BTreeMap;
7+
use std::num::TryFromIntError;
78

89
use crate::builder::meta::ObjectMetaBuilder;
910
use crate::commons::affinity::StackableAffinity;
@@ -12,7 +13,8 @@ use crate::commons::resources::{
1213
ComputeResource, ResourceRequirementsExt, ResourceRequirementsType, LIMIT_REQUEST_RATIO_CPU,
1314
LIMIT_REQUEST_RATIO_MEMORY,
1415
};
15-
use crate::error::{Error, OperatorResult};
16+
use crate::duration::Duration;
17+
use crate::error::{self, OperatorResult};
1618

1719
use super::{ListenerOperatorVolumeSourceBuilder, ListenerReference, VolumeBuilder};
1820
use k8s_openapi::{
@@ -25,8 +27,18 @@ use k8s_openapi::{
2527
};
2628
use tracing::warn;
2729

30+
#[derive(Debug, thiserror::Error)]
31+
pub enum Error {
32+
#[error("termination grace period is too long (got {duration}, maximum allowed is {max})", max = Duration::from_secs(i64::MAX as u64))]
33+
TerminationGracePeriodTooLong {
34+
source: TryFromIntError,
35+
duration: Duration,
36+
},
37+
}
38+
pub type Result<T> = std::result::Result<T, Error>;
39+
2840
/// A builder to build [`Pod`] or [`PodTemplateSpec`] objects.
29-
#[derive(Clone, Default)]
41+
#[derive(Clone, Debug, Default, PartialEq)]
3042
pub struct PodBuilder {
3143
containers: Vec<Container>,
3244
host_network: Option<bool>,
@@ -452,19 +464,27 @@ impl PodBuilder {
452464
self
453465
}
454466

455-
pub fn termination_grace_period_seconds(
467+
pub fn termination_grace_period(
456468
&mut self,
457-
termination_grace_period_seconds: i64,
458-
) -> &mut Self {
469+
termination_grace_period: &Duration,
470+
) -> Result<&mut Self> {
471+
let termination_grace_period_seconds = termination_grace_period
472+
.as_secs()
473+
.try_into()
474+
.map_err(|err| Error::TerminationGracePeriodTooLong {
475+
source: err,
476+
duration: *termination_grace_period,
477+
})?;
478+
459479
self.termination_grace_period_seconds = Some(termination_grace_period_seconds);
460-
self
480+
Ok(self)
461481
}
462482

463483
/// Consumes the Builder and returns a constructed [`Pod`]
464484
pub fn build(&self) -> OperatorResult<Pod> {
465485
Ok(Pod {
466486
metadata: match self.metadata {
467-
None => return Err(Error::MissingObjectKey { key: "metadata" }),
487+
None => return Err(error::Error::MissingObjectKey { key: "metadata" }),
468488
Some(ref metadata) => metadata.clone(),
469489
},
470490
spec: Some(self.build_spec()),
@@ -637,7 +657,8 @@ mod tests {
637657
.with_config_map("configmap")
638658
.build(),
639659
)
640-
.termination_grace_period_seconds(42)
660+
.termination_grace_period(&Duration::from_secs(42))
661+
.unwrap()
641662
.build()
642663
.unwrap();
643664

@@ -691,4 +712,19 @@ mod tests {
691712
.unwrap();
692713
assert_eq!(pod.spec.unwrap().restart_policy.unwrap(), "Always");
693714
}
715+
716+
#[test]
717+
fn test_pod_builder_too_long_termination_grace_period() {
718+
let too_long_duration = Duration::from_secs(i64::MAX as u64 + 1);
719+
let mut pod_builder = PodBuilder::new();
720+
721+
let result = pod_builder.termination_grace_period(&too_long_duration);
722+
assert!(matches!(
723+
result,
724+
Err(Error::TerminationGracePeriodTooLong {
725+
source: TryFromIntError { .. },
726+
duration,
727+
}) if duration == too_long_duration
728+
));
729+
}
694730
}

0 commit comments

Comments
 (0)