-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontainer.rs
More file actions
93 lines (76 loc) · 2.26 KB
/
Copy pathcontainer.rs
File metadata and controls
93 lines (76 loc) · 2.26 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
use std::collections::BTreeMap;
use stackable_operator::{
builder::pod::container::FieldPathEnvVar,
k8s_openapi::api::core::v1::{EnvVar, EnvVarSource, ObjectFieldSelector},
};
// TODO Use validated type
type EnvVarName = String;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct EnvVarSet(BTreeMap<EnvVarName, EnvVar>);
impl EnvVarSet {
pub fn new() -> Self {
Self::default()
}
pub fn get_env_var(&self, env_var_name: impl Into<EnvVarName>) -> Option<&EnvVar> {
self.0.get(&env_var_name.into())
}
pub fn add_env_var(mut self, env_var: EnvVar) -> Self {
self.0.insert(env_var.name.clone(), env_var);
self
}
pub fn merge(mut self, mut env_var_set: EnvVarSet) -> Self {
self.0.append(&mut env_var_set.0);
self
}
pub fn with_values<I, K, V>(self, env_vars: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
K: Into<EnvVarName>,
V: Into<String>,
{
env_vars
.into_iter()
.fold(self, |extended_env_vars, (name, value)| {
extended_env_vars.with_value(name, value)
})
}
pub fn with_value(mut self, name: impl Into<EnvVarName>, value: impl Into<String>) -> Self {
let name: EnvVarName = name.into();
self.0.insert(
name.clone(),
EnvVar {
name,
value: Some(value.into()),
value_from: None,
},
);
self
}
pub fn with_field_path(
mut self,
name: impl Into<EnvVarName>,
field_path: FieldPathEnvVar,
) -> Self {
let name: EnvVarName = name.into();
self.0.insert(
name.clone(),
EnvVar {
name,
value: None,
value_from: Some(EnvVarSource {
field_ref: Some(ObjectFieldSelector {
field_path: field_path.to_string(),
..ObjectFieldSelector::default()
}),
..EnvVarSource::default()
}),
},
);
self
}
}
impl From<EnvVarSet> for Vec<EnvVar> {
fn from(value: EnvVarSet) -> Self {
value.0.values().cloned().collect()
}
}