-
-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathmulti_reflector.rs
More file actions
145 lines (132 loc) · 4.08 KB
/
Copy pathmulti_reflector.rs
File metadata and controls
145 lines (132 loc) · 4.08 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use futures::{future, stream, StreamExt};
use k8s_openapi::api::{
apps::v1::Deployment,
core::v1::{ConfigMap, Secret},
};
use kube::{
api::{ApiResource, DynamicObject, GroupVersionKind},
core::TypedResource,
runtime::{reflector::store::CacheWriter, watcher, WatchStreamExt},
Api, Client, Resource,
};
use parking_lot::RwLock;
use serde::de::DeserializeOwned;
use std::sync::Arc;
use tracing::*;
use std::collections::HashMap;
type Cache = Arc<RwLock<HashMap<LookupKey, Arc<DynamicObject>>>>;
#[derive(Default, Clone, Hash, PartialEq, Eq, Debug)]
struct LookupKey {
gvk: GroupVersionKind,
name: Option<String>,
namespace: Option<String>,
}
impl LookupKey {
fn new<R: TypedResource>(resource: &R) -> LookupKey {
let meta = resource.meta();
LookupKey {
gvk: resource.gvk(),
name: meta.name.clone(),
namespace: meta.namespace.clone(),
}
}
}
#[derive(Default, Clone)]
struct MultiCache {
store: Cache,
}
impl MultiCache {
fn get<K: Resource<DynamicType = impl Default> + DeserializeOwned + Clone>(
&self,
name: &str,
ns: &str,
) -> Option<Arc<K>> {
let obj = self
.store
.read()
.get(&LookupKey {
gvk: K::gvk(&Default::default()),
name: Some(name.into()),
namespace: if !ns.is_empty() { Some(ns.into()) } else { None },
})?
.as_ref()
.clone();
obj.try_parse().ok().map(Arc::new)
}
}
impl CacheWriter<DynamicObject> for MultiCache {
/// Applies a single watcher event to the store
fn apply_watcher_event(&mut self, event: &watcher::Event<DynamicObject>) {
match event {
watcher::Event::Init | watcher::Event::InitDone => {}
watcher::Event::Delete(obj) => {
self.store.write().remove(&LookupKey::new(obj));
}
watcher::Event::InitApply(obj) | watcher::Event::Apply(obj) => {
self.store
.write()
.insert(LookupKey::new(obj), Arc::new(obj.clone()));
}
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let client = Client::try_default().await?;
// multistore
let mut combo_stream = stream::select_all(vec![]);
combo_stream.push(
watcher::watcher(
Api::all_with(client.clone(), &ApiResource::erase::<Deployment>(&())),
Default::default(),
)
.boxed(),
);
combo_stream.push(
watcher::watcher(
Api::all_with(client.clone(), &ApiResource::erase::<ConfigMap>(&())),
Default::default(),
)
.boxed(),
);
// // Duplicate streams with narrowed down selection
combo_stream.push(
watcher::watcher(
Api::default_namespaced_with(client.clone(), &ApiResource::erase::<Secret>(&())),
Default::default(),
)
.boxed(),
);
combo_stream.push(
watcher::watcher(
Api::all_with(client.clone(), &ApiResource::erase::<Secret>(&())),
Default::default(),
)
.boxed(),
);
let multi_writer = MultiCache::default();
let watcher = combo_stream
.reflect(multi_writer.clone())
.applied_objects()
.for_each(|_| future::ready(()));
// simulate doing stuff with the stores from some other thread
tokio::spawn(async move {
// can use helper accessors
loop {
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
info!("cache content: {:?}", multi_writer.store.read().keys());
info!(
"common cm: {:?}",
multi_writer.get::<ConfigMap>("kube-root-ca.crt", "kube-system")
);
// access individual sub stores
info!("Current objects count: {}", multi_writer.store.read().len());
}
});
info!("long watches starting");
tokio::select! {
r = watcher => println!("watcher exit: {r:?}"),
}
Ok(())
}