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 replica_set::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 storage_class::StorageClassExt;
45pub use subject::SubjectExt;
46pub use taint::TaintExt;
47pub use time::TimeExt;
48pub use toleration::TolerationBuilder;
49pub use toleration::TolerationExt;
50pub use typed_object_reference::TypedObjectReferenceExt;
51pub use volume::VolumeExt;
52pub use volume_mount::VolumeMountExt;
53
54use effect::Effect;
55
56mod cluster_role;
57mod cluster_role_binding;
58mod configmap;
59mod configmap_volume_source;
60mod container;
61mod container_port;
62mod cronjob;
63mod daemon_set;
64mod deployment;
65mod effect;
66mod env;
67mod hpa;
68mod job;
69mod label_selector;
70mod local_object_reference;
71mod metric;
72mod namespace;
73mod node;
74mod owner_reference;
75mod pod;
76mod pod_spec;
77mod pod_template_spec;
78mod policy_rule;
79mod probe;
80mod replica_set;
81mod role;
82mod role_binding;
83mod role_ref;
84mod secret;
85mod secret_env_source;
86mod secret_reference;
87mod secret_volume_source;
88mod security_context;
89mod service;
90mod service_account;
91mod service_port;
92mod storage_class;
93mod subject;
94mod taint;
95mod time;
96mod toleration;
97mod typed_object_reference;
98mod volume;
99mod volume_mount;
100
101pub trait ResourceBuilder: Sized {
102    fn metadata(name: impl ToString) -> metav1::ObjectMeta {
103        metadata(name)
104    }
105
106    /// Set namespace for this object
107    ///
108    fn namespace(self, namespace: impl ToString) -> Self;
109
110    /// Set the owner for this object
111    ///
112    fn owner(self, owner: metav1::OwnerReference) -> Self;
113
114    /// Set one label for this object.
115    /// For settins multiple lables at once prefer `labels()`
116    ///
117    fn label(self, key: impl ToString, value: impl ToString) -> Self;
118
119    /// Set labels for this object
120    ///
121    fn labels(self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;
122
123    /// Set one annotation for this object.
124    /// For settins multiple lables at once prefer `labels()`
125    ///
126    fn annotation(self, key: impl ToString, value: impl ToString) -> Self;
127
128    /// Set annotations for this object
129    ///
130    fn annotations(
131        self,
132        annotations: impl IntoIterator<Item = (impl ToString, impl ToString)>,
133    ) -> Self;
134
135    fn with_resource_version(self, resource_version: String) -> Self;
136
137    /// Set recommended label 'app.kubernetes.io/name'
138    ///
139    fn app_name(self, name: impl ToString) -> Self {
140        self.labels([(label::APP_NAME, name)])
141    }
142
143    /// Set recommended label 'app.kubernetes.io/instance'
144    ///
145    fn app_instance(self, instance: impl ToString) -> Self {
146        self.labels([(label::APP_INSTANCE, instance)])
147    }
148
149    /// Set recommended label 'app.kubernetes.io/version'
150    ///
151    fn app_version(self, version: impl ToString) -> Self {
152        self.labels([(label::APP_VERSION, version)])
153    }
154
155    /// Set recommended label 'app.kubernetes.io/component'
156    ///
157    fn app_component(self, component: impl ToString) -> Self {
158        self.labels([(label::APP_COMPONENT, component)])
159    }
160
161    /// Set recommended label 'app.kubernetes.io/part-of'
162    ///
163    fn app_part_of(self, part_of: impl ToString) -> Self {
164        self.labels([(label::APP_PART_OF, part_of)])
165    }
166
167    /// Set recommended label 'app.kubernetes.io/managed-by'
168    ///
169    fn app_managed_by(self, managed_by: impl ToString) -> Self {
170        self.labels([(label::APP_MANAGED_BY, managed_by)])
171    }
172}
173
174impl<T> ResourceBuilder for T
175where
176    T: openapi::Metadata<Ty = metav1::ObjectMeta>,
177{
178    fn namespace(mut self, namespace: impl ToString) -> Self {
179        let namespace = Some(namespace.to_string());
180        self.metadata_mut().namespace = namespace;
181        self
182    }
183
184    fn owner(mut self, owner: metav1::OwnerReference) -> Self {
185        self.metadata_mut()
186            .owner_references
187            .get_or_insert_default()
188            .push(owner);
189        self
190    }
191
192    fn label(mut self, key: impl ToString, value: impl ToString) -> Self {
193        self.metadata_mut()
194            .labels
195            .get_or_insert_default()
196            .insert(key.to_string(), value.to_string());
197        self
198    }
199
200    fn labels(mut self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
201        let labels = labels
202            .into_iter()
203            .map(|(key, value)| (key.to_string(), value.to_string()));
204        self.metadata_mut()
205            .labels
206            .get_or_insert_default()
207            .extend(labels);
208        self
209    }
210
211    fn annotation(mut self, key: impl ToString, value: impl ToString) -> Self {
212        self.metadata_mut()
213            .annotations
214            .get_or_insert_default()
215            .insert(key.to_string(), value.to_string());
216        self
217    }
218
219    fn annotations(
220        mut self,
221        annotations: impl IntoIterator<Item = (impl ToString, impl ToString)>,
222    ) -> Self {
223        let annotations = annotations
224            .into_iter()
225            .map(|(key, value)| (key.to_string(), value.to_string()));
226        self.metadata_mut()
227            .annotations
228            .get_or_insert_default()
229            .extend(annotations);
230        self
231    }
232
233    fn with_resource_version(mut self, resource_version: String) -> Self {
234        self.metadata_mut().resource_version = Some(resource_version);
235        self
236    }
237}
238
239fn metadata(name: impl ToString) -> metav1::ObjectMeta {
240    let name = Some(name.to_string());
241    metav1::ObjectMeta {
242        name,
243        // annotations: todo!(),
244        // creation_timestamp: todo!(),
245        // deletion_grace_period_seconds: todo!(),
246        // deletion_timestamp: todo!(),
247        // finalizers: todo!(),
248        // generate_name: todo!(),
249        // generation: todo!(),
250        // labels: todo!(),
251        // managed_fields: todo!(),
252        // namespace: todo!(),
253        // owner_references: todo!(),
254        // resource_version: todo!(),
255        // self_link: todo!(),
256        // uid: todo!(),
257        ..default()
258    }
259}
260
261trait HasSpec {
262    type Spec;
263    fn spec_mut(&mut self) -> &mut Self::Spec;
264}