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
use std::collections::BTreeMap;
use super::*;
pub use cluster_role::ClusterRoleExt;
pub use cluster_role_binding::ClusterRoleBindingExt;
pub use configmap::ConfigMapExt;
pub use container::ContainerExt;
pub use daemon_set::DaemonSetExt;
pub use deployment::DeploymentExt;
pub use env::EnvVarExt;
pub use env::ToEnvFrom;
pub use env::ToEnvVar;
pub use job::JobExt;
pub use label_selector::LabelSelectorExt;
pub use namespace::NamespaceExt;
pub use pod_spec::PodSpecExt;
pub use pod_template_spec::PodTemplateSpecExt;
pub use policy_rule::PolicyRuleExt;
pub use probe::ProbeExt;
pub use role::RoleExt;
pub use role_binding::RoleBindingExt;
pub use role_ref::RoleRefExt;
pub use secret::SecretExt;
pub use secret::SecretExt2;
pub use secret_env_source::SecretEnvSourceExt;
pub use secret_volume_source::SecretVolumeSourceExt;
pub use service::ServiceExt;
pub use service_account::ServiceAccountExt;
pub use storage_class::StorageClassExt;
pub use subject::SubjectExt;
pub use volume::VolumeExt;
pub use volume_mount::VolumeMountExt;
mod cluster_role;
mod cluster_role_binding;
mod configmap;
mod container;
mod daemon_set;
mod deployment;
mod env;
mod job;
mod label_selector;
mod namespace;
mod pod_spec;
mod pod_template_spec;
mod policy_rule;
mod probe;
mod role;
mod role_binding;
mod role_ref;
mod secret;
mod secret_env_source;
mod secret_volume_source;
mod service;
mod service_account;
mod storage_class;
mod subject;
mod volume;
mod volume_mount;
pub trait ResourceBuilder: Sized {
fn metadata(name: impl ToString) -> metav1::ObjectMeta {
metadata(name)
}
fn namespace(self, namespace: impl ToString) -> Self;
fn owner(self, owner: metav1::OwnerReference) -> Self;
fn labels(self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;
fn with_resource_version(self, resource_version: String) -> Self;
fn app_name(self, name: impl ToString) -> Self {
self.labels([(label::APP_NAME, name)])
}
fn app_instance(self, instance: impl ToString) -> Self {
self.labels([(label::APP_INSTANCE, instance)])
}
fn app_version(self, version: impl ToString) -> Self {
self.labels([(label::APP_VERSION, version)])
}
fn app_component(self, component: impl ToString) -> Self {
self.labels([(label::APP_COMPONENT, component)])
}
fn app_part_of(self, part_of: impl ToString) -> Self {
self.labels([(label::APP_PART_OF, part_of)])
}
fn app_managed_by(self, managed_by: impl ToString) -> Self {
self.labels([(label::APP_MANAGED_BY, managed_by)])
}
}
impl<T> ResourceBuilder for T
where
T: openapi::Metadata<Ty = metav1::ObjectMeta>,
{
fn namespace(mut self, namespace: impl ToString) -> Self {
let namespace = Some(namespace.to_string());
self.metadata_mut().namespace = namespace;
self
}
fn owner(mut self, owner: metav1::OwnerReference) -> Self {
self.metadata_mut()
.owner_references
.get_or_insert_with(Vec::new)
.push(owner);
self
}
fn labels(mut self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
let labels = labels
.into_iter()
.map(|(key, value)| (key.to_string(), value.to_string()));
self.metadata_mut()
.labels
.get_or_insert_with(BTreeMap::new)
.extend(labels);
self
}
fn with_resource_version(mut self, resource_version: String) -> Self {
self.metadata_mut().resource_version = Some(resource_version);
self
}
}
fn metadata(name: impl ToString) -> metav1::ObjectMeta {
let name = Some(name.to_string());
metav1::ObjectMeta {
name,
..metav1::ObjectMeta::default()
}
}
fn default<T: Default>() -> T {
Default::default()
}