k8s_openapi_ext/ext/
container.rs1use super::*;
2
3pub trait ContainerExt: Sized {
4 fn new(name: impl ToString) -> Self;
5
6 fn env(self, env: impl IntoIterator<Item = impl ToEnvVar>) -> Self;
7
8 fn env_from(self, env: impl IntoIterator<Item = impl ToEnvFrom>) -> Self;
9
10 fn image(self, image: impl ToString) -> Self;
11
12 fn image_pull_policy(self, policy: impl ToString) -> Self;
13
14 fn image_pull_policy_always(self) -> Self {
15 self.image_pull_policy("Always")
16 }
17
18 fn image_pull_policy_never(self) -> Self {
19 self.image_pull_policy("Never")
20 }
21
22 fn ports(self, ports: impl IntoIterator<Item = corev1::ContainerPort>) -> Self;
23
24 fn security_context(self, security_context: corev1::SecurityContext) -> Self;
25
26 fn allow_privilege_escalation(mut self, yes: bool) -> Self {
27 self.security_context_mut().allow_privilege_escalation = Some(yes);
28 self
29 }
30
31 fn run_as_user(mut self, user: i64) -> Self {
32 self.security_context_mut().run_as_user = Some(user);
33 self
34 }
35
36 fn run_as_group(mut self, group: i64) -> Self {
37 self.security_context_mut().run_as_group = Some(group);
38 self
39 }
40
41 fn run_as_non_root(mut self, yes: bool) -> Self {
42 self.security_context_mut().run_as_non_root = Some(yes);
43 self
44 }
45
46 fn privileged(mut self, yes: bool) -> Self {
47 self.security_context_mut().privileged = Some(yes);
48 self
49 }
50
51 fn liveness_probe(self, probe: corev1::Probe) -> Self;
52
53 fn readiness_probe(self, probe: corev1::Probe) -> Self;
54
55 fn resource_limits(
56 self,
57 limits: impl IntoIterator<Item = (String, resource::Quantity)>,
58 ) -> Self;
59
60 fn resource_requests(
61 self,
62 requests: impl IntoIterator<Item = (String, resource::Quantity)>,
63 ) -> Self;
64
65 fn startup_probe(self, probe: corev1::Probe) -> Self;
66
67 fn volume_mounts(self, volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>) -> Self;
68
69 fn security_context_mut(&mut self) -> &mut corev1::SecurityContext;
70}
71
72impl ContainerExt for corev1::Container {
73 fn new(name: impl ToString) -> Self {
74 let name = name.to_string();
75 Self {
76 name,
77 ..default()
99 }
100 }
101
102 fn env(self, env: impl IntoIterator<Item = impl ToEnvVar>) -> Self {
103 let env = Some(
104 env.into_iter()
105 .map(|envvar| ToEnvVar::to_envvar(&envvar))
106 .collect(),
107 );
108 Self { env, ..self }
109 }
110
111 fn env_from(self, env: impl IntoIterator<Item = impl ToEnvFrom>) -> Self {
112 let env_from = Some(env.into_iter().map(ToEnvFrom::to_envfrom).collect());
113 Self { env_from, ..self }
114 }
115
116 fn image(self, image: impl ToString) -> Self {
117 let image = Some(image.to_string());
118 Self { image, ..self }
119 }
120
121 fn image_pull_policy(self, policy: impl ToString) -> Self {
122 let image_pull_policy = Some(policy.to_string());
123 Self {
124 image_pull_policy,
125 ..self
126 }
127 }
128
129 fn ports(self, ports: impl IntoIterator<Item = corev1::ContainerPort>) -> Self {
130 let ports = Some(ports.into_iter().collect());
131 Self { ports, ..self }
132 }
133
134 fn security_context(self, security_context: corev1::SecurityContext) -> Self {
135 Self {
136 security_context: Some(security_context),
137 ..self
138 }
139 }
140
141 fn liveness_probe(mut self, probe: corev1::Probe) -> Self {
142 self.liveness_probe = Some(probe);
143 self
144 }
145
146 fn readiness_probe(mut self, probe: corev1::Probe) -> Self {
147 self.readiness_probe = Some(probe);
148 self
149 }
150
151 fn resource_limits(
152 mut self,
153 limits: impl IntoIterator<Item = (String, resource::Quantity)>,
154 ) -> Self {
155 self.resources
156 .get_or_insert_default()
157 .limits
158 .get_or_insert_default()
159 .extend(limits);
160 self
161 }
162
163 fn resource_requests(
164 mut self,
165 requests: impl IntoIterator<Item = (String, resource::Quantity)>,
166 ) -> Self {
167 self.resources
168 .get_or_insert_default()
169 .requests
170 .get_or_insert_default()
171 .extend(requests);
172 self
173 }
174
175 fn startup_probe(mut self, probe: corev1::Probe) -> Self {
176 self.startup_probe = Some(probe);
177 self
178 }
179
180 fn volume_mounts(
181 mut self,
182 volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>,
183 ) -> Self {
184 let volume_mounts = volume_mounts.into_iter().collect();
185 self.volume_mounts = Some(volume_mounts);
186 self
187 }
188
189 fn security_context_mut(&mut self) -> &mut corev1::SecurityContext {
190 self.security_context.get_or_insert_default()
191 }
192}