k8s_openapi_ext/
ext.rs

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    /// Set namespace for this object
109    ///
110    fn namespace(self, namespace: impl ToString) -> Self;
111
112    /// Set the owner for this object
113    ///
114    fn owner(self, owner: metav1::OwnerReference) -> Self;
115
116    /// Set one label for this object.
117    /// For settins multiple lables at once prefer `labels()`
118    ///
119    fn label(self, key: impl ToString, value: impl ToString) -> Self;
120
121    /// Set labels for this object
122    ///
123    fn labels(self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;
124
125    /// Set one annotation for this object.
126    /// For settins multiple lables at once prefer `labels()`
127    ///
128    fn annotation(self, key: impl ToString, value: impl ToString) -> Self;
129
130    /// Set annotations for this object
131    ///
132    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    /// Set recommended label 'app.kubernetes.io/name'
140    ///
141    fn app_name(self, name: impl ToString) -> Self {
142        self.labels([(label::APP_NAME, name)])
143    }
144
145    /// Set recommended label 'app.kubernetes.io/instance'
146    ///
147    fn app_instance(self, instance: impl ToString) -> Self {
148        self.labels([(label::APP_INSTANCE, instance)])
149    }
150
151    /// Set recommended label 'app.kubernetes.io/version'
152    ///
153    fn app_version(self, version: impl ToString) -> Self {
154        self.labels([(label::APP_VERSION, version)])
155    }
156
157    /// Set recommended label 'app.kubernetes.io/component'
158    ///
159    fn app_component(self, component: impl ToString) -> Self {
160        self.labels([(label::APP_COMPONENT, component)])
161    }
162
163    /// Set recommended label 'app.kubernetes.io/part-of'
164    ///
165    fn app_part_of(self, part_of: impl ToString) -> Self {
166        self.labels([(label::APP_PART_OF, part_of)])
167    }
168
169    /// Set recommended label 'app.kubernetes.io/managed-by'
170    ///
171    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        // annotations: todo!(),
246        // creation_timestamp: todo!(),
247        // deletion_grace_period_seconds: todo!(),
248        // deletion_timestamp: todo!(),
249        // finalizers: todo!(),
250        // generate_name: todo!(),
251        // generation: todo!(),
252        // labels: todo!(),
253        // managed_fields: todo!(),
254        // namespace: todo!(),
255        // owner_references: todo!(),
256        // resource_version: todo!(),
257        // self_link: todo!(),
258        // uid: todo!(),
259        ..default()
260    }
261}
262
263trait HasSpec {
264    type Spec;
265    fn spec_mut(&mut self) -> &mut Self::Spec;
266}