1use super::{
2 ControllerId, DEFAULT_RECONCILE_INTERVAL, KanidmClients,
3 kanidm::{KanidmKey, KanidmResource, KanidmUser},
4};
5
6use crate::error::{Error, Result};
7use crate::kanidm::crd::Kanidm;
8use crate::metrics::ControllerMetrics;
9
10use kaniop_k8s_util::types::short_type_name;
11
12use std::collections::HashMap;
13use std::sync::Arc;
14
15use backon::{BackoffBuilder, ExponentialBackoff, ExponentialBuilder};
16use k8s_openapi::{NamespaceResourceScope, api::core::v1::Namespace};
17use kanidm_client::KanidmClient;
18use kube::runtime::events::{Event, EventType, Recorder};
19use kube::{Api, client::Client};
20use kube::{Resource, ResourceExt};
21use kube::{
22 api::{Patch, PatchParams},
23 runtime::reflector::{Lookup, ObjectRef, Store},
24};
25use serde::{Deserialize, Serialize};
26use tokio::sync::RwLock;
27use tokio::time::Duration;
28use tracing::{error, info, trace};
29
30#[derive(Clone)]
32pub struct Context<K: Resource> {
33 pub controller_id: ControllerId,
35 pub client: Client,
37 pub metrics: Arc<ControllerMetrics>,
39 error_backoff_cache: Arc<RwLock<HashMap<ObjectRef<K>, RwLock<ExponentialBackoff>>>>,
41 pub recorder: Recorder,
43 pub namespace_store: Store<Namespace>,
45 pub kanidm_store: Store<Kanidm>,
47 idm_clients: Arc<RwLock<KanidmClients>>,
49 system_clients: Arc<RwLock<KanidmClients>>,
52}
53
54impl<K> Context<K>
55where
56 K: Resource + ResourceExt + Lookup + Clone + 'static,
57 <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
58{
59 #[allow(clippy::too_many_arguments)]
60 pub fn new(
61 controller_id: ControllerId,
62 client: Client,
63 metrics: Arc<ControllerMetrics>,
64 recorder: Recorder,
65 idm_clients: Arc<RwLock<KanidmClients>>,
66 system_clients: Arc<RwLock<KanidmClients>>,
67 namespace_store: Store<Namespace>,
68 kanidm_store: Store<Kanidm>,
69 ) -> Self {
70 Self {
71 controller_id,
72 client,
73 metrics,
74 recorder,
75 namespace_store,
76 kanidm_store,
77 idm_clients,
78 system_clients,
79 error_backoff_cache: Arc::default(),
80 }
81 }
82}
83
84impl<K> Context<K>
85where
86 K: Resource<DynamicType = ()> + ResourceExt + KanidmResource + Lookup + Clone + 'static,
87 <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
88{
89 async fn get_kanidm_client(&self, obj: &K, user: KanidmUser) -> Result<Arc<KanidmClient>> {
92 let namespace = obj.kanidm_namespace();
93 let name = obj.kanidm_name();
94
95 let cache = match user {
96 KanidmUser::Admin => self.system_clients.clone(),
97 KanidmUser::IdmAdmin => self.idm_clients.clone(),
98 };
99 trace!(msg = "try to reuse Kanidm client", namespace, name);
100
101 let key = KanidmKey {
102 namespace: namespace.clone(),
103 name: name.clone(),
104 };
105
106 if let Some(client) = cache.read().await.get(&key) {
107 trace!(
108 msg = "check existing Kanidm client session",
109 namespace, name
110 );
111 if client.auth_valid().await.is_ok() {
112 trace!(msg = "reuse Kanidm client session", namespace, name);
113 return Ok(client.clone());
114 }
115 }
116
117 match KanidmClients::create_client(&namespace, &name, user, self.client.clone()).await {
118 Ok(client) => {
119 cache.write().await.insert(key.clone(), client.clone());
120 Ok(client)
121 }
122 Err(e) => {
123 self.recorder
124 .publish(
125 &Event {
126 type_: EventType::Warning,
127 reason: "KanidmClientError".to_string(),
128 note: Some(e.to_string()),
129 action: "KanidmClientCreating".into(),
130 secondary: None,
131 },
132 &obj.object_ref(&()),
133 )
134 .await
135 .map_err(|e| {
136 error!(msg = "failed to create Kanidm client", %e);
137 Error::KubeError("failed to publish event".to_string(), Box::new(e))
138 })?;
139 Err(e)
140 }
141 }
142 }
143
144 pub fn get_kanidm(&self, obj: &K) -> Option<Arc<Kanidm>> {
148 let namespace = obj.kanidm_namespace();
149 let name = obj.kanidm_name();
150 self.kanidm_store.find(|k| {
151 kube::ResourceExt::namespace(k).as_ref() == Some(&namespace) && k.name_any() == name
152 })
153 }
154}
155
156#[allow(async_fn_in_trait)]
157pub trait BackoffContext<K: Resource> {
158 fn metrics(&self) -> &Arc<ControllerMetrics>;
159 async fn get_backoff(&self, obj_ref: ObjectRef<K>) -> Duration;
160 async fn reset_backoff(&self, obj_ref: ObjectRef<K>);
161}
162
163impl<K> BackoffContext<K> for Context<K>
164where
165 K: Resource<DynamicType = ()> + ResourceExt + Lookup + Clone + 'static,
166 <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
167{
168 fn metrics(&self) -> &Arc<ControllerMetrics> {
169 &self.metrics
170 }
171
172 async fn get_backoff(&self, obj_ref: ObjectRef<K>) -> Duration {
174 {
175 let read_guard = self.error_backoff_cache.read().await;
176 if let Some(backoff) = read_guard.get(&obj_ref) {
177 if let Some(duration) = backoff.write().await.next() {
178 return duration;
179 }
180 }
181 }
182
183 let mut backoff = ExponentialBuilder::default()
185 .with_max_delay(DEFAULT_RECONCILE_INTERVAL)
186 .without_max_times()
187 .build();
188 let duration = backoff.next().unwrap();
190 self.error_backoff_cache
191 .write()
192 .await
193 .insert(obj_ref.clone(), RwLock::new(backoff));
194 trace!(
195 msg = format!("recreate backoff policy"),
196 namespace = obj_ref.namespace.as_deref().unwrap(),
197 name = obj_ref.name,
198 );
199 duration
200 }
201
202 async fn reset_backoff(&self, obj_ref: ObjectRef<K>) {
204 let read_guard = self.error_backoff_cache.read().await;
205 if read_guard.get(&obj_ref).is_some() {
206 drop(read_guard);
207 trace!(
208 msg = "reset backoff policy",
209 namespace = obj_ref.namespace.as_deref().unwrap(),
210 name = obj_ref.name
211 );
212 let mut write_guard = self.error_backoff_cache.write().await;
213 write_guard.remove(&obj_ref);
214 }
215 }
216}
217
218#[allow(async_fn_in_trait)]
219pub trait IdmClientContext<K: Resource> {
220 async fn get_idm_client(&self, obj: &K) -> Result<Arc<KanidmClient>>;
221}
222
223impl<K> IdmClientContext<K> for Context<K>
224where
225 K: Resource<DynamicType = ()> + ResourceExt + KanidmResource + Lookup + Clone + 'static,
226 <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
227{
228 async fn get_idm_client(&self, obj: &K) -> Result<Arc<KanidmClient>> {
229 self.get_kanidm_client(obj, KanidmUser::IdmAdmin).await
230 }
231}
232
233#[allow(async_fn_in_trait)]
234pub trait SystemClientContext<K: Resource> {
235 async fn get_system_client(&self, obj: &K) -> Result<Arc<KanidmClient>>;
236}
237
238impl<K> SystemClientContext<K> for Context<K>
239where
240 K: Resource<DynamicType = ()> + ResourceExt + KanidmResource + Lookup + Clone + 'static,
241 <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
242{
243 async fn get_system_client(&self, obj: &K) -> Result<Arc<KanidmClient>> {
244 self.get_kanidm_client(obj, KanidmUser::Admin).await
245 }
246}
247
248#[allow(async_fn_in_trait)]
249pub trait KubeOperations<T, K>
250where
251 T: Resource + ResourceExt + Lookup + Clone + 'static,
252 <T as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
253 K: Resource<Scope = NamespaceResourceScope>
254 + Serialize
255 + Clone
256 + std::fmt::Debug
257 + for<'de> Deserialize<'de>,
258 <K as kube::Resource>::DynamicType: Default,
259 <K as Resource>::Scope: std::marker::Sized,
260{
261 async fn kube_delete(&self, client: Client, metrics: &ControllerMetrics, obj: &K)
262 -> Result<()>;
263 async fn kube_patch(
264 &self,
265 client: Client,
266 metrics: &ControllerMetrics,
267 obj: K,
268 operator_name: &str,
269 ) -> Result<K>;
270}
271
272impl<T, K> KubeOperations<T, K> for T
273where
274 T: Resource + ResourceExt + Lookup + Clone + 'static,
275 <T as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
276 K: Resource<Scope = NamespaceResourceScope>
277 + Serialize
278 + Clone
279 + std::fmt::Debug
280 + for<'de> Deserialize<'de>,
281 <K as kube::Resource>::DynamicType: Default,
282 <K as Resource>::Scope: std::marker::Sized,
283{
284 async fn kube_delete(
285 &self,
286 client: Client,
287 _metrics: &ControllerMetrics,
288 obj: &K,
289 ) -> Result<()> {
290 let name = obj.name_any();
291 let namespace = kube::ResourceExt::namespace(self).unwrap();
293 trace!(
294 msg = format!("deleting {}", short_type_name::<K>().unwrap_or("Unknown")),
295 resource.name = &name,
296 resource.namespace = &namespace
297 );
298 let api = Api::<K>::namespaced(client, &namespace);
299 api.delete(&name, &Default::default()).await.map_err(|e| {
300 Error::KubeError(
301 format!(
302 "failed to delete {} {namespace}/{name}",
303 short_type_name::<K>().unwrap_or("Unknown")
304 ),
305 Box::new(e),
306 )
307 })?;
308 Ok(())
309 }
310
311 async fn kube_patch(
312 &self,
313 client: Client,
314 metrics: &ControllerMetrics,
315 obj: K,
316 operator_name: &str,
317 ) -> Result<K> {
318 let name = obj.name_any();
319 let namespace = kube::ResourceExt::namespace(self).unwrap();
321 trace!(
322 msg = format!("patching {}", short_type_name::<K>().unwrap_or("Unknown")),
323 resource.name = &name,
324 resource.namespace = &namespace
325 );
326 let resource_api = Api::<K>::namespaced(client.clone(), &namespace);
327
328 let result = resource_api
329 .patch(
330 &name,
331 &PatchParams::apply(operator_name).force(),
332 &Patch::Apply(&obj),
333 )
334 .await;
335 match result {
336 Ok(resource) => Ok(resource),
337 Err(e) => match e {
338 kube::Error::Api(ae) if ae.code == 422 => {
339 info!(
340 msg = format!(
341 "recreating {} because the update operation was not possible",
342 short_type_name::<K>().unwrap_or("Unknown")
343 ),
344 reason = ae.reason
345 );
346 trace!(msg = "operation was not possible because of 422", ?ae);
347 self.kube_delete(client.clone(), metrics, &obj).await?;
348 metrics.reconcile_deploy_delete_create_inc();
349 resource_api
350 .patch(
351 &name,
352 &PatchParams::apply(operator_name).force(),
353 &Patch::Apply(&obj),
354 )
355 .await
356 .map_err(|e| {
357 Error::KubeError(
358 format!(
359 "failed to re-try patch {} {namespace}/{name}",
360 short_type_name::<K>().unwrap_or("Unknown")
361 ),
362 Box::new(e),
363 )
364 })
365 }
366 _ => Err(Error::KubeError(
367 format!(
368 "failed to patch {} {namespace}/{name}",
369 short_type_name::<K>().unwrap_or("Unknown")
370 ),
371 Box::new(e),
372 )),
373 },
374 }
375 }
376}