Skip to content

Commit

Permalink
Merge branch 'main' into feature/podlisteners
Browse files Browse the repository at this point in the history
  • Loading branch information
nightkr authored Sep 5, 2023
2 parents 5989481 + 89d655f commit d85a64d
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 22 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ All notable changes to this project will be documented in this file.

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

## [0.48.0] - 2023-08-18

### Added

- Add `PodBuilder::termination_grace_period_seconds` ([#641]).
- Add support for adding `lifecycle`s to `ContainerBuilder` ([#641]).

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

## [0.47.0] - 2023-08-16

### Added

- Implement `Display` for `MemoryQuantity` ([#638]).
- Implement `Sum` for `CpuQuantity` and `MemoryQuantity` ([#634]).

### Changed

- Switch from `openssl` to `rustls` ([#635]).
- Bump `product-config`` 0.4.0 -> 0.5.0 ([#639]).

### Fixed

- Fixed buggy `Div`, `SubAssign` and `AddAssign` for `MemoryQuantity` when left and right side had different units ([#636], [#637]).

[#634]: https://github.com/stackabletech/operator-rs/pull/634
[#635]: https://github.com/stackabletech/operator-rs/pull/635
[#636]: https://github.com/stackabletech/operator-rs/pull/636
[#637]: https://github.com/stackabletech/operator-rs/pull/637
[#638]: https://github.com/stackabletech/operator-rs/pull/638
[#639]: https://github.com/stackabletech/operator-rs/pull/639

## [0.46.0] - 2023-08-08

### Changed
Expand Down
21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.46.0"
version = "0.48.0"
authors = ["Stackable GmbH <info@stackable.de>"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -18,30 +18,31 @@ repository.workspace = true
chrono = { version = "0.4.26", default-features = false }
clap = { version = "4.3.19", features = ["derive", "cargo", "env"] }
const_format = "0.2.31"
derivative = "2.2.0"
either = "1.9.0"
futures = "0.3.28"
json-patch = "1.0.0"
k8s-openapi = { version = "0.19.0", default-features = false, features = ["schemars", "v1_27"] }
kube = { version = "0.85.0", features = ["jsonpatch", "runtime", "derive"] }
# We use rustls instead of openssl for easier portablitly, e.g. so that we can build stackablectl without the need to vendor (build from source) openssl
kube = { version = "0.85.0", default-features = false, features = ["client", "jsonpatch", "runtime", "derive", "rustls-tls"] }
lazy_static = "1.4.0"
product-config = { git = "https://github.com/stackabletech/product-config.git", tag = "0.4.0" }
opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
opentelemetry-jaeger = { version = "0.19.0", features = ["rt-tokio"] }
product-config = { git = "https://github.com/stackabletech/product-config.git", tag = "0.5.0" }
rand = "0.8.5"
regex = "1.9.3"
schemars = "0.8.12"
serde = { version = "=1.0.171", features = ["derive"] } # We need to pin 1.0.171 as of now, as otherwise Nix builds break because of https://github.com/serde-rs/serde/issues/2538
serde = { version = "1.0.184", features = ["derive"] }
serde_json = "1.0.104"
serde_yaml = "0.9.25"
snafu = "0.7.5"
stackable-operator-derive = { path = "stackable-operator-derive" }
strum = { version = "0.25.0", features = ["derive"] }
thiserror = "1.0.44"
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
derivative = "2.2.0"
tracing-opentelemetry = "0.20.0"
opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
opentelemetry-jaeger = { version = "0.19.0", features = ["rt-tokio"] }
stackable-operator-derive = { path = "stackable-operator-derive" }
snafu = "0.7.5"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

[dev-dependencies]
rstest = "0.18.1"
Expand Down
54 changes: 52 additions & 2 deletions src/builder/pod/container.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use k8s_openapi::api::core::v1::{
ConfigMapKeySelector, Container, ContainerPort, EnvVar, EnvVarSource, ObjectFieldSelector,
Probe, ResourceRequirements, SecretKeySelector, SecurityContext, VolumeMount,
ConfigMapKeySelector, Container, ContainerPort, EnvVar, EnvVarSource, Lifecycle,
LifecycleHandler, ObjectFieldSelector, Probe, ResourceRequirements, SecretKeySelector,
SecurityContext, VolumeMount,
};
use std::fmt;

Expand All @@ -26,6 +27,7 @@ pub struct ContainerBuilder {
readiness_probe: Option<Probe>,
liveness_probe: Option<Probe>,
startup_probe: Option<Probe>,
lifecycle: Option<Lifecycle>,
security_context: Option<SecurityContext>,
}

Expand Down Expand Up @@ -213,6 +215,23 @@ impl ContainerBuilder {
self
}

pub fn lifecycle(&mut self, lifecycle: Lifecycle) -> &mut Self {
self.lifecycle = Some(lifecycle);
self
}

pub fn lifecycle_post_start(&mut self, post_start: LifecycleHandler) -> &mut Self {
self.lifecycle
.get_or_insert(Lifecycle::default())
.post_start = Some(post_start);
self
}

pub fn lifecycle_pre_stop(&mut self, pre_stop: LifecycleHandler) -> &mut Self {
self.lifecycle.get_or_insert(Lifecycle::default()).pre_stop = Some(pre_stop);
self
}

pub fn security_context(&mut self, context: SecurityContext) -> &mut Self {
self.security_context = Some(context);
self
Expand All @@ -237,6 +256,7 @@ impl ContainerBuilder {
readiness_probe: self.readiness_probe.clone(),
liveness_probe: self.liveness_probe.clone(),
startup_probe: self.startup_probe.clone(),
lifecycle: self.lifecycle.clone(),
security_context: self.security_context.clone(),
..Container::default()
}
Expand Down Expand Up @@ -331,6 +351,8 @@ impl fmt::Display for FieldPathEnvVar {

#[cfg(test)]
mod tests {
use k8s_openapi::api::core::v1::ExecAction;

use super::*;
use crate::{
builder::{
Expand Down Expand Up @@ -400,6 +422,34 @@ mod tests {
assert_eq!(container.resources, Some(resources));
}

#[test]
fn test_container_builder_lifecycle() {
let post_start = LifecycleHandler {
exec: Some(ExecAction {
command: Some(vec!["hello".to_string(), "world".to_string()]),
}),
..Default::default()
};
let pre_stop = LifecycleHandler {
exec: Some(ExecAction {
command: Some(vec!["bye".to_string(), "bye".to_string()]),
}),
..Default::default()
};
let container = ContainerBuilder::new("testcontainer")
.expect("ContainerBuilder not created")
.lifecycle_post_start(post_start.clone())
.lifecycle_pre_stop(pre_stop.clone())
.build();
assert_eq!(
container.lifecycle,
Some(Lifecycle {
post_start: Some(post_start),
pre_stop: Some(pre_stop)
})
);
}

#[test]
fn test_container_port_builder() {
let port: i32 = 10000;
Expand Down
12 changes: 12 additions & 0 deletions src/builder/pod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct PodBuilder {
service_account_name: Option<String>,
image_pull_secrets: Option<Vec<LocalObjectReference>>,
restart_policy: Option<String>,
termination_grace_period_seconds: Option<i64>,
}

impl PodBuilder {
Expand Down Expand Up @@ -451,6 +452,14 @@ impl PodBuilder {
self
}

pub fn termination_grace_period_seconds(
&mut self,
termination_grace_period_seconds: i64,
) -> &mut Self {
self.termination_grace_period_seconds = Some(termination_grace_period_seconds);
self
}

/// Consumes the Builder and returns a constructed [`Pod`]
pub fn build(&self) -> OperatorResult<Pod> {
Ok(Pod {
Expand Down Expand Up @@ -498,6 +507,7 @@ impl PodBuilder {
service_account_name: self.service_account_name.clone(),
image_pull_secrets: self.image_pull_secrets.clone(),
restart_policy: self.restart_policy.clone(),
termination_grace_period_seconds: self.termination_grace_period_seconds,
..PodSpec::default()
};

Expand Down Expand Up @@ -627,6 +637,7 @@ mod tests {
.with_config_map("configmap")
.build(),
)
.termination_grace_period_seconds(42)
.build()
.unwrap();

Expand Down Expand Up @@ -654,6 +665,7 @@ mod tests {
.and_then(|volume| volume.config_map.as_ref()?.name.clone())),
Some("configmap".to_string())
);
assert_eq!(pod_spec.termination_grace_period_seconds, Some(42));
}

#[rstest]
Expand Down
7 changes: 7 additions & 0 deletions src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
iter::Sum,
ops::{Add, AddAssign, Div, Mul, MulAssign},
str::FromStr,
};
Expand Down Expand Up @@ -151,6 +152,12 @@ impl MulAssign<f32> for CpuQuantity {
}
}

impl Sum for CpuQuantity {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(CpuQuantity { millis: 0 }, CpuQuantity::add)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
51 changes: 41 additions & 10 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ impl Div<MemoryQuantity> for MemoryQuantity {
type Output = f32;

fn div(self, rhs: MemoryQuantity) -> Self::Output {
let rhs = rhs.scale_to(self.unit);
self.value / rhs.value
}
}
Expand All @@ -341,6 +342,7 @@ impl Sub<MemoryQuantity> for MemoryQuantity {

impl SubAssign<MemoryQuantity> for MemoryQuantity {
fn sub_assign(&mut self, rhs: MemoryQuantity) {
let rhs = rhs.scale_to(self.unit);
self.value -= rhs.value;
}
}
Expand All @@ -356,7 +358,7 @@ impl Add<MemoryQuantity> for MemoryQuantity {
}
}

impl Sum<MemoryQuantity> for MemoryQuantity {
impl Sum for MemoryQuantity {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(
MemoryQuantity {
Expand All @@ -370,6 +372,7 @@ impl Sum<MemoryQuantity> for MemoryQuantity {

impl AddAssign<MemoryQuantity> for MemoryQuantity {
fn add_assign(&mut self, rhs: MemoryQuantity) {
let rhs = rhs.scale_to(self.unit);
self.value += rhs.value;
}
}
Expand Down Expand Up @@ -411,6 +414,12 @@ impl FromStr for MemoryQuantity {
}
}

impl Display for MemoryQuantity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.value, self.unit)
}
}

impl TryFrom<Quantity> for MemoryQuantity {
type Error = Error;

Expand All @@ -435,7 +444,7 @@ impl From<MemoryQuantity> for Quantity {

impl From<&MemoryQuantity> for Quantity {
fn from(quantity: &MemoryQuantity) -> Self {
Quantity(format!("{}{}", quantity.value, quantity.unit))
Quantity(format!("{}", quantity))
}
}

Expand All @@ -447,17 +456,30 @@ mod test {
use rstest::rstest;

#[rstest]
#[case("256Ki", MemoryQuantity { value: 256f32, unit: BinaryMultiple::Kibi })]
#[case("8Mi", MemoryQuantity { value: 8f32, unit: BinaryMultiple::Mebi })]
#[case("1.5Gi", MemoryQuantity { value: 1.5f32, unit: BinaryMultiple::Gibi })]
#[case("0.8Ti", MemoryQuantity { value: 0.8f32, unit: BinaryMultiple::Tebi })]
#[case("3.2Pi", MemoryQuantity { value: 3.2f32, unit: BinaryMultiple::Pebi })]
#[case("0.2Ei", MemoryQuantity { value: 0.2f32, unit: BinaryMultiple::Exbi })]
#[case("256Ki", MemoryQuantity { value: 256.0, unit: BinaryMultiple::Kibi })]
#[case("49041204Ki", MemoryQuantity { value: 49041204.0, unit: BinaryMultiple::Kibi })]
#[case("8Mi", MemoryQuantity { value: 8.0, unit: BinaryMultiple::Mebi })]
#[case("1.5Gi", MemoryQuantity { value: 1.5, unit: BinaryMultiple::Gibi })]
#[case("0.8Ti", MemoryQuantity { value: 0.8, unit: BinaryMultiple::Tebi })]
#[case("3.2Pi", MemoryQuantity { value: 3.2, unit: BinaryMultiple::Pebi })]
#[case("0.2Ei", MemoryQuantity { value: 0.2, unit: BinaryMultiple::Exbi })]
fn test_memory_parse(#[case] input: &str, #[case] output: MemoryQuantity) {
let got = input.parse::<MemoryQuantity>().unwrap();
assert_eq!(got, output);
}

#[rstest]
#[case("256Ki")]
#[case("1.6Mi")]
#[case("1.2Gi")]
#[case("1.6Gi")]
#[case("1Gi")]
pub fn test_fmt(#[case] q: String) {
let m = MemoryQuantity::try_from(Quantity(q.clone())).unwrap();
let actual = format!("{m}");
assert_eq!(q, actual);
}

#[rstest]
#[case("256Ki", 1.0, "-Xmx256k")]
#[case("256Ki", 0.8, "-Xmx205k")]
Expand Down Expand Up @@ -553,7 +575,11 @@ mod test {
let rhs = MemoryQuantity::try_from(Quantity(rhs.to_owned())).unwrap();
let expected = MemoryQuantity::try_from(Quantity(res.to_owned())).unwrap();
let actual = lhs - rhs;
assert_eq!(expected, actual)
assert_eq!(expected, actual);

let mut actual = lhs;
actual -= rhs;
assert_eq!(expected, actual);
}

#[rstest]
Expand All @@ -566,7 +592,12 @@ mod test {
let rhs = MemoryQuantity::try_from(Quantity(rhs.to_owned())).unwrap();
let expected = MemoryQuantity::try_from(Quantity(res.to_owned())).unwrap();
let actual = lhs + rhs;
assert_eq!(expected, actual)
assert_eq!(expected, actual);

let mut actual = MemoryQuantity::from_mebi(0.0);
actual += lhs;
actual += rhs;
assert_eq!(expected, actual);
}

#[rstest]
Expand Down

0 comments on commit d85a64d

Please sign in to comment.