1use std::collections::BTreeMap;
2use std::fmt;
3
4use super::*;
5
6pub use cluster_role::ClusterRoleExt;
7pub use cluster_role_binding::ClusterRoleBindingExt;
8pub use configmap::ConfigMapExt;
9pub use configmap_volume_source::ConfigMapVolumeSourceExt;
10pub use container::ContainerExt;
11pub use container_port::ContainerPortExt;
12pub use cronjob::CronJobExt;
13pub use daemon_set::DaemonSetExt;
14pub use deployment::DeploymentExt;
15pub use env::EnvVarExt;
16pub use env::ToEnvFrom;
17pub use env::ToEnvVar;
18pub use hpa::HorizontalPodAutoscalerExt;
19pub use job::JobExt;
20pub use label_selector::LabelSelectorExt;
21pub use local_object_reference::LocalObjectReferenceExt;
22pub use metric::MetricExt;
23pub use namespace::NamespaceExt;
24pub use node::NodeExt;
25pub use owner_reference::OwnerReferenceExt;
26pub use pod::PodExt;
27pub use pod_spec::PodSpecExt;
28pub use pod_template_spec::PodTemplateSpecExt;
29pub use policy_rule::PolicyRuleExt;
30pub use probe::ProbeExt;
31pub use replicaset::ReplicaSetExt;
32pub use role::RoleExt;
33pub use role_binding::RoleBindingExt;
34pub use role_ref::RoleRefExt;
35pub use secret::SecretExt;
36pub use secret::SecretExt2;
37pub use secret_env_source::SecretEnvSourceExt;
38pub use secret_reference::SecretReferenceExt;
39pub use secret_volume_source::SecretVolumeSourceExt;
40pub use security_context::SecurityContextExt;
41pub use service::ServiceExt;
42pub use service_account::ServiceAccountExt;
43pub use service_port::ServicePortExt;
44pub use statefulset::StatefulSetExt;
45pub use storage_class::StorageClassExt;
46pub use subject::SubjectExt;
47pub use taint::TaintExt;
48pub use time::TimeExt;
49pub use toleration::TolerationBuilder;
50pub use toleration::TolerationExt;
51pub use typed_object_reference::TypedObjectReferenceExt;
52pub use volume::VolumeExt;
53pub use volume_mount::VolumeMountExt;
54
55use effect::Effect;
56
57mod cluster_role;
58mod cluster_role_binding;
59mod configmap;
60mod configmap_volume_source;
61mod container;
62mod container_port;
63mod cronjob;
64mod daemon_set;
65mod deployment;
66mod effect;
67mod env;
68mod hpa;
69mod job;
70mod label_selector;
71mod local_object_reference;
72mod metric;
73mod namespace;
74mod node;
75mod owner_reference;
76mod pod;
77mod pod_spec;
78mod pod_template_spec;
79mod policy_rule;
80mod probe;
81mod replicaset;
82mod role;
83mod role_binding;
84mod role_ref;
85mod secret;
86mod secret_env_source;
87mod secret_reference;
88mod secret_volume_source;
89mod security_context;
90mod service;
91mod service_account;
92mod service_port;
93mod statefulset;
94mod storage_class;
95mod subject;
96mod taint;
97mod time;
98mod toleration;
99mod typed_object_reference;
100mod volume;
101mod volume_mount;
102
103pub trait ResourceBuilder: Sized {
104 fn metadata(name: impl ToString) -> metav1::ObjectMeta {
105 metadata(name)
106 }
107
108 fn namespace(self, namespace: impl ToString) -> Self;
111
112 fn owner(self, owner: metav1::OwnerReference) -> Self;
115
116 fn label(self, key: impl ToString, value: impl ToString) -> Self;
120
121 fn labels(self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;
124
125 fn annotation(self, key: impl ToString, value: impl ToString) -> Self;
129
130 fn annotations(
133 self,
134 annotations: impl IntoIterator<Item = (impl ToString, impl ToString)>,
135 ) -> Self;
136
137 fn with_resource_version(self, resource_version: String) -> Self;
138
139 fn app_name(self, name: impl ToString) -> Self {
142 self.labels([(label::APP_NAME, name)])
143 }
144
145 fn app_instance(self, instance: impl ToString) -> Self {
148 self.labels([(label::APP_INSTANCE, instance)])
149 }
150
151 fn app_version(self, version: impl ToString) -> Self {
154 self.labels([(label::APP_VERSION, version)])
155 }
156
157 fn app_component(self, component: impl ToString) -> Self {
160 self.labels([(label::APP_COMPONENT, component)])
161 }
162
163 fn app_part_of(self, part_of: impl ToString) -> Self {
166 self.labels([(label::APP_PART_OF, part_of)])
167 }
168
169 fn app_managed_by(self, managed_by: impl ToString) -> Self {
172 self.labels([(label::APP_MANAGED_BY, managed_by)])
173 }
174}
175
176impl<T> ResourceBuilder for T
177where
178 T: openapi::Metadata<Ty = metav1::ObjectMeta>,
179{
180 fn namespace(mut self, namespace: impl ToString) -> Self {
181 let namespace = Some(namespace.to_string());
182 self.metadata_mut().namespace = namespace;
183 self
184 }
185
186 fn owner(mut self, owner: metav1::OwnerReference) -> Self {
187 self.metadata_mut()
188 .owner_references
189 .get_or_insert_default()
190 .push(owner);
191 self
192 }
193
194 fn label(mut self, key: impl ToString, value: impl ToString) -> Self {
195 self.metadata_mut()
196 .labels
197 .get_or_insert_default()
198 .insert(key.to_string(), value.to_string());
199 self
200 }
201
202 fn labels(mut self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
203 let labels = labels
204 .into_iter()
205 .map(|(key, value)| (key.to_string(), value.to_string()));
206 self.metadata_mut()
207 .labels
208 .get_or_insert_default()
209 .extend(labels);
210 self
211 }
212
213 fn annotation(mut self, key: impl ToString, value: impl ToString) -> Self {
214 self.metadata_mut()
215 .annotations
216 .get_or_insert_default()
217 .insert(key.to_string(), value.to_string());
218 self
219 }
220
221 fn annotations(
222 mut self,
223 annotations: impl IntoIterator<Item = (impl ToString, impl ToString)>,
224 ) -> Self {
225 let annotations = annotations
226 .into_iter()
227 .map(|(key, value)| (key.to_string(), value.to_string()));
228 self.metadata_mut()
229 .annotations
230 .get_or_insert_default()
231 .extend(annotations);
232 self
233 }
234
235 fn with_resource_version(mut self, resource_version: String) -> Self {
236 self.metadata_mut().resource_version = Some(resource_version);
237 self
238 }
239}
240
241fn metadata(name: impl ToString) -> metav1::ObjectMeta {
242 let name = Some(name.to_string());
243 metav1::ObjectMeta {
244 name,
245 ..default()
260 }
261}
262
263trait HasSpec {
264 type Spec;
265 fn spec_mut(&mut self) -> &mut Self::Spec;
266}