Skip to content

Commit b537300

Browse files
committed
Merge branch 'feature/podlisteners' of github.com:stackabletech/operator-rs into feature/podlisteners
2 parents df72972 + d85a64d commit b537300

File tree

6 files changed

+155
-22
lines changed

6 files changed

+155
-22
lines changed

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@ All notable changes to this project will be documented in this file.
1414

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

17+
## [0.48.0] - 2023-08-18
18+
19+
### Added
20+
21+
- Add `PodBuilder::termination_grace_period_seconds` ([#641]).
22+
- Add support for adding `lifecycle`s to `ContainerBuilder` ([#641]).
23+
24+
[#641]: https://github.com/stackabletech/operator-rs/pull/641
25+
26+
## [0.47.0] - 2023-08-16
27+
28+
### Added
29+
30+
- Implement `Display` for `MemoryQuantity` ([#638]).
31+
- Implement `Sum` for `CpuQuantity` and `MemoryQuantity` ([#634]).
32+
33+
### Changed
34+
35+
- Switch from `openssl` to `rustls` ([#635]).
36+
- Bump `product-config`` 0.4.0 -> 0.5.0 ([#639]).
37+
38+
### Fixed
39+
40+
- Fixed buggy `Div`, `SubAssign` and `AddAssign` for `MemoryQuantity` when left and right side had different units ([#636], [#637]).
41+
42+
[#634]: https://github.com/stackabletech/operator-rs/pull/634
43+
[#635]: https://github.com/stackabletech/operator-rs/pull/635
44+
[#636]: https://github.com/stackabletech/operator-rs/pull/636
45+
[#637]: https://github.com/stackabletech/operator-rs/pull/637
46+
[#638]: https://github.com/stackabletech/operator-rs/pull/638
47+
[#639]: https://github.com/stackabletech/operator-rs/pull/639
48+
1749
## [0.46.0] - 2023-08-08
1850

1951
### Changed

Cargo.toml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace.package]
2-
version = "0.46.0"
2+
version = "0.48.0"
33
authors = ["Stackable GmbH <info@stackable.de>"]
44
license = "Apache-2.0"
55
edition = "2021"
@@ -18,30 +18,31 @@ repository.workspace = true
1818
chrono = { version = "0.4.26", default-features = false }
1919
clap = { version = "4.3.19", features = ["derive", "cargo", "env"] }
2020
const_format = "0.2.31"
21+
derivative = "2.2.0"
2122
either = "1.9.0"
2223
futures = "0.3.28"
2324
json-patch = "1.0.0"
2425
k8s-openapi = { version = "0.19.0", default-features = false, features = ["schemars", "v1_27"] }
25-
kube = { version = "0.85.0", features = ["jsonpatch", "runtime", "derive"] }
26+
# 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
27+
kube = { version = "0.85.0", default-features = false, features = ["client", "jsonpatch", "runtime", "derive", "rustls-tls"] }
2628
lazy_static = "1.4.0"
27-
product-config = { git = "https://github.com/stackabletech/product-config.git", tag = "0.4.0" }
29+
opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
30+
opentelemetry-jaeger = { version = "0.19.0", features = ["rt-tokio"] }
31+
product-config = { git = "https://github.com/stackabletech/product-config.git", tag = "0.5.0" }
2832
rand = "0.8.5"
2933
regex = "1.9.3"
3034
schemars = "0.8.12"
31-
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
35+
serde = { version = "1.0.184", features = ["derive"] }
3236
serde_json = "1.0.104"
3337
serde_yaml = "0.9.25"
38+
snafu = "0.7.5"
39+
stackable-operator-derive = { path = "stackable-operator-derive" }
3440
strum = { version = "0.25.0", features = ["derive"] }
3541
thiserror = "1.0.44"
3642
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
3743
tracing = "0.1.37"
38-
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
39-
derivative = "2.2.0"
4044
tracing-opentelemetry = "0.20.0"
41-
opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
42-
opentelemetry-jaeger = { version = "0.19.0", features = ["rt-tokio"] }
43-
stackable-operator-derive = { path = "stackable-operator-derive" }
44-
snafu = "0.7.5"
45+
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
4546

4647
[dev-dependencies]
4748
rstest = "0.18.1"

src/builder/pod/container.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use k8s_openapi::api::core::v1::{
2-
ConfigMapKeySelector, Container, ContainerPort, EnvVar, EnvVarSource, ObjectFieldSelector,
3-
Probe, ResourceRequirements, SecretKeySelector, SecurityContext, VolumeMount,
2+
ConfigMapKeySelector, Container, ContainerPort, EnvVar, EnvVarSource, Lifecycle,
3+
LifecycleHandler, ObjectFieldSelector, Probe, ResourceRequirements, SecretKeySelector,
4+
SecurityContext, VolumeMount,
45
};
56
use std::fmt;
67

@@ -26,6 +27,7 @@ pub struct ContainerBuilder {
2627
readiness_probe: Option<Probe>,
2728
liveness_probe: Option<Probe>,
2829
startup_probe: Option<Probe>,
30+
lifecycle: Option<Lifecycle>,
2931
security_context: Option<SecurityContext>,
3032
}
3133

@@ -213,6 +215,23 @@ impl ContainerBuilder {
213215
self
214216
}
215217

218+
pub fn lifecycle(&mut self, lifecycle: Lifecycle) -> &mut Self {
219+
self.lifecycle = Some(lifecycle);
220+
self
221+
}
222+
223+
pub fn lifecycle_post_start(&mut self, post_start: LifecycleHandler) -> &mut Self {
224+
self.lifecycle
225+
.get_or_insert(Lifecycle::default())
226+
.post_start = Some(post_start);
227+
self
228+
}
229+
230+
pub fn lifecycle_pre_stop(&mut self, pre_stop: LifecycleHandler) -> &mut Self {
231+
self.lifecycle.get_or_insert(Lifecycle::default()).pre_stop = Some(pre_stop);
232+
self
233+
}
234+
216235
pub fn security_context(&mut self, context: SecurityContext) -> &mut Self {
217236
self.security_context = Some(context);
218237
self
@@ -237,6 +256,7 @@ impl ContainerBuilder {
237256
readiness_probe: self.readiness_probe.clone(),
238257
liveness_probe: self.liveness_probe.clone(),
239258
startup_probe: self.startup_probe.clone(),
259+
lifecycle: self.lifecycle.clone(),
240260
security_context: self.security_context.clone(),
241261
..Container::default()
242262
}
@@ -331,6 +351,8 @@ impl fmt::Display for FieldPathEnvVar {
331351

332352
#[cfg(test)]
333353
mod tests {
354+
use k8s_openapi::api::core::v1::ExecAction;
355+
334356
use super::*;
335357
use crate::{
336358
builder::{
@@ -400,6 +422,34 @@ mod tests {
400422
assert_eq!(container.resources, Some(resources));
401423
}
402424

425+
#[test]
426+
fn test_container_builder_lifecycle() {
427+
let post_start = LifecycleHandler {
428+
exec: Some(ExecAction {
429+
command: Some(vec!["hello".to_string(), "world".to_string()]),
430+
}),
431+
..Default::default()
432+
};
433+
let pre_stop = LifecycleHandler {
434+
exec: Some(ExecAction {
435+
command: Some(vec!["bye".to_string(), "bye".to_string()]),
436+
}),
437+
..Default::default()
438+
};
439+
let container = ContainerBuilder::new("testcontainer")
440+
.expect("ContainerBuilder not created")
441+
.lifecycle_post_start(post_start.clone())
442+
.lifecycle_pre_stop(pre_stop.clone())
443+
.build();
444+
assert_eq!(
445+
container.lifecycle,
446+
Some(Lifecycle {
447+
post_start: Some(post_start),
448+
pre_stop: Some(pre_stop)
449+
})
450+
);
451+
}
452+
403453
#[test]
404454
fn test_container_port_builder() {
405455
let port: i32 = 10000;

src/builder/pod/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct PodBuilder {
4444
service_account_name: Option<String>,
4545
image_pull_secrets: Option<Vec<LocalObjectReference>>,
4646
restart_policy: Option<String>,
47+
termination_grace_period_seconds: Option<i64>,
4748
}
4849

4950
impl PodBuilder {
@@ -451,6 +452,14 @@ impl PodBuilder {
451452
self
452453
}
453454

455+
pub fn termination_grace_period_seconds(
456+
&mut self,
457+
termination_grace_period_seconds: i64,
458+
) -> &mut Self {
459+
self.termination_grace_period_seconds = Some(termination_grace_period_seconds);
460+
self
461+
}
462+
454463
/// Consumes the Builder and returns a constructed [`Pod`]
455464
pub fn build(&self) -> OperatorResult<Pod> {
456465
Ok(Pod {
@@ -498,6 +507,7 @@ impl PodBuilder {
498507
service_account_name: self.service_account_name.clone(),
499508
image_pull_secrets: self.image_pull_secrets.clone(),
500509
restart_policy: self.restart_policy.clone(),
510+
termination_grace_period_seconds: self.termination_grace_period_seconds,
501511
..PodSpec::default()
502512
};
503513

@@ -627,6 +637,7 @@ mod tests {
627637
.with_config_map("configmap")
628638
.build(),
629639
)
640+
.termination_grace_period_seconds(42)
630641
.build()
631642
.unwrap();
632643

@@ -654,6 +665,7 @@ mod tests {
654665
.and_then(|volume| volume.config_map.as_ref()?.name.clone())),
655666
Some("configmap".to_string())
656667
);
668+
assert_eq!(pod_spec.termination_grace_period_seconds, Some(42));
657669
}
658670

659671
#[rstest]

src/cpu.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
iter::Sum,
23
ops::{Add, AddAssign, Div, Mul, MulAssign},
34
str::FromStr,
45
};
@@ -151,6 +152,12 @@ impl MulAssign<f32> for CpuQuantity {
151152
}
152153
}
153154

155+
impl Sum for CpuQuantity {
156+
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
157+
iter.fold(CpuQuantity { millis: 0 }, CpuQuantity::add)
158+
}
159+
}
160+
154161
#[cfg(test)]
155162
mod test {
156163
use super::*;

src/memory.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ impl Div<MemoryQuantity> for MemoryQuantity {
324324
type Output = f32;
325325

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

342343
impl SubAssign<MemoryQuantity> for MemoryQuantity {
343344
fn sub_assign(&mut self, rhs: MemoryQuantity) {
345+
let rhs = rhs.scale_to(self.unit);
344346
self.value -= rhs.value;
345347
}
346348
}
@@ -356,7 +358,7 @@ impl Add<MemoryQuantity> for MemoryQuantity {
356358
}
357359
}
358360

359-
impl Sum<MemoryQuantity> for MemoryQuantity {
361+
impl Sum for MemoryQuantity {
360362
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
361363
iter.fold(
362364
MemoryQuantity {
@@ -370,6 +372,7 @@ impl Sum<MemoryQuantity> for MemoryQuantity {
370372

371373
impl AddAssign<MemoryQuantity> for MemoryQuantity {
372374
fn add_assign(&mut self, rhs: MemoryQuantity) {
375+
let rhs = rhs.scale_to(self.unit);
373376
self.value += rhs.value;
374377
}
375378
}
@@ -411,6 +414,12 @@ impl FromStr for MemoryQuantity {
411414
}
412415
}
413416

417+
impl Display for MemoryQuantity {
418+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
419+
write!(f, "{}{}", self.value, self.unit)
420+
}
421+
}
422+
414423
impl TryFrom<Quantity> for MemoryQuantity {
415424
type Error = Error;
416425

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

436445
impl From<&MemoryQuantity> for Quantity {
437446
fn from(quantity: &MemoryQuantity) -> Self {
438-
Quantity(format!("{}{}", quantity.value, quantity.unit))
447+
Quantity(format!("{}", quantity))
439448
}
440449
}
441450

@@ -447,17 +456,30 @@ mod test {
447456
use rstest::rstest;
448457

449458
#[rstest]
450-
#[case("256Ki", MemoryQuantity { value: 256f32, unit: BinaryMultiple::Kibi })]
451-
#[case("8Mi", MemoryQuantity { value: 8f32, unit: BinaryMultiple::Mebi })]
452-
#[case("1.5Gi", MemoryQuantity { value: 1.5f32, unit: BinaryMultiple::Gibi })]
453-
#[case("0.8Ti", MemoryQuantity { value: 0.8f32, unit: BinaryMultiple::Tebi })]
454-
#[case("3.2Pi", MemoryQuantity { value: 3.2f32, unit: BinaryMultiple::Pebi })]
455-
#[case("0.2Ei", MemoryQuantity { value: 0.2f32, unit: BinaryMultiple::Exbi })]
459+
#[case("256Ki", MemoryQuantity { value: 256.0, unit: BinaryMultiple::Kibi })]
460+
#[case("49041204Ki", MemoryQuantity { value: 49041204.0, unit: BinaryMultiple::Kibi })]
461+
#[case("8Mi", MemoryQuantity { value: 8.0, unit: BinaryMultiple::Mebi })]
462+
#[case("1.5Gi", MemoryQuantity { value: 1.5, unit: BinaryMultiple::Gibi })]
463+
#[case("0.8Ti", MemoryQuantity { value: 0.8, unit: BinaryMultiple::Tebi })]
464+
#[case("3.2Pi", MemoryQuantity { value: 3.2, unit: BinaryMultiple::Pebi })]
465+
#[case("0.2Ei", MemoryQuantity { value: 0.2, unit: BinaryMultiple::Exbi })]
456466
fn test_memory_parse(#[case] input: &str, #[case] output: MemoryQuantity) {
457467
let got = input.parse::<MemoryQuantity>().unwrap();
458468
assert_eq!(got, output);
459469
}
460470

471+
#[rstest]
472+
#[case("256Ki")]
473+
#[case("1.6Mi")]
474+
#[case("1.2Gi")]
475+
#[case("1.6Gi")]
476+
#[case("1Gi")]
477+
pub fn test_fmt(#[case] q: String) {
478+
let m = MemoryQuantity::try_from(Quantity(q.clone())).unwrap();
479+
let actual = format!("{m}");
480+
assert_eq!(q, actual);
481+
}
482+
461483
#[rstest]
462484
#[case("256Ki", 1.0, "-Xmx256k")]
463485
#[case("256Ki", 0.8, "-Xmx205k")]
@@ -553,7 +575,11 @@ mod test {
553575
let rhs = MemoryQuantity::try_from(Quantity(rhs.to_owned())).unwrap();
554576
let expected = MemoryQuantity::try_from(Quantity(res.to_owned())).unwrap();
555577
let actual = lhs - rhs;
556-
assert_eq!(expected, actual)
578+
assert_eq!(expected, actual);
579+
580+
let mut actual = lhs;
581+
actual -= rhs;
582+
assert_eq!(expected, actual);
557583
}
558584

559585
#[rstest]
@@ -566,7 +592,12 @@ mod test {
566592
let rhs = MemoryQuantity::try_from(Quantity(rhs.to_owned())).unwrap();
567593
let expected = MemoryQuantity::try_from(Quantity(res.to_owned())).unwrap();
568594
let actual = lhs + rhs;
569-
assert_eq!(expected, actual)
595+
assert_eq!(expected, actual);
596+
597+
let mut actual = MemoryQuantity::from_mebi(0.0);
598+
actual += lhs;
599+
actual += rhs;
600+
assert_eq!(expected, actual);
570601
}
571602

572603
#[rstest]

0 commit comments

Comments
 (0)