k8s_openapi_ext/ext/
pod_spec.rs

1use super::*;
2
3pub trait PodSpecExt {
4    /// Build `corev1::PodSpec` with this container
5    ///
6    fn container(container: corev1::Container) -> Self;
7
8    /// Build `corev1::PodSpec` with these containers
9    ///
10    fn containers(containers: impl IntoIterator<Item = corev1::Container>) -> Self;
11
12    /// Set service account name
13    ///
14    fn service_account_name(self, name: impl ToString) -> Self;
15
16    /// Add image pull secret
17    ///
18    fn image_pull_secret(self, name: impl ToString) -> Self;
19
20    /// Add `volumes`
21    ///
22    fn volumes(self, volumes: impl IntoIterator<Item = corev1::Volume>) -> Self;
23
24    /// Add node selector
25    ///
26    fn node_selector(
27        self,
28        node_selector: impl IntoIterator<Item = (impl ToString, impl ToString)>,
29    ) -> Self;
30
31    /// Set affinity
32    ///
33    fn affinity(self, affinity: impl Into<Option<corev1::Affinity>>) -> Self;
34
35    /// Add tolerations
36    ///
37    fn tolerations(self, tolerations: impl IntoIterator<Item = corev1::Toleration>) -> Self;
38}
39
40impl PodSpecExt for corev1::PodSpec {
41    fn container(container: corev1::Container) -> Self {
42        let containers = vec![container];
43        Self {
44            containers,
45            // active_deadline_seconds: todo!(),
46            // affinity: todo!(),
47            // automount_service_account_token: todo!(),
48            // dns_config: todo!(),
49            // dns_policy: todo!(),
50            // enable_service_links: todo!(),
51            // ephemeral_containers: todo!(),
52            // host_aliases: todo!(),
53            // host_ipc: todo!(),
54            // host_network: todo!(),
55            // host_pid: todo!(),
56            // hostname: todo!(),
57            // image_pull_secrets: todo!(),
58            // init_containers: todo!(),
59            // node_name: todo!(),
60            // node_selector: todo!(),
61            // overhead: todo!(),
62            // preemption_policy: todo!(),
63            // priority: todo!(),
64            // priority_class_name: todo!(),
65            // readiness_gates: todo!(),
66            // restart_policy: todo!(),
67            // runtime_class_name: todo!(),
68            // scheduler_name: todo!(),
69            // security_context: todo!(),
70            // service_account: todo!(),
71            // service_account_name: todo!(),
72            // set_hostname_as_fqdn: todo!(),
73            // share_process_namespace: todo!(),
74            // subdomain: todo!(),
75            // termination_grace_period_seconds: todo!(),
76            // tolerations: todo!(),
77            // topology_spread_constraints: todo!(),
78            // volumes: todo!(),
79            ..default()
80        }
81    }
82
83    fn containers(containers: impl IntoIterator<Item = corev1::Container>) -> Self {
84        let containers = Vec::from_iter(containers);
85        Self {
86            containers,
87            ..default()
88        }
89    }
90
91    fn service_account_name(self, name: impl ToString) -> Self {
92        let service_account_name = Some(name.to_string());
93        Self {
94            service_account_name,
95            ..self
96        }
97    }
98
99    fn image_pull_secret(mut self, name: impl ToString) -> Self {
100        let secret = corev1::LocalObjectReference::new(name);
101        self.image_pull_secrets.get_or_insert_default().push(secret);
102        self
103    }
104
105    fn volumes(mut self, volumes: impl IntoIterator<Item = corev1::Volume>) -> Self {
106        self.volumes.get_or_insert_default().extend(volumes);
107        self
108    }
109
110    fn node_selector(
111        mut self,
112        node_selector: impl IntoIterator<Item = (impl ToString, impl ToString)>,
113    ) -> Self {
114        let node_selector = node_selector
115            .into_iter()
116            .map(|(key, value)| (key.to_string(), value.to_string()));
117        self.node_selector
118            .get_or_insert_default()
119            .extend(node_selector);
120        self
121    }
122
123    fn affinity(self, affinity: impl Into<Option<corev1::Affinity>>) -> Self {
124        let affinity = affinity.into();
125        Self { affinity, ..self }
126    }
127
128    fn tolerations(mut self, tolerations: impl IntoIterator<Item = corev1::Toleration>) -> Self {
129        self.tolerations.get_or_insert_default().extend(tolerations);
130        self
131    }
132}