-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathstatefulset.rs
More file actions
119 lines (105 loc) · 4.19 KB
/
Copy pathstatefulset.rs
File metadata and controls
119 lines (105 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::collections::BTreeMap;
use crate::{
kvp::Annotations,
v2::types::kubernetes::{ConfigMapName, SecretName},
};
/// Creates `restarter.stackable.tech/ignore-configmap.{i}` annotations for each given ConfigMap.
///
/// The restarter uses these annotations to skip restarting Pods when specific ConfigMaps change.
/// Indices start at 0 and are assigned in iteration order, so **do not merge the result with
/// annotations from another call** — duplicate indices would overwrite each other.
pub fn restarter_ignore_configmap_annotations(
ignored_config_maps: impl IntoIterator<Item = ConfigMapName>,
) -> Annotations {
let annotation_key_values = ignored_config_maps
.into_iter()
.enumerate()
.map(|(i, config_map_name)| {
(
format!("restarter.stackable.tech/ignore-configmap.{i}"),
config_map_name.to_string(),
)
})
.collect::<BTreeMap<_, _>>();
Annotations::try_from(annotation_key_values).expect(
"should contain only valid annotations because the annotation keys are statically \
defined apart from the index number and the names of ConfigMaps are valid annotation \
values.",
)
}
/// Creates `restarter.stackable.tech/ignore-secret.{i}` annotations for each given Secret.
///
/// The restarter uses these annotations to skip restarting Pods when specific Secrets change.
/// Indices start at 0 and are assigned in iteration order, so **do not merge the result with
/// annotations from another call** — duplicate indices would overwrite each other.
pub fn restarter_ignore_secret_annotations(
ignored_secrets: impl IntoIterator<Item = SecretName>,
) -> Annotations {
let annotation_key_values = ignored_secrets
.into_iter()
.enumerate()
.map(|(i, secret_name)| {
(
format!("restarter.stackable.tech/ignore-secret.{i}"),
secret_name.to_string(),
)
})
.collect::<BTreeMap<_, _>>();
Annotations::try_from(annotation_key_values).expect(
"should contain only valid annotations because the annotation keys are statically \
defined apart from the index number and the names of Secrets are valid annotation \
values.",
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn multiple_config_maps_produce_indexed_annotations() {
let ignored_config_maps = [
ConfigMapName::from_str_unsafe("first-config"),
ConfigMapName::from_str_unsafe("second-config"),
ConfigMapName::from_str_unsafe("third-config"),
];
let actual_annotations = restarter_ignore_configmap_annotations(ignored_config_maps);
let expected_annotations = BTreeMap::from([
(
"restarter.stackable.tech/ignore-configmap.0".to_owned(),
"first-config".to_owned(),
),
(
"restarter.stackable.tech/ignore-configmap.1".to_owned(),
"second-config".to_owned(),
),
(
"restarter.stackable.tech/ignore-configmap.2".to_owned(),
"third-config".to_owned(),
),
]);
assert_eq!(expected_annotations, actual_annotations.into());
}
#[test]
fn multiple_secrets_produce_indexed_annotations() {
let ignored_secrets = [
SecretName::from_str_unsafe("first-secret"),
SecretName::from_str_unsafe("second-secret"),
SecretName::from_str_unsafe("third-secret"),
];
let actual_annotations = restarter_ignore_secret_annotations(ignored_secrets);
let expected_annotations = BTreeMap::from([
(
"restarter.stackable.tech/ignore-secret.0".to_owned(),
"first-secret".to_owned(),
),
(
"restarter.stackable.tech/ignore-secret.1".to_owned(),
"second-secret".to_owned(),
),
(
"restarter.stackable.tech/ignore-secret.2".to_owned(),
"third-secret".to_owned(),
),
]);
assert_eq!(expected_annotations, actual_annotations.into());
}
}