k8s_openapi_ext/get/
pod.rs

1use super::*;
2
3pub use condition::PodConditionGetExt;
4
5mod condition;
6
7pub trait PodGetExt {
8    fn spec(&self) -> Option<&corev1::PodSpec>;
9
10    fn status(&self) -> Option<&corev1::PodStatus>;
11
12    // From spec
13    fn containers(&self) -> Option<&[corev1::Container]> {
14        self.spec().map(|spec| spec.containers.as_ref())
15    }
16
17    fn ephemeral_containers(&self) -> Option<&[corev1::EphemeralContainer]> {
18        self.spec()?.ephemeral_containers.as_deref()
19    }
20
21    fn init_containers(&self) -> Option<&[corev1::Container]> {
22        self.spec()?.init_containers.as_deref()
23    }
24
25    fn node_selector(&self) -> Option<&BTreeMap<String, String>> {
26        self.spec()?.node_selector.as_ref()
27    }
28
29    fn resource_claims(&self) -> Option<&[corev1::PodResourceClaim]> {
30        self.spec()?.resource_claims.as_deref()
31    }
32
33    fn tolerations(&self) -> Option<&[corev1::Toleration]> {
34        self.spec()?.tolerations.as_deref()
35    }
36
37    // From status
38    fn message(&self) -> Option<&str> {
39        self.status()?.message.as_deref()
40    }
41
42    fn phase(&self) -> Option<&str> {
43        self.status()?.phase.as_deref()
44    }
45
46    fn qos_class(&self) -> Option<&str> {
47        self.status()?.qos_class.as_deref()
48    }
49
50    fn reason(&self) -> Option<&str> {
51        self.status()?.reason.as_deref()
52    }
53
54    fn resize(&self) -> Option<&str> {
55        self.status()?.resize.as_deref()
56    }
57
58    fn conditions(&self) -> Option<&[corev1::PodCondition]> {
59        self.status()?.conditions.as_deref()
60    }
61
62    fn container_statuses(&self) -> Option<&[corev1::ContainerStatus]> {
63        self.status()?.container_statuses.as_deref()
64    }
65
66    fn ephemeral_container_statuses(&self) -> Option<&[corev1::ContainerStatus]> {
67        self.status()?.ephemeral_container_statuses.as_deref()
68    }
69
70    fn init_container_statuses(&self) -> Option<&[corev1::ContainerStatus]> {
71        self.status()?.init_container_statuses.as_deref()
72    }
73
74    fn nominated_node_name(&self) -> Option<&str> {
75        self.status()?.nominated_node_name.as_deref()
76    }
77
78    fn resource_claim_statuses(&self) -> Option<&[corev1::PodResourceClaimStatus]> {
79        self.status()?.resource_claim_statuses.as_deref()
80    }
81
82    fn host_ip(&self) -> Option<&str> {
83        self.status()?.host_ip.as_deref()
84    }
85
86    fn host_ips(&self) -> Option<&[corev1::HostIP]> {
87        self.status()?.host_ips.as_deref()
88    }
89
90    fn pod_ip(&self) -> Option<&str> {
91        self.status()?.pod_ip.as_deref()
92    }
93
94    fn pod_ips(&self) -> Option<&[corev1::PodIP]> {
95        self.status()?.pod_ips.as_deref()
96    }
97
98    fn start_time(&self) -> Option<&metav1::Time> {
99        self.status()?.start_time.as_ref()
100    }
101
102    fn condition(&self, type_: &str) -> Option<&corev1::PodCondition> {
103        self.conditions()?
104            .iter()
105            .find(|condition| condition.type_ == type_)
106    }
107
108    fn is_ready(&self) -> bool {
109        self.condition("Ready")
110            .is_some_and(|condition| condition.is_true())
111    }
112}
113
114impl PodGetExt for corev1::Pod {
115    fn spec(&self) -> Option<&corev1::PodSpec> {
116        self.spec.as_ref()
117    }
118
119    fn status(&self) -> Option<&corev1::PodStatus> {
120        self.status.as_ref()
121    }
122}
123
124impl PodGetExt for corev1::PodStatus {
125    fn spec(&self) -> Option<&corev1::PodSpec> {
126        None
127    }
128
129    fn status(&self) -> Option<&corev1::PodStatus> {
130        Some(self)
131    }
132}
133
134impl PodGetExt for corev1::PodTemplate {
135    fn spec(&self) -> Option<&corev1::PodSpec> {
136        self.template.as_ref()?.spec.as_ref()
137    }
138
139    fn status(&self) -> Option<&corev1::PodStatus> {
140        None
141    }
142}
143
144impl PodGetExt for corev1::PodTemplateSpec {
145    fn spec(&self) -> Option<&corev1::PodSpec> {
146        self.spec.as_ref()
147    }
148
149    fn status(&self) -> Option<&corev1::PodStatus> {
150        None
151    }
152}