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