k8s_openapi_ext/get/
pod.rs

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