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    fn readiness_gates(&self) -> Option<&[corev1::PodReadinessGate]> {
38        self.spec()?.readiness_gates.as_deref()
39    }
40
41    // From status
42    fn message(&self) -> Option<&str> {
43        self.status()?.message.as_deref()
44    }
45
46    fn phase(&self) -> Option<&str> {
47        self.status()?.phase.as_deref()
48    }
49
50    fn qos_class(&self) -> Option<&str> {
51        self.status()?.qos_class.as_deref()
52    }
53
54    fn reason(&self) -> Option<&str> {
55        self.status()?.reason.as_deref()
56    }
57
58    fn resize(&self) -> Option<&str> {
59        self.status()?.resize.as_deref()
60    }
61
62    fn conditions(&self) -> Option<&[corev1::PodCondition]> {
63        self.status()?.conditions.as_deref()
64    }
65
66    fn container_statuses(&self) -> Option<&[corev1::ContainerStatus]> {
67        self.status()?.container_statuses.as_deref()
68    }
69
70    fn ephemeral_container_statuses(&self) -> Option<&[corev1::ContainerStatus]> {
71        self.status()?.ephemeral_container_statuses.as_deref()
72    }
73
74    fn init_container_statuses(&self) -> Option<&[corev1::ContainerStatus]> {
75        self.status()?.init_container_statuses.as_deref()
76    }
77
78    fn nominated_node_name(&self) -> Option<&str> {
79        self.status()?.nominated_node_name.as_deref()
80    }
81
82    fn resource_claim_statuses(&self) -> Option<&[corev1::PodResourceClaimStatus]> {
83        self.status()?.resource_claim_statuses.as_deref()
84    }
85
86    fn host_ip(&self) -> Option<&str> {
87        self.status()?.host_ip.as_deref()
88    }
89
90    fn host_ips(&self) -> Option<&[corev1::HostIP]> {
91        self.status()?.host_ips.as_deref()
92    }
93
94    fn pod_ip(&self) -> Option<&str> {
95        self.status()?.pod_ip.as_deref()
96    }
97
98    fn pod_ips(&self) -> Option<&[corev1::PodIP]> {
99        self.status()?.pod_ips.as_deref()
100    }
101
102    fn start_time(&self) -> Option<&metav1::Time> {
103        self.status()?.start_time.as_ref()
104    }
105
106    fn condition(&self, type_: &str) -> Option<&corev1::PodCondition> {
107        self.conditions()?
108            .iter()
109            .find(|condition| condition.type_ == type_)
110    }
111
112    fn is_ready(&self) -> bool {
113        let gates = self
114            .readiness_gates()
115            .unwrap_or_default()
116            .iter()
117            .all(|gate| {
118                self.condition(&gate.condition_type)
119                    .is_some_and(|condition| condition.is_true())
120            });
121
122        let ready = self
123            .condition("Ready")
124            .is_some_and(|condition| condition.is_true());
125
126        gates && ready
127    }
128}
129
130impl PodGetExt for corev1::Pod {
131    fn spec(&self) -> Option<&corev1::PodSpec> {
132        self.spec.as_ref()
133    }
134
135    fn status(&self) -> Option<&corev1::PodStatus> {
136        self.status.as_ref()
137    }
138}
139
140impl PodGetExt for corev1::PodStatus {
141    fn spec(&self) -> Option<&corev1::PodSpec> {
142        None
143    }
144
145    fn status(&self) -> Option<&corev1::PodStatus> {
146        Some(self)
147    }
148}
149
150impl PodGetExt for corev1::PodTemplate {
151    fn spec(&self) -> Option<&corev1::PodSpec> {
152        self.template.as_ref()?.spec.as_ref()
153    }
154
155    fn status(&self) -> Option<&corev1::PodStatus> {
156        None
157    }
158}
159
160impl PodGetExt for corev1::PodTemplateSpec {
161    fn spec(&self) -> Option<&corev1::PodSpec> {
162        self.spec.as_ref()
163    }
164
165    fn status(&self) -> Option<&corev1::PodStatus> {
166        None
167    }
168}