-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlabel.rs
More file actions
211 lines (186 loc) · 6.49 KB
/
Copy pathlabel.rs
File metadata and controls
211 lines (186 loc) · 6.49 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
use crate::{
kube::Resource,
kvp::{Labels, ObjectLabels},
v2::{
HasName, NameIsValidLabelValue,
types::operator::{
ControllerName, OperatorName, ProductName, ProductVersion, RoleGroupName, RoleName,
},
},
};
/// Infallible variant of [`stackable_operator::kvp::Labels::recommended`]
pub fn recommended_labels(
owner: &(impl Resource + HasName + NameIsValidLabelValue),
product_name: &ProductName,
product_version: &ProductVersion,
operator_name: &OperatorName,
controller_name: &ControllerName,
role_name: &RoleName,
role_group_name: &RoleGroupName,
) -> Labels {
let object_labels = ObjectLabels {
owner,
app_name: &product_name.to_label_value(),
app_version: &product_version.to_label_value(),
operator_name: &operator_name.to_label_value(),
controller_name: &controller_name.to_label_value(),
role: &role_name.to_label_value(),
role_group: &role_group_name.to_label_value(),
};
Labels::recommended(&object_labels).expect(
"Labels should be created because the owner has an object name and all given parameters \
produce valid label values.",
)
}
/// Infallible variant of [`stackable_operator::kvp::Labels::role_selector`]
pub fn role_selector(
owner: &(impl Resource + HasName + NameIsValidLabelValue),
product_name: &ProductName,
role_name: &RoleName,
) -> Labels {
Labels::role_selector(
owner,
&product_name.to_label_value(),
&role_name.to_label_value(),
)
.expect("Labels should be created because all given parameters produce valid label values")
}
/// Infallible variant of [`stackable_operator::kvp::Labels::role_group_selector`]
pub fn role_group_selector(
owner: &(impl Resource + HasName + NameIsValidLabelValue),
product_name: &ProductName,
role_name: &RoleName,
role_group_name: &RoleGroupName,
) -> Labels {
Labels::role_group_selector(
owner,
&product_name.to_label_value(),
&role_name.to_label_value(),
&role_group_name.to_label_value(),
)
.expect("Labels should be created because all given parameters produce valid label values")
}
#[cfg(test)]
mod tests {
use std::{borrow::Cow, collections::BTreeMap};
use crate::{
k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta,
kube::Resource,
v2::{
HasName, NameIsValidLabelValue,
kvp::label::{recommended_labels, role_group_selector, role_selector},
types::operator::{
ControllerName, OperatorName, ProductName, ProductVersion, RoleGroupName, RoleName,
},
},
};
struct Cluster {
object_meta: ObjectMeta,
}
impl Cluster {
fn new() -> Self {
Self {
object_meta: ObjectMeta {
name: Some("cluster-name".to_owned()),
..ObjectMeta::default()
},
}
}
}
impl Resource for Cluster {
type DynamicType = ();
type Scope = ();
fn kind(_dt: &Self::DynamicType) -> Cow<'_, str> {
Cow::from("kind")
}
fn group(_dt: &Self::DynamicType) -> Cow<'_, str> {
Cow::from("group")
}
fn version(_dt: &Self::DynamicType) -> Cow<'_, str> {
Cow::from("version")
}
fn plural(_dt: &Self::DynamicType) -> Cow<'_, str> {
Cow::from("plural")
}
fn meta(&self) -> &ObjectMeta {
&self.object_meta
}
fn meta_mut(&mut self) -> &mut ObjectMeta {
&mut self.object_meta
}
}
impl HasName for Cluster {
fn to_name(&self) -> String {
self.object_meta
.name
.clone()
.expect("should be set in Cluster::new")
}
}
impl NameIsValidLabelValue for Cluster {
fn to_label_value(&self) -> String {
self.object_meta
.name
.clone()
.expect("should be set in Cluster::new")
}
}
#[test]
fn test_recommended_labels() {
let actual_labels = recommended_labels(
&Cluster::new(),
&ProductName::from_str_unsafe("my-product"),
&ProductVersion::from_str_unsafe("1.0.0"),
&OperatorName::from_str_unsafe("my-operator"),
&ControllerName::from_str_unsafe("my-controller"),
&RoleName::from_str_unsafe("my-role"),
&RoleGroupName::from_str_unsafe("my-role-group"),
);
let expected_labels: BTreeMap<String, String> = [
("app.kubernetes.io/component", "my-role"),
("app.kubernetes.io/instance", "cluster-name"),
("app.kubernetes.io/managed-by", "my-operator_my-controller"),
("app.kubernetes.io/name", "my-product"),
("app.kubernetes.io/role-group", "my-role-group"),
("app.kubernetes.io/version", "1.0.0"),
("stackable.tech/vendor", "Stackable"),
]
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.into();
assert_eq!(expected_labels, actual_labels.into());
}
#[test]
fn test_role_selector() {
let actual_labels = role_selector(
&Cluster::new(),
&ProductName::from_str_unsafe("my-product"),
&RoleName::from_str_unsafe("my-role"),
);
let expected_labels: BTreeMap<String, String> = [
("app.kubernetes.io/component", "my-role"),
("app.kubernetes.io/instance", "cluster-name"),
("app.kubernetes.io/name", "my-product"),
]
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.into();
assert_eq!(expected_labels, actual_labels.into());
}
#[test]
fn test_role_group_selector() {
let actual_labels = role_group_selector(
&Cluster::new(),
&ProductName::from_str_unsafe("my-product"),
&RoleName::from_str_unsafe("my-role"),
&RoleGroupName::from_str_unsafe("my-role-group"),
);
let expected_labels: BTreeMap<String, String> = [
("app.kubernetes.io/component", "my-role"),
("app.kubernetes.io/instance", "cluster-name"),
("app.kubernetes.io/name", "my-product"),
("app.kubernetes.io/role-group", "my-role-group"),
]
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.into();
assert_eq!(expected_labels, actual_labels.into());
}
}