Skip to content

Commit

Permalink
Add ListenerOperatorVolumeSourceBuilder::build_pvc (#719)
Browse files Browse the repository at this point in the history
* Add ListenerOperatorVolumeSourceBuilder::build_pvc

* Changelog
  • Loading branch information
nightkr authored Jan 17, 2024
1 parent 998685a commit 3e002b4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ All notable changes to this project will be documented in this file.
### Added

- Added `Option::as_ref_or_else` to `utils` ([#717]).
- Added `ListenerOperatorVolumeSourceBuilder::build_pvc` ([#719]).

### Changed

- Split `utils` into submodules ([#717]).
- Renamed `ListenerOperatorVolumeSourceBuilder::build` to `::build_ephemeral` ([#719]).

[#717]: https://github.com/stackabletech/operator-rs/pull/717
[#719]: https://github.com/stackabletech/operator-rs/pull/719

## [0.61.0] - 2024-01-15

Expand Down
4 changes: 2 additions & 2 deletions src/builder/pod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl PodBuilder {
) -> Result<&mut Self> {
let listener_reference = ListenerReference::ListenerClass(listener_class.to_string());
let volume = ListenerOperatorVolumeSourceBuilder::new(&listener_reference)
.build()
.build_ephemeral()
.context(ListenerVolumeSnafu { name: volume_name })?;

self.add_volume(Volume {
Expand Down Expand Up @@ -440,7 +440,7 @@ impl PodBuilder {
) -> Result<&mut Self> {
let listener_reference = ListenerReference::ListenerName(listener_name.to_string());
let volume = ListenerOperatorVolumeSourceBuilder::new(&listener_reference)
.build()
.build_ephemeral()
.context(ListenerVolumeSnafu { name: volume_name })?;

self.add_volume(Volume {
Expand Down
60 changes: 46 additions & 14 deletions src/builder/pod/volume.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use k8s_openapi::{
api::core::v1::{
CSIVolumeSource, ConfigMapVolumeSource, DownwardAPIVolumeSource, EmptyDirVolumeSource,
EphemeralVolumeSource, HostPathVolumeSource, PersistentVolumeClaimSpec,
PersistentVolumeClaimTemplate, PersistentVolumeClaimVolumeSource, ProjectedVolumeSource,
ResourceRequirements, SecretVolumeSource, Volume, VolumeMount,
EphemeralVolumeSource, HostPathVolumeSource, PersistentVolumeClaim,
PersistentVolumeClaimSpec, PersistentVolumeClaimTemplate,
PersistentVolumeClaimVolumeSource, ProjectedVolumeSource, ResourceRequirements,
SecretVolumeSource, Volume, VolumeMount,
},
apimachinery::pkg::api::resource::Quantity,
};
Expand Down Expand Up @@ -461,8 +462,27 @@ impl ListenerOperatorVolumeSourceBuilder {
}
}

/// Build an [`EphemeralVolumeSource`] from the builder
fn build_spec(&self) -> PersistentVolumeClaimSpec {
PersistentVolumeClaimSpec {
storage_class_name: Some("listeners.stackable.tech".to_string()),
resources: Some(ResourceRequirements {
requests: Some([("storage".to_string(), Quantity("1".to_string()))].into()),
..ResourceRequirements::default()
}),
access_modes: Some(vec!["ReadWriteMany".to_string()]),
..PersistentVolumeClaimSpec::default()
}
}

#[deprecated(note = "renamed to `build_ephemeral`", since = "0.61.1")]
pub fn build(&self) -> Result<EphemeralVolumeSource, ListenerOperatorVolumeSourceBuilderError> {
self.build_ephemeral()
}

/// Build an [`EphemeralVolumeSource`] from the builder.
pub fn build_ephemeral(
&self,
) -> Result<EphemeralVolumeSource, ListenerOperatorVolumeSourceBuilderError> {
let listener_reference_annotation = self
.listener_reference
.to_annotation()
Expand All @@ -475,18 +495,30 @@ impl ListenerOperatorVolumeSourceBuilder {
.with_annotation(listener_reference_annotation)
.build(),
),
spec: PersistentVolumeClaimSpec {
storage_class_name: Some("listeners.stackable.tech".to_string()),
resources: Some(ResourceRequirements {
requests: Some([("storage".to_string(), Quantity("1".to_string()))].into()),
..ResourceRequirements::default()
}),
access_modes: Some(vec!["ReadWriteMany".to_string()]),
..PersistentVolumeClaimSpec::default()
},
spec: self.build_spec(),
}),
})
}

/// Build a [`PersistentVolumeClaim`] from the builder.
pub fn build_pvc(
&self,
name: impl Into<String>,
) -> Result<PersistentVolumeClaim, ListenerOperatorVolumeSourceBuilderError> {
let listener_reference_annotation = self
.listener_reference
.to_annotation()
.context(ListenerReferenceAnnotationSnafu)?;

Ok(PersistentVolumeClaim {
metadata: ObjectMetaBuilder::new()
.name(name)
.with_annotation(listener_reference_annotation)
.build(),
spec: Some(self.build_spec()),
..Default::default()
})
}
}

#[cfg(test)]
Expand Down Expand Up @@ -556,7 +588,7 @@ mod tests {
"public".into(),
));

let volume_source = builder.build().unwrap();
let volume_source = builder.build_ephemeral().unwrap();

let volume_claim_template = volume_source.volume_claim_template;
let annotations = volume_claim_template
Expand Down

0 comments on commit 3e002b4

Please sign in to comment.