kaniop_operator/controller/
context.rs

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// Context for our reconciler
31#[derive(Clone)]
32pub struct Context<K: Resource> {
33    /// Controller ID
34    pub controller_id: ControllerId,
35    /// Kubernetes client
36    pub client: Client,
37    /// Prometheus metrics
38    pub metrics: Arc<ControllerMetrics>,
39    /// State of the error backoff policy per object
40    error_backoff_cache: Arc<RwLock<HashMap<ObjectRef<K>, RwLock<ExponentialBackoff>>>>,
41    /// Event recorder
42    pub recorder: Recorder,
43    /// Cache for Namespace resources
44    pub namespace_store: Store<Namespace>,
45    /// Cache for Kanidm resources
46    pub kanidm_store: Store<Kanidm>,
47    /// Shared Kanidm cache clients with the ability to manage users and their groups
48    idm_clients: Arc<RwLock<KanidmClients>>,
49    /// Shared Kanidm cache clients with the ability to manage the operation of Kanidm as a
50    /// database and service
51    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    pub async fn release_kanidm_clients(&self, kanidm: &Kanidm) {
84        let key = KanidmKey {
85            // safe unwrap: Kanidm is namespaced scoped
86            namespace: kube::ResourceExt::namespace(kanidm).unwrap(),
87            name: kanidm.name_any(),
88        };
89
90        {
91            self.idm_clients.write().await.remove(&key);
92        }
93        {
94            self.system_clients.write().await.remove(&key);
95        }
96    }
97}
98
99impl<K> Context<K>
100where
101    K: Resource<DynamicType = ()> + ResourceExt + KanidmResource + Lookup + Clone + 'static,
102    <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
103{
104    /// Return a valid client for the Kanidm cluster. This operation require to do at least a
105    /// request for validating the client, use it wisely.
106    async fn get_kanidm_client(&self, obj: &K, user: KanidmUser) -> Result<Arc<KanidmClient>> {
107        let namespace = obj.kanidm_namespace();
108        let name = obj.kanidm_name();
109
110        let cache = match user {
111            KanidmUser::Admin => self.system_clients.clone(),
112            KanidmUser::IdmAdmin => self.idm_clients.clone(),
113        };
114        trace!(msg = "try to reuse Kanidm client", namespace, name);
115
116        let key = KanidmKey {
117            namespace: namespace.clone(),
118            name: name.clone(),
119        };
120
121        let client = { cache.read().await.get(&key).cloned() };
122
123        if let Some(client) = client {
124            trace!(
125                msg = "check existing Kanidm client session",
126                namespace, name
127            );
128            if client.auth_valid().await.is_ok() {
129                trace!(msg = "reuse Kanidm client session", namespace, name);
130                return Ok(client.clone());
131            }
132        }
133
134        match KanidmClients::create_client(&namespace, &name, user, self.client.clone()).await {
135            Ok(client) => {
136                {
137                    cache.write().await.insert(key.clone(), client.clone());
138                }
139                Ok(client)
140            }
141            Err(e) => {
142                self.recorder
143                    .publish(
144                        &Event {
145                            type_: EventType::Warning,
146                            reason: "KanidmClientError".to_string(),
147                            note: Some(e.to_string()),
148                            action: "KanidmClientCreating".into(),
149                            secondary: None,
150                        },
151                        &obj.object_ref(&()),
152                    )
153                    .await
154                    .map_err(|e| {
155                        error!(msg = "failed to create Kanidm client", %e);
156                        Error::KubeError("failed to publish event".to_string(), Box::new(e))
157                    })?;
158                Err(e)
159            }
160        }
161    }
162
163    /// Return [`Kanidm`] of the given object
164    ///
165    /// [`Kanidm`]: struct.Kanidm.html
166    pub fn get_kanidm(&self, obj: &K) -> Option<Arc<Kanidm>> {
167        let namespace = obj.kanidm_namespace();
168        let name = obj.kanidm_name();
169        self.kanidm_store.find(|k| {
170            kube::ResourceExt::namespace(k).as_ref() == Some(&namespace) && k.name_any() == name
171        })
172    }
173}
174
175#[allow(async_fn_in_trait)]
176pub trait BackoffContext<K: Resource> {
177    fn metrics(&self) -> &Arc<ControllerMetrics>;
178    async fn get_backoff(&self, obj_ref: ObjectRef<K>) -> Duration;
179    async fn reset_backoff(&self, obj_ref: ObjectRef<K>);
180}
181
182impl<K> BackoffContext<K> for Context<K>
183where
184    K: Resource<DynamicType = ()> + ResourceExt + Lookup + Clone + 'static,
185    <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
186{
187    fn metrics(&self) -> &Arc<ControllerMetrics> {
188        &self.metrics
189    }
190
191    /// Return next duration of the backoff policy for the given object
192    async fn get_backoff(&self, obj_ref: ObjectRef<K>) -> Duration {
193        {
194            let read_guard = self.error_backoff_cache.read().await;
195            if let Some(backoff) = read_guard.get(&obj_ref) {
196                if let Some(duration) = backoff.write().await.next() {
197                    return duration;
198                }
199            }
200        }
201
202        // Backoff policy: 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s, 300s, 300s...
203        let mut backoff = ExponentialBuilder::default()
204            .with_max_delay(DEFAULT_RECONCILE_INTERVAL)
205            .without_max_times()
206            .build();
207        // safe unwrap: first backoff is always Some(Duration)
208        let duration = backoff.next().unwrap();
209        self.error_backoff_cache
210            .write()
211            .await
212            .insert(obj_ref.clone(), RwLock::new(backoff));
213        trace!(
214            msg = format!("recreate backoff policy"),
215            namespace = obj_ref.namespace.as_deref().unwrap(),
216            name = obj_ref.name,
217        );
218        duration
219    }
220
221    /// Reset the backoff policy for the given object
222    async fn reset_backoff(&self, obj_ref: ObjectRef<K>) {
223        let read_guard = self.error_backoff_cache.read().await;
224        if read_guard.get(&obj_ref).is_some() {
225            drop(read_guard);
226            trace!(
227                msg = "reset backoff policy",
228                namespace = obj_ref.namespace.as_deref().unwrap(),
229                name = obj_ref.name
230            );
231            self.error_backoff_cache.write().await.remove(&obj_ref);
232        }
233    }
234}
235
236#[allow(async_fn_in_trait)]
237pub trait IdmClientContext<K: Resource> {
238    async fn get_idm_client(&self, obj: &K) -> Result<Arc<KanidmClient>>;
239}
240
241impl<K> IdmClientContext<K> for Context<K>
242where
243    K: Resource<DynamicType = ()> + ResourceExt + KanidmResource + Lookup + Clone + 'static,
244    <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
245{
246    async fn get_idm_client(&self, obj: &K) -> Result<Arc<KanidmClient>> {
247        self.get_kanidm_client(obj, KanidmUser::IdmAdmin).await
248    }
249}
250
251#[allow(async_fn_in_trait)]
252pub trait SystemClientContext<K: Resource> {
253    async fn get_system_client(&self, obj: &K) -> Result<Arc<KanidmClient>>;
254}
255
256impl<K> SystemClientContext<K> for Context<K>
257where
258    K: Resource<DynamicType = ()> + ResourceExt + KanidmResource + Lookup + Clone + 'static,
259    <K as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
260{
261    async fn get_system_client(&self, obj: &K) -> Result<Arc<KanidmClient>> {
262        self.get_kanidm_client(obj, KanidmUser::Admin).await
263    }
264}
265
266#[allow(async_fn_in_trait)]
267pub trait KubeOperations<T, K>
268where
269    T: Resource + ResourceExt + Lookup + Clone + 'static,
270    <T as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
271    K: Resource<Scope = NamespaceResourceScope>
272        + Serialize
273        + Clone
274        + std::fmt::Debug
275        + for<'de> Deserialize<'de>,
276    <K as kube::Resource>::DynamicType: Default,
277    <K as Resource>::Scope: std::marker::Sized,
278{
279    async fn kube_delete(&self, client: Client, metrics: &ControllerMetrics, obj: &K)
280    -> Result<()>;
281    async fn kube_patch(
282        &self,
283        client: Client,
284        metrics: &ControllerMetrics,
285        obj: K,
286        operator_name: &str,
287    ) -> Result<K>;
288}
289
290impl<T, K> KubeOperations<T, K> for T
291where
292    T: Resource + ResourceExt + Lookup + Clone + 'static,
293    <T as Lookup>::DynamicType: Eq + std::hash::Hash + Clone,
294    K: Resource<Scope = NamespaceResourceScope>
295        + Serialize
296        + Clone
297        + std::fmt::Debug
298        + for<'de> Deserialize<'de>,
299    <K as kube::Resource>::DynamicType: Default,
300    <K as Resource>::Scope: std::marker::Sized,
301{
302    async fn kube_delete(
303        &self,
304        client: Client,
305        _metrics: &ControllerMetrics,
306        obj: &K,
307    ) -> Result<()> {
308        let name = obj.name_any();
309        // safe unwrap: self is namespaced scoped
310        let namespace = kube::ResourceExt::namespace(self).unwrap();
311        trace!(
312            msg = format!("deleting {}", short_type_name::<K>().unwrap_or("Unknown")),
313            resource.name = &name,
314            resource.namespace = &namespace
315        );
316        let api = Api::<K>::namespaced(client, &namespace);
317        api.delete(&name, &Default::default()).await.map_err(|e| {
318            Error::KubeError(
319                format!(
320                    "failed to delete {} {namespace}/{name}",
321                    short_type_name::<K>().unwrap_or("Unknown")
322                ),
323                Box::new(e),
324            )
325        })?;
326        Ok(())
327    }
328
329    async fn kube_patch(
330        &self,
331        client: Client,
332        metrics: &ControllerMetrics,
333        obj: K,
334        operator_name: &str,
335    ) -> Result<K> {
336        let name = obj.name_any();
337        // safe unwrap: self is namespaced scoped
338        let namespace = kube::ResourceExt::namespace(self).unwrap();
339        trace!(
340            msg = format!("patching {}", short_type_name::<K>().unwrap_or("Unknown")),
341            resource.name = &name,
342            resource.namespace = &namespace
343        );
344        let resource_api = Api::<K>::namespaced(client.clone(), &namespace);
345
346        let result = resource_api
347            .patch(
348                &name,
349                &PatchParams::apply(operator_name).force(),
350                &Patch::Apply(&obj),
351            )
352            .await;
353        match result {
354            Ok(resource) => Ok(resource),
355            Err(e) => match e {
356                kube::Error::Api(ae) if ae.code == 422 => {
357                    info!(
358                        msg = format!(
359                            "recreating {} because the update operation was not possible",
360                            short_type_name::<K>().unwrap_or("Unknown")
361                        ),
362                        reason = ae.reason
363                    );
364                    trace!(msg = "operation was not possible because of 422", ?ae);
365                    self.kube_delete(client.clone(), metrics, &obj).await?;
366                    metrics.reconcile_deploy_delete_create_inc();
367                    resource_api
368                        .patch(
369                            &name,
370                            &PatchParams::apply(operator_name).force(),
371                            &Patch::Apply(&obj),
372                        )
373                        .await
374                        .map_err(|e| {
375                            Error::KubeError(
376                                format!(
377                                    "failed to re-try patch {} {namespace}/{name}",
378                                    short_type_name::<K>().unwrap_or("Unknown")
379                                ),
380                                Box::new(e),
381                            )
382                        })
383                }
384                _ => Err(Error::KubeError(
385                    format!(
386                        "failed to patch {} {namespace}/{name}",
387                        short_type_name::<K>().unwrap_or("Unknown")
388                    ),
389                    Box::new(e),
390                )),
391            },
392        }
393    }
394}