-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathpod_watcher.rs
54 lines (51 loc) · 1.63 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
48
49
50
51
52
53
54
use futures::prelude::*;
use k8s_openapi::api::core::v1::Pod;
use kube::{
api::{Api, ResourceExt},
runtime::{watcher, WatchStreamExt},
Client,
};
use tracing::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let client = Client::try_default().await?;
let api = Api::<Pod>::default_namespaced(client);
let use_watchlist = std::env::var("WATCHLIST").map(|s| s == "1").unwrap_or(false);
let wc = if use_watchlist {
// requires WatchList feature gate on 1.27 or later
watcher::Config::default().streaming_lists()
} else {
watcher::Config::default()
};
watcher(api, wc)
.applied_objects()
.default_backoff()
.try_for_each(|p| async move {
info!("saw {}", p.name_any());
if let Some(unready_reason) = pod_unready(&p) {
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
.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_any(), failed));
}
}
None
}