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 container::ContainerExt;
10pub use cronjob::CronJobExt;
11pub use daemon_set::DaemonSetExt;
12pub use deployment::DeploymentExt;
13pub use env::EnvVarExt;
14pub use env::ToEnvFrom;
15pub use env::ToEnvVar;
16pub use hpa::HorizontalPodAutoscalerExt;
17pub use job::JobExt;
18pub use label_selector::LabelSelectorExt;
19pub use local_object_reference::LocalObjectReferenceExt;
20pub use metric::MetricExt;
21pub use namespace::NamespaceExt;
22pub use node::NodeExt;
23pub use owner_reference::OwnerReferenceExt;
24pub use pod::PodExt;
25pub use pod_spec::PodSpecExt;
26pub use pod_template_spec::PodTemplateSpecExt;
27pub use policy_rule::PolicyRuleExt;
28pub use probe::ProbeExt;
29pub use replica_set::ReplicaSetExt;
30pub use role::RoleExt;
31pub use role_binding::RoleBindingExt;
32pub use role_ref::RoleRefExt;
33pub use secret::SecretExt;
34pub use secret::SecretExt2;
35pub use secret_env_source::SecretEnvSourceExt;
36pub use secret_reference::SecretReferenceExt;
37pub use secret_volume_source::SecretVolumeSourceExt;
38pub use service::ServiceExt;
39pub use service_account::ServiceAccountExt;
40pub use service_port::ServicePortExt;
41pub use storage_class::StorageClassExt;
42pub use subject::SubjectExt;
43pub use taint::TaintExt;
44pub use time::TimeExt;
45pub use toleration::TolerationBuilder;
46pub use toleration::TolerationExt;
47pub use typed_object_reference::TypedObjectReferenceExt;
48pub use volume::VolumeExt;
49pub use volume_mount::VolumeMountExt;
50
51use effect::Effect;
52
53mod cluster_role;
54mod cluster_role_binding;
55mod configmap;
56mod container;
57mod cronjob;
58mod daemon_set;
59mod deployment;
60mod effect;
61mod env;
62mod hpa;
63mod job;
64mod label_selector;
65mod local_object_reference;
66mod metric;
67mod namespace;
68mod node;
69mod owner_reference;
70mod pod;
71mod pod_spec;
72mod pod_template_spec;
73mod policy_rule;
74mod probe;
75mod replica_set;
76mod role;
77mod role_binding;
78mod role_ref;
79mod secret;
80mod secret_env_source;
81mod secret_reference;
82mod secret_volume_source;
83mod service;
84mod service_account;
85mod service_port;
86mod storage_class;
87mod subject;
88mod taint;
89mod time;
90mod toleration;
91mod typed_object_reference;
92mod volume;
93mod volume_mount;
94
95pub trait ResourceBuilder: Sized {
96 fn metadata(name: impl ToString) -> metav1::ObjectMeta {
97 metadata(name)
98 }
99
100 fn namespace(self, namespace: impl ToString) -> Self;
103
104 fn owner(self, owner: metav1::OwnerReference) -> Self;
107
108 fn label(self, key: impl ToString, value: impl ToString) -> Self;
112
113 fn labels(self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;
116
117 fn with_resource_version(self, resource_version: String) -> Self;
118
119 fn app_name(self, name: impl ToString) -> Self {
122 self.labels([(label::APP_NAME, name)])
123 }
124
125 fn app_instance(self, instance: impl ToString) -> Self {
128 self.labels([(label::APP_INSTANCE, instance)])
129 }
130
131 fn app_version(self, version: impl ToString) -> Self {
134 self.labels([(label::APP_VERSION, version)])
135 }
136
137 fn app_component(self, component: impl ToString) -> Self {
140 self.labels([(label::APP_COMPONENT, component)])
141 }
142
143 fn app_part_of(self, part_of: impl ToString) -> Self {
146 self.labels([(label::APP_PART_OF, part_of)])
147 }
148
149 fn app_managed_by(self, managed_by: impl ToString) -> Self {
152 self.labels([(label::APP_MANAGED_BY, managed_by)])
153 }
154}
155
156impl<T> ResourceBuilder for T
157where
158 T: openapi::Metadata<Ty = metav1::ObjectMeta>,
159{
160 fn namespace(mut self, namespace: impl ToString) -> Self {
161 let namespace = Some(namespace.to_string());
162 self.metadata_mut().namespace = namespace;
163 self
164 }
165
166 fn owner(mut self, owner: metav1::OwnerReference) -> Self {
167 self.metadata_mut()
168 .owner_references
169 .get_or_insert_with(default)
170 .push(owner);
171 self
172 }
173
174 fn label(mut self, key: impl ToString, value: impl ToString) -> Self {
175 self.metadata_mut()
176 .labels
177 .get_or_insert_with(default)
178 .insert(key.to_string(), value.to_string());
179 self
180 }
181
182 fn labels(mut self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
183 let labels = labels
184 .into_iter()
185 .map(|(key, value)| (key.to_string(), value.to_string()));
186 self.metadata_mut()
187 .labels
188 .get_or_insert_with(default)
189 .extend(labels);
190 self
191 }
192
193 fn with_resource_version(mut self, resource_version: String) -> Self {
194 self.metadata_mut().resource_version = Some(resource_version);
195 self
196 }
197}
198
199fn metadata(name: impl ToString) -> metav1::ObjectMeta {
200 let name = Some(name.to_string());
201 metav1::ObjectMeta {
202 name,
203 ..default()
218 }
219}
220
221trait HasSpec {
222 type Spec;
223 fn spec_mut(&mut self) -> &mut Self::Spec;
224}