-
|
Is this the right way to check if a container is running? thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
|
It may be better to avoid use k8s_openapi::api::core::v1 as corev1;
fn container_running(pod: &corev1::Pod, name: &str) -> Option<&corev1::ContainerStateRunning> {
pod.status
.as_ref()?
.container_statuses
.as_deref()?
.iter()
.find(|container| container.name == name)?
.state
.as_ref()?
.running
.as_ref()
}
fn container_is_running(pod: &corev1::Pod, name: &str) -> bool {
container_running(pod, name).is_some()
} |
Beta Was this translation helpful? Give feedback.
-
|
Iterating over container status as is done in the main post and imp's answer should both work. Do note that we already expose the conditions can be used with an but if you are looking for one or all containers specifically in that same interface we have a setup like this in test code; fn is_each_container_ready() -> impl Condition<Pod> {
|obj: Option<&Pod>| {
if let Some(o) = obj {
if let Some(s) = &o.status {
if let Some(conds) = &s.conditions {
if let Some(pcond) = conds.iter().find(|c| c.type_ == "ContainersReady") {
return pcond.status == "True";
}
}
}
}
false
}
}not exposed yet because there were some uncertainty/laziness around the direction of the conditions module w.r.t. let-chains (which would obviously simplify the look of this code). See #1792 |
Beta Was this translation helpful? Give feedback.
Iterating over container status as is done in the main post and imp's answer should both work.
Do note that we already expose the
Conditioninterface for many of these use cases. In particularis_pod_runningis similar.conditions can be used with an
await_condition(if you are waiting for a pod to become ready) or you can pass it anOption<&Pod>that you have at hand (if you just want to check).but if you are looking for one or all containers specifically in that same interface we have a setup like this in test code;