k8s_openapi_ext/get/
service.rs

1use super::*;
2
3pub trait ServiceGetExt {
4    fn spec(&self) -> Option<&corev1::ServiceSpec>;
5    fn status(&self) -> Option<&corev1::ServiceStatus>;
6
7    fn cluster_ip(&self) -> Option<&str> {
8        self.spec()?.cluster_ip.as_deref()
9    }
10
11    fn cluster_ips(&self) -> Option<&[String]> {
12        self.spec()?.cluster_ips.as_deref()
13    }
14
15    fn ports(&self) -> Option<&[corev1::ServicePort]> {
16        self.spec()?.ports.as_deref()
17    }
18
19    fn port(&self, name: &str) -> Option<&corev1::ServicePort> {
20        self.ports()?
21            .iter()
22            .find(|port| port.name.as_deref() == Some(name))
23    }
24
25    fn external_name(&self) -> Option<&str> {
26        self.spec()?.external_name.as_deref()
27    }
28
29    fn external_ips(&self) -> Option<&[String]> {
30        self.spec()?.external_ips.as_deref()
31    }
32
33    fn selector(&self) -> Option<&BTreeMap<String, String>> {
34        self.spec()?.selector.as_ref()
35    }
36
37    fn r#type(&self) -> Option<&str> {
38        self.spec()?.type_.as_deref()
39    }
40
41    fn load_balancer_ingress(&self) -> Option<&[corev1::LoadBalancerIngress]> {
42        self.status()?
43            .load_balancer
44            .as_ref()
45            .and_then(|lb| lb.ingress.as_deref())
46    }
47
48    fn is_cluster_ip(&self) -> bool;
49
50    fn is_external_name(&self) -> bool;
51
52    fn is_node_port(&self) -> bool;
53
54    fn is_load_balancer(&self) -> bool;
55
56    fn conditions(&self) -> Option<&[metav1::Condition]> {
57        self.status()?.conditions.as_deref()
58    }
59}
60
61impl ServiceGetExt for corev1::Service {
62    fn spec(&self) -> Option<&corev1::ServiceSpec> {
63        self.spec.as_ref()
64    }
65
66    fn status(&self) -> Option<&corev1::ServiceStatus> {
67        self.status.as_ref()
68    }
69
70    fn is_cluster_ip(&self) -> bool {
71        self.r#type()
72            .is_none_or(|r#type| r#type == <Self as ServiceExt>::CLUSTER_IP)
73    }
74
75    fn is_external_name(&self) -> bool {
76        self.r#type()
77            .is_some_and(|r#type| r#type == <Self as ServiceExt>::EXTERNAL_NAME)
78    }
79
80    fn is_node_port(&self) -> bool {
81        self.r#type()
82            .is_some_and(|r#type| r#type == <Self as ServiceExt>::NODE_PORT)
83    }
84
85    fn is_load_balancer(&self) -> bool {
86        self.r#type()
87            .is_some_and(|r#type| r#type == <Self as ServiceExt>::LOAD_BALANCER)
88    }
89}