k8s_openapi_ext/ext/
container_port.rs1use super::*;
2
3pub trait ContainerPortExt: Sized {
4 fn new(port: i32, protocol: impl ToString) -> Self;
5
6 fn tcp(port: i32) -> Self {
7 Self::new(port, "TCP")
8 }
9
10 fn udp(port: i32) -> Self {
11 Self::new(port, "UDP")
12 }
13
14 fn sctp(port: i32) -> Self {
15 Self::new(port, "SCTP")
16 }
17
18 fn name(self, name: impl ToString) -> Self;
19
20 fn host_ip(self, ip: impl ToString) -> Self;
21
22 fn host_port(self, port: i32) -> Self;
23}
24
25impl ContainerPortExt for corev1::ContainerPort {
26 fn new(port: i32, protocol: impl ToString) -> Self {
27 let protocol = Some(protocol.to_string());
28 Self {
29 container_port: port,
30 protocol,
31 ..default()
35 }
36 }
37
38 fn name(self, name: impl ToString) -> Self {
39 let name = Some(name.to_string());
40 Self { name, ..self }
41 }
42
43 fn host_ip(self, ip: impl ToString) -> Self {
44 let host_ip = Some(ip.to_string());
45 Self { host_ip, ..self }
46 }
47
48 fn host_port(self, port: i32) -> Self {
49 let host_port = Some(port);
50 Self { host_port, ..self }
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn host_ip() {
60 let ip: std::net::IpAddr = "127.0.0.1".parse().unwrap();
61 let port = corev1::ContainerPort::tcp(1234).host_ip(ip);
62 assert_eq!(port.host_ip.as_deref(), Some("127.0.0.1"));
63 }
64}