-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmod.rs
More file actions
330 lines (280 loc) · 10.5 KB
/
mod.rs
File metadata and controls
330 lines (280 loc) · 10.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! This module provides various types and functions to construct valid Kubernetes
//! annotations. Annotations are key/value pairs, where the key must meet certain
//! requirementens regarding length and character set. The value can contain
//! **any** valid UTF-8 data.
//!
//! Additionally, the [`Annotation`] struct provides various helper functions to
//! construct commonly used annotations across the Stackable Data Platform, like
//! the secret scope or class.
//!
//! See <https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/>
//! for more information on Kubernetes annotations.
use std::{
collections::{BTreeMap, BTreeSet},
convert::Infallible,
fmt::Display,
};
use delegate::delegate;
use crate::{
builder::pod::volume::SecretOperatorVolumeScope,
iter::TryFromIterator,
kvp::{Key, KeyValuePair, KeyValuePairError, KeyValuePairs, KeyValuePairsError},
};
mod value;
pub use value::*;
pub type AnnotationsError = KeyValuePairsError;
/// A type alias for errors returned when construction or manipulation of a set
/// of annotations fails.
pub type AnnotationError = KeyValuePairError<Infallible>;
/// A specialized implementation of a key/value pair representing Kubernetes
/// annotations.
///
/// The validation of the annotation value can **never** fail, as [`str`] is
/// guaranteed to only contain valid UTF-8 data - which is the only
/// requirement for a valid Kubernetes annotation value.
///
/// See <https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/>
/// for more information on Kubernetes annotations.
#[derive(Debug)]
pub struct Annotation(KeyValuePair<AnnotationValue>);
impl<K, V> TryFrom<(K, V)> for Annotation
where
K: AsRef<str>,
V: AsRef<str>,
{
type Error = AnnotationError;
fn try_from(value: (K, V)) -> Result<Self, Self::Error> {
let kvp = KeyValuePair::try_from(value)?;
Ok(Self(kvp))
}
}
impl Display for Annotation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Annotation {
/// Returns an immutable reference to the annotation's [`Key`].
pub fn key(&self) -> &Key {
self.0.key()
}
/// Returns an immutable reference to the annotation's value.
pub fn value(&self) -> &AnnotationValue {
self.0.value()
}
/// Consumes self and returns the inner [`KeyValuePair<AnnotationValue>`].
pub fn into_inner(self) -> KeyValuePair<AnnotationValue> {
self.0
}
/// Constructs a `secrets.stackable.tech/class` annotation.
pub fn secret_class(secret_class: &str) -> Result<Self, AnnotationError> {
let kvp = KeyValuePair::try_from(("secrets.stackable.tech/class", secret_class))?;
Ok(Self(kvp))
}
/// Constructs a `secrets.stackable.tech/scope` annotation.
pub fn secret_scope(
scopes: impl AsRef<[SecretOperatorVolumeScope]>,
) -> Result<Self, AnnotationError> {
let mut value = String::new();
for scope in scopes.as_ref() {
if !value.is_empty() {
value.push(',');
}
match scope {
SecretOperatorVolumeScope::Node => value.push_str("node"),
SecretOperatorVolumeScope::Pod => value.push_str("pod"),
SecretOperatorVolumeScope::Service { name } => {
value.push_str("service=");
value.push_str(name);
}
SecretOperatorVolumeScope::ListenerVolume { name } => {
value.push_str("listener-volume=");
value.push_str(name);
}
}
}
let kvp = KeyValuePair::try_from(("secrets.stackable.tech/scope", value))?;
Ok(Self(kvp))
}
/// Constructs a `secrets.stackable.tech/format` annotation.
pub fn secret_format(format: &str) -> Result<Self, AnnotationError> {
let kvp = KeyValuePair::try_from(("secrets.stackable.tech/format", format))?;
Ok(Self(kvp))
}
/// Constructs a `secrets.stackable.tech/kerberos.service.names` annotation.
pub fn kerberos_service_names(names: impl AsRef<[String]>) -> Result<Self, AnnotationError> {
let names = names.as_ref().join(",");
let kvp = KeyValuePair::try_from(("secrets.stackable.tech/kerberos.service.names", names))?;
Ok(Self(kvp))
}
/// Constructs a `secrets.stackable.tech/format.compatibility.tls-pkcs12.password`
/// annotation.
pub fn tls_pkcs12_password(password: &str) -> Result<Self, AnnotationError> {
let kvp = KeyValuePair::try_from((
"secrets.stackable.tech/format.compatibility.tls-pkcs12.password",
password,
))?;
Ok(Self(kvp))
}
}
/// A validated set/list of Kubernetes annotations.
///
/// It provides selected associated functions to manipulate the set of
/// annotations, like inserting or extending.
///
/// ## Examples
///
/// ### Converting a BTreeMap into a list of labels
///
/// ```
/// # use std::collections::BTreeMap;
/// # use stackable_operator::kvp::Annotations;
/// let map = BTreeMap::from([
/// ("stackable.tech/managed-by", "stackablectl"),
/// ("stackable.tech/vendor", "Stäckable"),
/// ]);
///
/// let labels = Annotations::try_from(map).unwrap();
/// ```
///
/// ### Creating a list of labels from an array
///
/// ```
/// # use stackable_operator::kvp::Annotations;
/// let labels = Annotations::try_from([
/// ("stackable.tech/managed-by", "stackablectl"),
/// ("stackable.tech/vendor", "Stäckable"),
/// ]).unwrap();
/// ```
#[derive(Clone, Debug, Default)]
pub struct Annotations(KeyValuePairs<AnnotationValue>);
impl<K, V> TryFrom<BTreeMap<K, V>> for Annotations
where
K: AsRef<str>,
V: AsRef<str>,
{
type Error = AnnotationError;
fn try_from(map: BTreeMap<K, V>) -> Result<Self, Self::Error> {
Self::try_from_iter(map)
}
}
impl<K, V> TryFrom<&BTreeMap<K, V>> for Annotations
where
K: AsRef<str>,
V: AsRef<str>,
{
type Error = AnnotationError;
fn try_from(map: &BTreeMap<K, V>) -> Result<Self, Self::Error> {
Self::try_from_iter(map)
}
}
impl<const N: usize, K, V> TryFrom<[(K, V); N]> for Annotations
where
K: AsRef<str>,
V: AsRef<str>,
{
type Error = AnnotationError;
fn try_from(array: [(K, V); N]) -> Result<Self, Self::Error> {
Self::try_from_iter(array)
}
}
impl FromIterator<KeyValuePair<AnnotationValue>> for Annotations {
fn from_iter<T: IntoIterator<Item = KeyValuePair<AnnotationValue>>>(iter: T) -> Self {
let kvps = KeyValuePairs::from_iter(iter);
Self(kvps)
}
}
impl<K, V> TryFromIterator<(K, V)> for Annotations
where
K: AsRef<str>,
V: AsRef<str>,
{
type Error = AnnotationError;
fn try_from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Result<Self, Self::Error> {
let kvps = KeyValuePairs::try_from_iter(iter)?;
Ok(Self(kvps))
}
}
impl From<Annotations> for BTreeMap<String, String> {
fn from(value: Annotations) -> Self {
value.0.into()
}
}
impl Annotations {
/// Creates a new empty list of [`Annotations`].
pub fn new() -> Self {
Self::default()
}
/// Creates a new list of [`Annotations`] from `pairs`.
pub fn new_with(pairs: BTreeSet<KeyValuePair<AnnotationValue>>) -> Self {
Self(KeyValuePairs::new_with(pairs))
}
/// Tries to insert a new annotation by first parsing `annotation` as an
/// [`Annotation`] and then inserting it into the list. This function will
/// overwrite any existing annotation already present.
pub fn parse_insert(
&mut self,
annotation: impl TryInto<Annotation, Error = AnnotationError>,
) -> Result<(), AnnotationError> {
self.0.insert(annotation.try_into()?.0);
Ok(())
}
/// Inserts a new [`Annotation`]. This function will overwrite any existing
/// annotation already present.
pub fn insert(&mut self, annotation: Annotation) -> &mut Self {
self.0.insert(annotation.0);
self
}
// This forwards / delegates associated functions to the inner field. In
// this case self.0 which is of type KeyValuePairs<T>. So calling
// Annotations::len() will be delegated to KeyValuePair<T>::len() without
// the need to write boilerplate code.
delegate! {
to self.0 {
/// Tries to insert a new [`Annotation`]. It ensures there are no duplicate
/// entries. Trying to insert duplicated data returns an error. If no such
/// check is required, use [`Annotations::insert`] instead.
pub fn try_insert(&mut self, #[newtype] annotation: Annotation) -> Result<(), AnnotationsError>;
/// Extends `self` with `other`.
pub fn extend(&mut self, #[newtype] other: Self);
/// Returns the number of labels.
pub fn len(&self) -> usize;
/// Returns if the set of labels is empty.
pub fn is_empty(&self) -> bool;
/// Returns if the set of annotations contains the provided
/// `annotation`. Failure to parse/validate the [`KeyValuePair`]
/// will return `false`.
pub fn contains(&self, annotation: impl TryInto<KeyValuePair<AnnotationValue>>) -> bool;
/// Returns if the set of annotations contains a label with the
/// provided `key`. Failure to parse/validate the [`Key`] will
/// return `false`.
pub fn contains_key(&self, key: impl TryInto<Key>) -> bool;
/// Returns an [`Iterator`] over [`Annotations`] yielding a reference to every [`Annotation`] contained within.
pub fn iter(&self) -> impl Iterator<Item = KeyValuePair<AnnotationValue>> + '_;
}
}
}
impl IntoIterator for Annotations {
type Item = KeyValuePair<AnnotationValue>;
type IntoIter = <KeyValuePairs<AnnotationValue> as IntoIterator>::IntoIter;
/// Returns a consuming [`Iterator`] over [`Annotations`] moving every [`Annotation`] out.
/// The [`Annotations`] cannot be used again after calling this.
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_insert() {
let mut annotations = Annotations::new();
annotations
.parse_insert(("stackable.tech/managed-by", "stackablectl"))
.unwrap();
annotations
.parse_insert(("stackable.tech/vendor", "Stäckable"))
.unwrap();
assert_eq!(annotations.len(), 2);
}
}