Skip to main content

k8s_openapi_ext/ext/
container.rs

1use super::*;
2
3pub trait ContainerExt: Sized {
4    fn new(name: impl ToString) -> Self;
5
6    /// Add `args` to container args list
7    fn args(self, args: impl IntoIterator<Item = impl ToString>) -> Self;
8
9    fn command(self, command: impl IntoIterator<Item = impl ToString>) -> Self;
10
11    /// Add environment variables to the container environment list
12    ///
13    fn env(self, env: impl IntoIterator<Item = impl ToEnvVar>) -> Self;
14
15    /// Add environment variables to the container environment list
16    ///
17    fn env_from(self, env: impl IntoIterator<Item = impl ToEnvFrom>) -> Self;
18
19    fn image(self, image: impl ToString) -> Self;
20
21    fn image_pull_policy(self, policy: impl ToString) -> Self;
22
23    fn image_pull_policy_always(self) -> Self {
24        self.image_pull_policy("Always")
25    }
26
27    fn image_pull_policy_never(self) -> Self {
28        self.image_pull_policy("Never")
29    }
30
31    /// Add `ports` to container ports list
32    ///
33    fn ports(self, ports: impl IntoIterator<Item = corev1::ContainerPort>) -> Self;
34
35    fn security_context(self, security_context: corev1::SecurityContext) -> Self;
36
37    fn allow_privilege_escalation(mut self, yes: bool) -> Self {
38        self.security_context_mut().allow_privilege_escalation = Some(yes);
39        self
40    }
41
42    fn read_only_root_filesystem(mut self, yes: bool) -> Self {
43        self.security_context_mut().read_only_root_filesystem = Some(yes);
44        self
45    }
46
47    fn run_as_user(mut self, user: i64) -> Self {
48        self.security_context_mut().run_as_user = Some(user);
49        self
50    }
51
52    fn run_as_group(mut self, group: i64) -> Self {
53        self.security_context_mut().run_as_group = Some(group);
54        self
55    }
56
57    fn run_as_non_root(mut self, yes: bool) -> Self {
58        self.security_context_mut().run_as_non_root = Some(yes);
59        self
60    }
61
62    fn privileged(mut self, yes: bool) -> Self {
63        self.security_context_mut().privileged = Some(yes);
64        self
65    }
66
67    /// Add `capabilities` to 'add' list
68    ///
69    fn add_capabilities(mut self, capabilities: impl IntoIterator<Item = impl ToString>) -> Self {
70        let add = capabilities.into_iter().map(|item| item.to_string());
71        self.security_context_mut()
72            .capabilities
73            .get_or_insert_default()
74            .add
75            .get_or_insert_default()
76            .extend(add);
77        self
78    }
79
80    /// Add `capabilities` to 'drop' list
81    ///
82    fn drop_capabilities(mut self, capabilities: impl IntoIterator<Item = impl ToString>) -> Self {
83        let drop = capabilities.into_iter().map(|item| item.to_string());
84        self.security_context_mut()
85            .capabilities
86            .get_or_insert_default()
87            .drop
88            .get_or_insert_default()
89            .extend(drop);
90        self
91    }
92
93    fn liveness_probe(self, probe: corev1::Probe) -> Self;
94
95    fn readiness_probe(self, probe: corev1::Probe) -> Self;
96
97    fn resource_limits(
98        self,
99        limits: impl IntoIterator<Item = (String, resource::Quantity)>,
100    ) -> Self;
101
102    fn resource_requests(
103        self,
104        requests: impl IntoIterator<Item = (String, resource::Quantity)>,
105    ) -> Self;
106
107    fn startup_probe(self, probe: corev1::Probe) -> Self;
108
109    fn volume_mounts(self, volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>) -> Self;
110
111    fn security_context_mut(&mut self) -> &mut corev1::SecurityContext;
112
113    fn working_dir(self, dir: impl ToString) -> Self;
114}
115
116impl ContainerExt for corev1::Container {
117    fn new(name: impl ToString) -> Self {
118        let name = name.to_string();
119        Self {
120            name,
121            // args: todo!(),
122            // command: todo!(),
123            // env: todo!(),
124            // env_from: todo!(),
125            // image: todo!(),
126            // image_pull_policy: todo!(),
127            // lifecycle: todo!(),
128            // liveness_probe: todo!(),
129            // ports: todo!(),
130            // readiness_probe: todo!(),
131            // resources: todo!(),
132            // security_context: todo!(),
133            // startup_probe: todo!(),
134            // stdin: todo!(),
135            // stdin_once: todo!(),
136            // termination_message_path: todo!(),
137            // termination_message_policy: todo!(),
138            // tty: todo!(),
139            // volume_devices: todo!(),
140            // volume_mounts: todo!(),
141            // working_dir: todo!(),
142            ..default()
143        }
144    }
145
146    fn args(mut self, args: impl IntoIterator<Item = impl ToString>) -> Self {
147        let args = args.into_iter().map(|item| item.to_string());
148        self.args.get_or_insert_default().extend(args);
149        self
150    }
151
152    fn command(self, command: impl IntoIterator<Item = impl ToString>) -> Self {
153        let command = command.into_iter().map(|item| item.to_string()).collect();
154        Self {
155            command: Some(command),
156            ..self
157        }
158    }
159
160    fn env(mut self, env: impl IntoIterator<Item = impl ToEnvVar>) -> Self {
161        let env = env.into_iter().map(|envvar| ToEnvVar::to_envvar(&envvar));
162        self.env.get_or_insert_default().extend(env);
163        self
164    }
165
166    fn env_from(mut self, env: impl IntoIterator<Item = impl ToEnvFrom>) -> Self {
167        let env = env.into_iter().map(ToEnvFrom::to_envfrom);
168        self.env_from.get_or_insert_default().extend(env);
169        self
170    }
171
172    fn image(self, image: impl ToString) -> Self {
173        let image = Some(image.to_string());
174        Self { image, ..self }
175    }
176
177    fn image_pull_policy(self, policy: impl ToString) -> Self {
178        let image_pull_policy = Some(policy.to_string());
179        Self {
180            image_pull_policy,
181            ..self
182        }
183    }
184
185    fn ports(mut self, ports: impl IntoIterator<Item = corev1::ContainerPort>) -> Self {
186        self.ports.get_or_insert_default().extend(ports);
187        self
188    }
189
190    fn security_context(self, security_context: corev1::SecurityContext) -> Self {
191        Self {
192            security_context: Some(security_context),
193            ..self
194        }
195    }
196
197    fn liveness_probe(mut self, probe: corev1::Probe) -> Self {
198        self.liveness_probe = Some(probe);
199        self
200    }
201
202    fn readiness_probe(mut self, probe: corev1::Probe) -> Self {
203        self.readiness_probe = Some(probe);
204        self
205    }
206
207    fn resource_limits(
208        mut self,
209        limits: impl IntoIterator<Item = (String, resource::Quantity)>,
210    ) -> Self {
211        self.resources
212            .get_or_insert_default()
213            .limits
214            .get_or_insert_default()
215            .extend(limits);
216        self
217    }
218
219    fn resource_requests(
220        mut self,
221        requests: impl IntoIterator<Item = (String, resource::Quantity)>,
222    ) -> Self {
223        self.resources
224            .get_or_insert_default()
225            .requests
226            .get_or_insert_default()
227            .extend(requests);
228        self
229    }
230
231    fn startup_probe(mut self, probe: corev1::Probe) -> Self {
232        self.startup_probe = Some(probe);
233        self
234    }
235
236    fn volume_mounts(
237        mut self,
238        volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>,
239    ) -> Self {
240        let volume_mounts = volume_mounts.into_iter().collect();
241        self.volume_mounts = Some(volume_mounts);
242        self
243    }
244
245    fn security_context_mut(&mut self) -> &mut corev1::SecurityContext {
246        self.security_context.get_or_insert_default()
247    }
248
249    fn working_dir(self, dir: impl ToString) -> Self {
250        let working_dir = Some(dir.to_string());
251        Self {
252            working_dir,
253            ..self
254        }
255    }
256}