forked from kube-rs/kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod_watcher.rs
47 lines (45 loc) · 1.52 KB
/
pod_watcher.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use color_eyre::Result;
use futures::prelude::*;
use k8s_openapi::api::core::v1::Pod;
use kube::{
api::{ListParams, ResourceExt},
Api, Client,
};
use kube_runtime::{utils::try_flatten_applied, watcher};
#[tokio::main]
async fn main() -> Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
env_logger::init();
let client = Client::try_default().await?;
let namespace = std::env::var("NAMESPACE").unwrap_or("default".into());
let api = Api::<Pod>::namespaced(client, &namespace);
let watcher = watcher(api, ListParams::default());
try_flatten_applied(watcher)
.try_for_each(|p| async move {
log::debug!("Applied: {}", p.name());
if let Some(unready_reason) = pod_unready(&p) {
log::warn!("{}", unready_reason);
}
Ok(())
})
.await?;
Ok(())
}
fn pod_unready(p: &Pod) -> Option<String> {
let status = p.status.as_ref().unwrap();
if let Some(conds) = &status.conditions {
let failed = conds
.into_iter()
.filter(|c| c.type_ == "Ready" && c.status == "False")
.map(|c| c.message.clone().unwrap_or_default())
.collect::<Vec<_>>()
.join(",");
if !failed.is_empty() {
if p.metadata.labels.as_ref().unwrap().contains_key("job-name") {
return None; // ignore job based pods, they are meant to exit 0
}
return Some(format!("Unready pod {}: {}", p.name(), failed));
}
}
None
}