Skip to content

Commit

Permalink
Use MemoryQuantity instead of u32 containing MiB
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedweber committed Jul 12, 2023
1 parent 4327052 commit 2779717
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use crate::error::{Error, OperatorResult};
use std::{
fmt::Display,
iter::Sum,
ops::{Add, AddAssign, Div, Mul, Sub, SubAssign},
str::FromStr,
};
Expand Down Expand Up @@ -347,6 +348,18 @@ impl Add<MemoryQuantity> for MemoryQuantity {
}
}

impl Sum<MemoryQuantity> for MemoryQuantity {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(
MemoryQuantity {
value: 0.0,
unit: BinaryMultiple::Kibi,
},
|a, b| a + b,
)
}
}

impl AddAssign<MemoryQuantity> for MemoryQuantity {
fn add_assign(&mut self, rhs: MemoryQuantity) {
self.value += rhs.value;
Expand Down
30 changes: 23 additions & 7 deletions src/product_logging/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
apimachinery::pkg::api::resource::Quantity,
},
kube::Resource,
memory::{BinaryMultiple, MemoryQuantity},
role_utils::RoleGroupRef,
};

Expand Down Expand Up @@ -49,29 +50,44 @@ pub const VECTOR_CONFIG_FILE: &str = "vector.toml";
/// PodBuilder,
/// meta::ObjectMetaBuilder,
/// },
/// memory::{
/// BinaryMultiple,
/// MemoryQuantity,
/// },
/// };
/// # use stackable_operator::product_logging;
///
/// pub const MAX_INIT_CONTAINER_LOG_FILES_SIZE_IN_MIB: u32 = 1;
/// pub const MAX_MAIN_CONTAINER_LOG_FILES_SIZE_IN_MIB: u32 = 10;
/// const MAX_INIT_CONTAINER_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity {
/// value: 1.0,
/// unit: BinaryMultiple::Mebi,
/// };
/// const MAX_MAIN_CONTAINER_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity {
/// value: 10.0,
/// unit: BinaryMultiple::Mebi,
/// };
///
/// PodBuilder::new()
/// .metadata(ObjectMetaBuilder::default().build())
/// .add_empty_dir_volume(
/// "log",
/// Some(product_logging::framework::calculate_log_volume_size_limit(
/// &[
/// MAX_INIT_CONTAINER_LOG_FILES_SIZE_IN_MIB,
/// MAX_MAIN_CONTAINER_LOG_FILES_SIZE_IN_MIB,
/// MAX_INIT_CONTAINER_LOG_FILES_SIZE,
/// MAX_MAIN_CONTAINER_LOG_FILES_SIZE,
/// ],
/// )),
/// )
/// .build()
/// .unwrap();
/// ```
pub fn calculate_log_volume_size_limit(max_log_files_size_in_mib: &[u32]) -> Quantity {
let log_volume_size_limit_in_mib = max_log_files_size_in_mib.iter().sum::<u32>() * 3;
Quantity(format!("{log_volume_size_limit_in_mib}Mi"))
pub fn calculate_log_volume_size_limit(max_log_files_size: &[MemoryQuantity]) -> Quantity {
let log_volume_size_limit = max_log_files_size
.iter()
.cloned()
.sum::<MemoryQuantity>()
.scale_to(BinaryMultiple::Mebi)
* 3.0;
log_volume_size_limit.into()
}

/// Create a Bash command which filters stdout and stderr according to the given log configuration
Expand Down

0 comments on commit 2779717

Please sign in to comment.