forked from kube-rs/kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod_reflector.rs
52 lines (47 loc) · 1.57 KB
/
pod_reflector.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
#[macro_use] extern crate log;
use futures_timer::Delay;
use k8s_openapi::api::core::v1::Pod;
use kube::{
api::{ListParams, Meta, Resource},
client::APIClient,
config,
runtime::Reflector,
};
use std::time::Duration;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
env_logger::init();
let config = config::load_kube_config().await?;
let client = APIClient::new(config);
let namespace = std::env::var("NAMESPACE").unwrap_or("default".into());
let resource = Resource::namespaced::<Pod>(&namespace);
let lp = ListParams::default().timeout(10); // short watch timeout in this example
let rf: Reflector<Pod> = Reflector::new(client, lp, resource).init().await?;
// Can read initial state now:
rf.state().await?.into_iter().for_each(|pod| {
let name = Meta::name(&pod);
let phase = pod.status.unwrap().phase.unwrap();
let containers = pod
.spec
.unwrap()
.containers
.into_iter()
.map(|c| c.name)
.collect::<Vec<_>>();
info!("Found initial pod {} ({}) with {:?}", name, phase, containers);
});
let cloned = rf.clone();
tokio::spawn(async move {
loop {
if let Err(e) = cloned.poll().await {
warn!("Poll error: {:?}", e);
}
}
});
loop {
Delay::new(Duration::from_secs(5)).await;
let pods: Vec<_> = rf.state().await?.iter().map(Meta::name).collect();
info!("Current pods: {:?}", pods);
}
}