Skip to content

refactor!: Change PodBuilder::termination_grace_period to take Duration struct #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ All notable changes to this project will be documented in this file.
### Changed

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

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

## [0.54.0] - 2023-10-10

Expand Down
52 changes: 44 additions & 8 deletions src/builder/pod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod security;
pub mod volume;

use std::collections::BTreeMap;
use std::num::TryFromIntError;

use crate::builder::meta::ObjectMetaBuilder;
use crate::commons::affinity::StackableAffinity;
Expand All @@ -12,7 +13,8 @@ use crate::commons::resources::{
ComputeResource, ResourceRequirementsExt, ResourceRequirementsType, LIMIT_REQUEST_RATIO_CPU,
LIMIT_REQUEST_RATIO_MEMORY,
};
use crate::error::{Error, OperatorResult};
use crate::duration::Duration;
use crate::error::{self, OperatorResult};

use super::{ListenerOperatorVolumeSourceBuilder, ListenerReference, VolumeBuilder};
use k8s_openapi::{
Expand All @@ -25,8 +27,18 @@ use k8s_openapi::{
};
use tracing::warn;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("termination grace period is too long (got {duration}, maximum allowed is {max})", max = Duration::from_secs(i64::MAX as u64))]
TerminationGracePeriodTooLong {
source: TryFromIntError,
duration: Duration,
},
}
pub type Result<T> = std::result::Result<T, Error>;

/// A builder to build [`Pod`] or [`PodTemplateSpec`] objects.
#[derive(Clone, Default)]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct PodBuilder {
containers: Vec<Container>,
host_network: Option<bool>,
Expand Down Expand Up @@ -452,19 +464,27 @@ impl PodBuilder {
self
}

pub fn termination_grace_period_seconds(
pub fn termination_grace_period(
&mut self,
termination_grace_period_seconds: i64,
) -> &mut Self {
termination_grace_period: &Duration,
) -> Result<&mut Self> {
let termination_grace_period_seconds = termination_grace_period
.as_secs()
.try_into()
.map_err(|err| Error::TerminationGracePeriodTooLong {
source: err,
duration: *termination_grace_period,
})?;

self.termination_grace_period_seconds = Some(termination_grace_period_seconds);
self
Ok(self)
}

/// Consumes the Builder and returns a constructed [`Pod`]
pub fn build(&self) -> OperatorResult<Pod> {
Ok(Pod {
metadata: match self.metadata {
None => return Err(Error::MissingObjectKey { key: "metadata" }),
None => return Err(error::Error::MissingObjectKey { key: "metadata" }),
Some(ref metadata) => metadata.clone(),
},
spec: Some(self.build_spec()),
Expand Down Expand Up @@ -637,7 +657,8 @@ mod tests {
.with_config_map("configmap")
.build(),
)
.termination_grace_period_seconds(42)
.termination_grace_period(&Duration::from_secs(42))
.unwrap()
.build()
.unwrap();

Expand Down Expand Up @@ -691,4 +712,19 @@ mod tests {
.unwrap();
assert_eq!(pod.spec.unwrap().restart_policy.unwrap(), "Always");
}

#[test]
fn test_pod_builder_too_long_termination_grace_period() {
let too_long_duration = Duration::from_secs(i64::MAX as u64 + 1);
let mut pod_builder = PodBuilder::new();

let result = pod_builder.termination_grace_period(&too_long_duration);
assert!(matches!(
result,
Err(Error::TerminationGracePeriodTooLong {
source: TryFromIntError { .. },
duration,
}) if duration == too_long_duration
));
}
}