forked from kube-rs/kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_watcher.rs
37 lines (32 loc) · 1.28 KB
/
dynamic_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
use futures::prelude::*;
use kube::{
api::{DynamicObject, GroupVersionKind, ListParams, ResourceExt},
discovery, Api, Client,
};
use kube_runtime::{utils::try_flatten_applied, watcher};
use std::env;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
env_logger::init();
let client = Client::try_default().await?;
// Take dynamic resource identifiers:
let group = env::var("GROUP").unwrap_or_else(|_| "clux.dev".into());
let version = env::var("VERSION").unwrap_or_else(|_| "v1".into());
let kind = env::var("KIND").unwrap_or_else(|_| "Foo".into());
// Turn them into a GVK
let gvk = GroupVersionKind::gvk(&group, &version, &kind);
// Use API discovery to identify more information about the type (like its plural)
let (ar, _caps) = discovery::pinned_kind(&client, &gvk).await?;
// Use the discovered kind in an Api with the ApiResource as its DynamicType
let api = Api::<DynamicObject>::all_with(client, &ar);
// Fully compatible with kube-runtime
let watcher = watcher(api, ListParams::default());
try_flatten_applied(watcher)
.try_for_each(|p| async move {
log::info!("Applied: {}", p.name());
Ok(())
})
.await?;
Ok(())
}