k8s_openapi_ext/ext/
service_port.rs

1use super::*;
2
3pub trait ServicePortExt {
4    /// Construct new `ServicePort`
5    ///
6    fn new(name: impl ToString, port: impl Into<i32>) -> Self;
7
8    /// Set targetPort
9    ///
10    fn target_port(self, port: impl ToIntOrString) -> Self;
11
12    /// Set protocol
13    ///
14    fn protocol(self, protocol: impl ToString) -> Self;
15
16    /// Create TCP ServicePort
17    ///
18    fn tcp(name: impl ToString, port: impl Into<i32>) -> Self
19    where
20        Self: Sized,
21    {
22        Self::new(name, port).protocol("TCP")
23    }
24
25    /// Create UDP ServicePort
26    ///
27    fn udp(name: impl ToString, port: impl Into<i32>) -> Self
28    where
29        Self: Sized,
30    {
31        Self::new(name, port).protocol("UDP")
32    }
33
34    /// Create SCTP ServicePort
35    ///
36    fn sctp(name: impl ToString, port: impl Into<i32>) -> Self
37    where
38        Self: Sized,
39    {
40        Self::new(name, port).protocol("SCTP")
41    }
42}
43
44impl ServicePortExt for corev1::ServicePort {
45    fn new(name: impl ToString, port: impl Into<i32>) -> Self {
46        let name = Some(name.to_string());
47        let port = port.into();
48        Self {
49            name,
50            port,
51            // app_protocol: todo!(),
52            // node_port: todo!(),
53            // protocol: todo!(),
54            // target_port: todo!(),
55            ..default()
56        }
57    }
58
59    fn target_port(self, port: impl ToIntOrString) -> Self {
60        let target_port = Some(port.to_int_or_string());
61        Self {
62            target_port,
63            ..self
64        }
65    }
66
67    fn protocol(self, protocol: impl ToString) -> Self {
68        let protocol = Some(protocol.to_string());
69        Self { protocol, ..self }
70    }
71}