k8s_openapi_ext/ext/
container.rs

1use super::*;
2
3pub trait ContainerExt {
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_always(self) -> Self;
13
14    fn image_pull_policy_never(self) -> Self;
15
16    fn liveness_probe(self, probe: corev1::Probe) -> Self;
17
18    fn readiness_probe(self, probe: corev1::Probe) -> Self;
19
20    fn resource_limits(
21        self,
22        limits: impl IntoIterator<Item = (String, resource::Quantity)>,
23    ) -> Self;
24
25    fn resource_requests(
26        self,
27        requests: impl IntoIterator<Item = (String, resource::Quantity)>,
28    ) -> Self;
29
30    fn startup_probe(self, probe: corev1::Probe) -> Self;
31
32    fn volume_mounts(self, volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>) -> Self;
33}
34
35impl ContainerExt for corev1::Container {
36    fn new(name: impl ToString) -> Self {
37        let name = name.to_string();
38        Self {
39            name,
40            // args: todo!(),
41            // command: todo!(),
42            // env: todo!(),
43            // env_from: todo!(),
44            // image: todo!(),
45            // image_pull_policy: todo!(),
46            // lifecycle: todo!(),
47            // liveness_probe: todo!(),
48            // ports: todo!(),
49            // readiness_probe: todo!(),
50            // resources: todo!(),
51            // security_context: todo!(),
52            // startup_probe: todo!(),
53            // stdin: todo!(),
54            // stdin_once: todo!(),
55            // termination_message_path: todo!(),
56            // termination_message_policy: todo!(),
57            // tty: todo!(),
58            // volume_devices: todo!(),
59            // volume_mounts: todo!(),
60            // working_dir: todo!(),
61            ..default()
62        }
63    }
64
65    fn env(self, env: impl IntoIterator<Item = impl ToEnvVar>) -> Self {
66        let env = Some(
67            env.into_iter()
68                .map(|envvar| ToEnvVar::to_envvar(&envvar))
69                .collect(),
70        );
71        Self { env, ..self }
72    }
73
74    fn env_from(self, env: impl IntoIterator<Item = impl ToEnvFrom>) -> Self {
75        let env_from = Some(env.into_iter().map(ToEnvFrom::to_envfrom).collect());
76        Self { env_from, ..self }
77    }
78
79    fn image(self, image: impl ToString) -> Self {
80        let image = Some(image.to_string());
81        Self { image, ..self }
82    }
83
84    fn image_pull_policy_always(self) -> Self {
85        let image_pull_policy = Some(String::from("Always"));
86        Self {
87            image_pull_policy,
88            ..self
89        }
90    }
91
92    fn image_pull_policy_never(self) -> Self {
93        let image_pull_policy = Some(String::from("Never"));
94        Self {
95            image_pull_policy,
96            ..self
97        }
98    }
99
100    fn liveness_probe(mut self, probe: corev1::Probe) -> Self {
101        self.liveness_probe = Some(probe);
102        self
103    }
104
105    fn readiness_probe(mut self, probe: corev1::Probe) -> Self {
106        self.readiness_probe = Some(probe);
107        self
108    }
109
110    fn resource_limits(
111        mut self,
112        limits: impl IntoIterator<Item = (String, resource::Quantity)>,
113    ) -> Self {
114        self.resources
115            .get_or_insert_with(default)
116            .limits
117            .get_or_insert_with(default)
118            .extend(limits);
119        self
120    }
121
122    fn resource_requests(
123        mut self,
124        requests: impl IntoIterator<Item = (String, resource::Quantity)>,
125    ) -> Self {
126        self.resources
127            .get_or_insert_with(default)
128            .requests
129            .get_or_insert_with(default)
130            .extend(requests);
131
132        self
133    }
134
135    fn startup_probe(mut self, probe: corev1::Probe) -> Self {
136        self.startup_probe = Some(probe);
137        self
138    }
139
140    fn volume_mounts(
141        mut self,
142        volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>,
143    ) -> Self {
144        let volume_mounts = volume_mounts.into_iter().collect();
145        self.volume_mounts = Some(volume_mounts);
146        self
147    }
148}