forked from kube-rs/kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrd_watcher.rs
169 lines (128 loc) · 5.28 KB
/
crd_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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::sync::Arc;
use anyhow::{Result};
use garde::Validate;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::time::Duration;
use tokio::time::sleep;
use tracing::*;
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use kube::runtime::watcher::Config;
use kube::{
api::{Api, Patch, PatchParams, ResourceExt},
core::crd::CustomResourceExt,
Client, CustomResource,
};
use kube::runtime::Controller;
use kube::runtime::controller::Action;
use kube::runtime::reflector::ObjectRef;
use futures::{FutureExt, StreamExt};
use tokio::time;
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, Validate, JsonSchema)]
#[kube(group = "clux.dev", version = "v1", kind = "MainThing", namespaced)]
pub struct MainThingSpec {}
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, Validate, JsonSchema)]
#[kube(group = "clux.dev", version = "v1", kind = "RefererThing", namespaced)]
pub struct RefererThingSpec {
#[garde(skip)]
pub main_thing_name: String,
#[garde(skip)]
pub main_thing_namespace: Option<String>,
#[garde(skip)]
pub value: i32,
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
ensure_crds().await?;
for i in 0..100 {
run_iteration(i).await?;
}
Ok(())
}
async fn run_iteration(large_iteration_index: i32) -> Result<()> {
let client = Client::try_default().await?;
let main_thing_api: Api<MainThing> = Api::all(client.clone());
let referer_thing_api: Api<RefererThing> = Api::all(client.clone());
let mut stream = Box::pin(Controller::new(main_thing_api.clone(), Config::default())
.watches(referer_thing_api, Config::default(), |o| {
let current_namespace = o.namespace().expect("referer thing should always be namespaced");
let object_ref = ObjectRef::new(&o.spec.main_thing_name).within(o.spec.main_thing_namespace.as_deref().unwrap_or(¤t_namespace));
Some(object_ref)
})
.run(
reconcile_main_thing,
error_policy, Arc::new(())));
let now = time::Instant::now();
while now + Duration::from_secs(1) > time::Instant::now() {
stream.next().now_or_never();
sleep(Duration::from_millis(1)).await;
}
info!("Ready for large iteration {large_iteration_index}!");
for i in 1..10 {
let referer_thing_api: Api<RefererThing> = Api::default_namespaced(client.clone());
let updated_referer = json!({
"spec": {
"value": i
}
});
referer_thing_api.patch("my-referer", &PatchParams::default(), &Patch::Merge(updated_referer)).await?;
info!("updated referer resource {i} in {large_iteration_index}");
let _ = stream.next().await.unwrap();
info!("Handled updated reconciliation");
}
info!("Finished large iteration {large_iteration_index}");
Ok(())
}
async fn reconcile_main_thing(resource: Arc<MainThing>, _context: Arc<()>) -> Result<Action, Error> {
info!("Reconciling {}", resource.name_any());
sleep(Duration::from_millis(1)).await;
Ok(Action::await_change())
}
fn error_policy(_resource: Arc<MainThing>, _error: &Error, _context: Arc<()>) -> Action {
Action::requeue(Duration::from_secs(15))
}
/// All errors possible to occur during reconciliation
#[derive(thiserror::Error, Debug)]
#[error(transparent)]
pub struct Error(#[from] anyhow::Error);
async fn ensure_crds() -> Result<()> {
let client = Client::try_default().await?;
// Manage CRDs first
let crds: Api<CustomResourceDefinition> = Api::all(client.clone());
// Create the CRD so we can create MainThings in kube
let main_thing_crd = MainThing::crd();
let patch_params = PatchParams::apply("example-crd-watcher").force();
crds.patch(&main_thing_crd.name_any(), &patch_params, &Patch::Apply(main_thing_crd)).await?;
// Create the CRD so we can create RefererThings in kube
let referer_thing_crd = RefererThing::crd();
crds.patch(&referer_thing_crd.name_any(), &patch_params, &Patch::Apply(referer_thing_crd)).await?;
// Wait for the api to catch up
sleep(Duration::from_secs(1)).await;
let main_thing_api: Api<MainThing> = Api::default_namespaced(client.clone());
let referer_thing_api: Api<RefererThing> = Api::default_namespaced(client.clone());
let main_thing_instance = MainThing {
metadata: ObjectMeta {
name: Some("my-main-thing".into()),
..Default::default()
},
spec: MainThingSpec {},
};
let main_thing_instance = main_thing_api.patch(&main_thing_instance.name_any(), &patch_params, &Patch::Apply(main_thing_instance)).await?;
let referer_instance = RefererThing {
metadata: ObjectMeta {
name: Some("my-referer".into()),
..Default::default()
},
spec: RefererThingSpec {
main_thing_name: main_thing_instance.name_any(),
main_thing_namespace: None,
value: 0,
},
};
let _referer_instance = referer_thing_api.patch(&referer_instance.name_any(), &patch_params, &Patch::Apply(referer_instance)).await?;
info!("All test resources created");
Ok(())
}