1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use super::*;

pub trait ContainerExt {
    fn new(name: impl ToString) -> Self;

    fn env(self, env: impl IntoIterator<Item = impl ToEnvVar>) -> Self;

    fn env_from(self, env: impl IntoIterator<Item = impl ToEnvFrom>) -> Self;

    fn image(self, image: impl ToString) -> Self;

    fn image_pull_policy_always(self) -> Self;

    fn image_pull_policy_never(self) -> Self;

    fn liveness_probe(self, probe: corev1::Probe) -> Self;

    fn readiness_probe(self, probe: corev1::Probe) -> Self;

    fn resource_limits(
        self,
        limits: impl IntoIterator<Item = (String, resource::Quantity)>,
    ) -> Self;

    fn resource_requests(
        self,
        requests: impl IntoIterator<Item = (String, resource::Quantity)>,
    ) -> Self;

    fn startup_probe(self, probe: corev1::Probe) -> Self;

    fn volume_mounts(self, volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>) -> Self;
}

impl ContainerExt for corev1::Container {
    fn new(name: impl ToString) -> Self {
        let name = name.to_string();
        Self {
            name,
            // args: todo!(),
            // command: todo!(),
            // env: todo!(),
            // env_from: todo!(),
            // image: todo!(),
            // image_pull_policy: todo!(),
            // lifecycle: todo!(),
            // liveness_probe: todo!(),
            // ports: todo!(),
            // readiness_probe: todo!(),
            // resources: todo!(),
            // security_context: todo!(),
            // startup_probe: todo!(),
            // stdin: todo!(),
            // stdin_once: todo!(),
            // termination_message_path: todo!(),
            // termination_message_policy: todo!(),
            // tty: todo!(),
            // volume_devices: todo!(),
            // volume_mounts: todo!(),
            // working_dir: todo!(),
            ..default()
        }
    }

    fn env(self, env: impl IntoIterator<Item = impl ToEnvVar>) -> Self {
        let env = Some(
            env.into_iter()
                .map(|envvar| ToEnvVar::to_envvar(&envvar))
                .collect(),
        );
        Self { env, ..self }
    }

    fn env_from(self, env: impl IntoIterator<Item = impl ToEnvFrom>) -> Self {
        let env_from = Some(env.into_iter().map(ToEnvFrom::to_envfrom).collect());
        Self { env_from, ..self }
    }

    fn image(self, image: impl ToString) -> Self {
        let image = Some(image.to_string());
        Self { image, ..self }
    }

    fn image_pull_policy_always(self) -> Self {
        let image_pull_policy = Some(String::from("Always"));
        Self {
            image_pull_policy,
            ..self
        }
    }

    fn image_pull_policy_never(self) -> Self {
        let image_pull_policy = Some(String::from("Never"));
        Self {
            image_pull_policy,
            ..self
        }
    }

    fn liveness_probe(mut self, probe: corev1::Probe) -> Self {
        self.liveness_probe = Some(probe);
        self
    }

    fn readiness_probe(mut self, probe: corev1::Probe) -> Self {
        self.readiness_probe = Some(probe);
        self
    }

    fn resource_limits(
        mut self,
        limits: impl IntoIterator<Item = (String, resource::Quantity)>,
    ) -> Self {
        self.resources
            .get_or_insert_with(default)
            .limits
            .get_or_insert_with(default)
            .extend(limits);
        self
    }

    fn resource_requests(
        mut self,
        requests: impl IntoIterator<Item = (String, resource::Quantity)>,
    ) -> Self {
        self.resources
            .get_or_insert_with(default)
            .requests
            .get_or_insert_with(default)
            .extend(requests);

        self
    }

    fn startup_probe(mut self, probe: corev1::Probe) -> Self {
        self.startup_probe = Some(probe);
        self
    }

    fn volume_mounts(
        mut self,
        volume_mounts: impl IntoIterator<Item = corev1::VolumeMount>,
    ) -> Self {
        let volume_mounts = volume_mounts.into_iter().collect();
        self.volume_mounts = Some(volume_mounts);
        self
    }
}