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