diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index c191ee5e9..9e0ec53b7 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +- Extend `ObjectMetaBuilder` with `finalizers` ([#1094]). + +[#1094]: https://github.com/stackabletech/operator-rs/pull/1094 + ## [0.97.0] - 2025-09-09 ### Added diff --git a/crates/stackable-operator/src/builder/meta.rs b/crates/stackable-operator/src/builder/meta.rs index ed1c3b20c..df48fa815 100644 --- a/crates/stackable-operator/src/builder/meta.rs +++ b/crates/stackable-operator/src/builder/meta.rs @@ -24,12 +24,13 @@ pub enum Error { /// It is strongly recommended to always call [`Self::with_recommended_labels()`]! #[derive(Clone, Default)] pub struct ObjectMetaBuilder { - ownerreference: Option, annotations: Option, + finalizers: Option>, generate_name: Option, - namespace: Option, labels: Option, + namespace: Option, name: Option, + ownerreference: Option, } impl ObjectMetaBuilder { @@ -163,6 +164,26 @@ impl ObjectMetaBuilder { Ok(self) } + /// This adds a single finalizer to the existing finalizers. + pub fn with_finalizer(&mut self, finalizer: impl Into) -> &mut Self { + self.finalizers + .get_or_insert(Vec::new()) + .push(finalizer.into()); + self + } + + /// This adds multiple finalizers to the existing finalizers. + pub fn with_finalizers(&mut self, finalizers: Vec) -> &mut Self { + self.finalizers.get_or_insert(Vec::new()).extend(finalizers); + self + } + + /// This will replace all existing finalizers + pub fn finalizers(&mut self, finalizers: Vec) -> &mut Self { + self.finalizers = Some(finalizers); + self + } + pub fn build(&self) -> ObjectMeta { // NOTE (Techassi): Shouldn't this take self instead of &self to consume // the builder and build ObjectMeta without cloning? @@ -187,6 +208,7 @@ impl ObjectMetaBuilder { .map(|ownerreference| vec![ownerreference.clone()]), labels: self.labels.clone().map(|l| l.into()), annotations: self.annotations.clone().map(|a| a.into()), + finalizers: self.finalizers.clone(), ..ObjectMeta::default() } } @@ -339,6 +361,7 @@ mod tests { }) .unwrap() .with_annotation(("foo", "bar").try_into().unwrap()) + .with_finalizer("finalizer") .build(); assert_eq!(meta.generate_name, Some("generate_foo".to_string())); @@ -352,5 +375,9 @@ mod tests { meta.annotations.as_ref().unwrap().get(&"foo".to_string()), Some(&"bar".to_string()) ); + assert_eq!( + meta.finalizers.as_ref().unwrap().first(), + Some(&"finalizer".to_string()) + ); } }