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: 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: 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: 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: 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: i32) -> Self {
46        let name = Some(name.to_string());
47        Self {
48            name,
49            port,
50            // app_protocol: todo!(),
51            // node_port: todo!(),
52            // protocol: todo!(),
53            // target_port: todo!(),
54            ..default()
55        }
56    }
57
58    fn target_port(self, port: impl ToIntOrString) -> Self {
59        let target_port = Some(port.to_int_or_string());
60        Self {
61            target_port,
62            ..self
63        }
64    }
65
66    fn protocol(self, protocol: impl ToString) -> Self {
67        let protocol = Some(protocol.to_string());
68        Self { protocol, ..self }
69    }
70}