-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmeta.rs
More file actions
123 lines (106 loc) · 3.47 KB
/
Copy pathmeta.rs
File metadata and controls
123 lines (106 loc) · 3.47 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
use crate::{
builder::meta::OwnerReferenceBuilder,
k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference,
kube::Resource,
v2::{HasName, HasUid},
};
/// Infallible variant of
/// [`stackable_operator::builder::meta::ObjectMetaBuilder::ownerreference_from_resource`]
pub fn ownerreference_from_resource(
resource: &(impl Resource<DynamicType = ()> + HasName + HasUid),
block_owner_deletion: Option<bool>,
controller: Option<bool>,
) -> OwnerReference {
OwnerReferenceBuilder::new()
// Set api_version, kind, name and additionally the UID if it exists.
.initialize_from_resource(resource)
// Ensure that the name is set.
.name(resource.to_name())
// Ensure that the UID is set.
.uid(resource.to_uid().to_string())
.block_owner_deletion_opt(block_owner_deletion)
.controller_opt(controller)
.build()
.expect(
"OwnerReference should be created because the resource has an api_version, kind, name \
and uid.",
)
}
#[cfg(test)]
mod tests {
use std::borrow::Cow;
use crate::{
k8s_openapi::apimachinery::pkg::apis::meta::v1::{ObjectMeta, OwnerReference},
kube::Resource,
v2::{HasName, HasUid, Uid, builder::meta::ownerreference_from_resource},
};
struct Cluster {
object_meta: ObjectMeta,
}
impl Cluster {
fn new() -> Self {
Cluster {
object_meta: ObjectMeta {
name: Some("cluster-name".to_owned()),
uid: Some("a6b89911-d48e-4328-88d6-b9251226583d".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 HasUid for Cluster {
fn to_uid(&self) -> Uid {
Uid::from_str_unsafe(
&self
.object_meta
.uid
.clone()
.expect("should be set in Cluster::new"),
)
}
}
#[test]
fn test_ownerreference_from_resource() {
let actual_owner_reference =
ownerreference_from_resource(&Cluster::new(), Some(true), Some(true));
let expected_owner_reference = OwnerReference {
api_version: "group/version".to_owned(),
block_owner_deletion: Some(true),
controller: Some(true),
kind: "kind".to_owned(),
name: "cluster-name".to_owned(),
uid: "a6b89911-d48e-4328-88d6-b9251226583d".to_owned(),
};
assert_eq!(expected_owner_reference, actual_owner_reference);
}
}