-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcontroller_utils.rs
More file actions
215 lines (178 loc) · 6.06 KB
/
Copy pathcontroller_utils.rs
File metadata and controls
215 lines (178 loc) · 6.06 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
//! Helper functions which are not tied to a specific controller step
use std::str::FromStr;
use snafu::{OptionExt, ResultExt, Snafu};
use strum::{EnumDiscriminants, IntoStaticStr};
use crate::{
kube::runtime::reflector::Lookup,
v2::types::{
kubernetes::{NamespaceName, Uid},
operator::ClusterName,
},
};
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
pub enum Error {
#[snafu(display("failed to get the cluster name"))]
GetClusterName {},
#[snafu(display("failed to get the namespace"))]
GetNamespace {},
#[snafu(display("failed to get the UID"))]
GetUid {},
#[snafu(display("failed to set the cluster name"))]
ParseClusterName {
source: crate::v2::macros::attributed_string_type::Error,
},
#[snafu(display("failed to set the namespace"))]
ParseNamespace {
source: crate::v2::macros::attributed_string_type::Error,
},
#[snafu(display("failed to set the UID"))]
ParseUid {
source: crate::v2::macros::attributed_string_type::Error,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
/// Get the cluster name from the given resource
pub fn get_cluster_name(cluster: &impl Lookup) -> Result<ClusterName> {
let raw_cluster_name = cluster.name().context(GetClusterNameSnafu)?;
let cluster_name = ClusterName::from_str(&raw_cluster_name).context(ParseClusterNameSnafu)?;
Ok(cluster_name)
}
/// Get the namespace from the given resource
pub fn get_namespace(resource: &impl Lookup) -> Result<NamespaceName> {
let raw_namespace = resource.namespace().context(GetNamespaceSnafu)?;
let namespace = NamespaceName::from_str(&raw_namespace).context(ParseNamespaceSnafu)?;
Ok(namespace)
}
/// Get the UID from the given resource
pub fn get_uid(resource: &impl Lookup) -> Result<Uid> {
let raw_uid = resource.uid().context(GetUidSnafu)?;
let uid = Uid::from_str(&raw_uid).context(ParseUidSnafu)?;
Ok(uid)
}
#[cfg(test)]
mod tests {
use uuid::uuid;
use super::{ErrorDiscriminants, get_cluster_name, get_namespace, get_uid};
use crate::{
kube::runtime::reflector::Lookup,
v2::types::{
kubernetes::{NamespaceName, Uid},
operator::ClusterName,
},
};
#[derive(Debug, Default)]
struct TestResource {
name: Option<&'static str>,
namespace: Option<&'static str>,
uid: Option<&'static str>,
}
impl Lookup for TestResource {
type DynamicType = ();
fn kind(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
"TestResource".into()
}
fn group(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
"stackable.tech".into()
}
fn version(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
"v1".into()
}
fn plural(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
"testresources".into()
}
fn name(&self) -> Option<std::borrow::Cow<'_, str>> {
self.name.map(std::borrow::Cow::Borrowed)
}
fn namespace(&self) -> Option<std::borrow::Cow<'_, str>> {
self.namespace.map(std::borrow::Cow::Borrowed)
}
fn resource_version(&self) -> Option<std::borrow::Cow<'_, str>> {
Some("1".into())
}
fn uid(&self) -> Option<std::borrow::Cow<'_, str>> {
self.uid.map(std::borrow::Cow::Borrowed)
}
}
#[test]
fn test_get_cluster_name() {
assert_eq!(
ClusterName::from_str_unsafe("test-cluster"),
get_cluster_name(&TestResource {
name: Some("test-cluster"),
..TestResource::default()
})
.expect("should contain a valid cluster name")
);
assert_eq!(
Err(ErrorDiscriminants::GetClusterName),
get_cluster_name(&TestResource {
name: None,
..TestResource::default()
})
.map_err(ErrorDiscriminants::from)
);
assert_eq!(
Err(ErrorDiscriminants::ParseClusterName),
get_cluster_name(&TestResource {
name: Some("invalid cluster name"),
..TestResource::default()
})
.map_err(ErrorDiscriminants::from)
);
}
#[test]
fn test_get_namespace() {
assert_eq!(
NamespaceName::from_str_unsafe("test-namespace"),
get_namespace(&TestResource {
namespace: Some("test-namespace"),
..TestResource::default()
})
.expect("should contain a valid namespace")
);
assert_eq!(
Err(ErrorDiscriminants::GetNamespace),
get_namespace(&TestResource {
namespace: None,
..TestResource::default()
})
.map_err(ErrorDiscriminants::from)
);
assert_eq!(
Err(ErrorDiscriminants::ParseNamespace),
get_namespace(&TestResource {
namespace: Some("invalid namespace"),
..TestResource::default()
})
.map_err(ErrorDiscriminants::from)
);
}
#[test]
fn test_get_uid() {
assert_eq!(
Uid::from(uuid!("e6ac237d-a6d4-43a1-8135-f36506110912")),
get_uid(&TestResource {
uid: Some("e6ac237d-a6d4-43a1-8135-f36506110912"),
..TestResource::default()
})
.expect("should contain a valid UID")
);
assert_eq!(
Err(ErrorDiscriminants::GetUid),
get_uid(&TestResource {
uid: None,
..TestResource::default()
})
.map_err(ErrorDiscriminants::from)
);
assert_eq!(
Err(ErrorDiscriminants::ParseUid),
get_uid(&TestResource {
uid: Some("invalid UID"),
..TestResource::default()
})
.map_err(ErrorDiscriminants::from)
);
}
}