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
use crate::utils::UseOrCreate;
use anyhow::Result;
use k8s_openapi::api::core::v1::{Container, ContainerPort};
pub trait ApplyPort {
fn apply_port<F, S>(&mut self, name: S, mutator: F) -> Result<()>
where
F: FnOnce(&mut ContainerPort) -> Result<()>,
S: AsRef<str>;
fn add_port<S>(&mut self, name: S, container_port: i32, protocol: Option<String>) -> Result<()>
where
S: AsRef<str>,
{
self.apply_port(name, |c| {
c.container_port = container_port;
c.protocol = protocol;
Ok(())
})
}
}
impl ApplyPort for Vec<ContainerPort> {
fn apply_port<F, S>(&mut self, name: S, mutator: F) -> Result<()>
where
F: FnOnce(&mut ContainerPort) -> Result<()>,
S: AsRef<str>,
{
let c = self.iter_mut().find(|c| match &c.name {
None => false,
Some(s) => s.as_str() == name.as_ref(),
});
match c {
Some(c) => {
mutator(c)?;
}
None => {
let mut port: ContainerPort = Default::default();
port.name = Some(name.as_ref().to_string());
mutator(&mut port)?;
self.push(port);
}
}
Ok(())
}
}
impl ApplyPort for Container {
fn apply_port<F, S>(&mut self, name: S, mutator: F) -> Result<()>
where
F: FnOnce(&mut ContainerPort) -> Result<()>,
S: AsRef<str>,
{
self.ports
.use_or_create(|ports| ports.apply_port(name, mutator))
}
}