From 13ddc853fb320d29ae86defa7aa1bb11114f9b07 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sat, 22 Mar 2025 02:22:41 +0100 Subject: [PATCH] Moved ServiceUser into separate file --- src/resources/mod.rs | 75 +++++++++++++++++++ .../service_user.rs} | 66 +--------------- ...user__tests__service_user_crd_output.snap} | 0 3 files changed, 78 insertions(+), 63 deletions(-) create mode 100644 src/resources/mod.rs rename src/{resources.rs => resources/service_user.rs} (79%) rename src/{snapshots/lldap_controller__resources__tests__service_user_crd_output.snap => resources/snapshots/lldap_controller__resources__service_user__tests__service_user_crd_output.snap} (100%) diff --git a/src/resources/mod.rs b/src/resources/mod.rs new file mode 100644 index 0000000..b09f9a9 --- /dev/null +++ b/src/resources/mod.rs @@ -0,0 +1,75 @@ +mod service_user; + +use core::fmt; +use std::sync::Arc; + +use async_trait::async_trait; +use k8s_openapi::NamespaceResourceScope; +use kube::runtime::controller::Action; +use kube::runtime::finalizer; +use kube::{Api, Resource, ResourceExt}; +use serde::de::DeserializeOwned; +use serde::Serialize; +use tracing::{debug, instrument}; + +use crate::context::Context; +use crate::lldap; + +pub use service_user::ServiceUser; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Failed to commit: {0}")] + Commit(#[from] kube::api::entry::CommitError), + #[error("Kube api error: {0}")] + Kube(#[from] kube::Error), + #[error("LLDAP error: {0}")] + Lldap(#[from] lldap::Error), + #[error("Finalizer error: {0}")] + Finalizer(#[source] Box>), + #[error("MissingObjectKey: {0}")] + MissingObjectKey(&'static str), +} + +impl From> for Error { + fn from(error: finalizer::Error) -> Self { + Self::Finalizer(Box::new(error)) + } +} + +type Result = std::result::Result; + +#[async_trait] +trait Reconcile { + async fn reconcile(self: Arc, ctx: Arc) -> Result; + + async fn cleanup(self: Arc, ctx: Arc) -> Result; +} + +#[instrument(skip(obj, ctx))] +pub async fn reconcile(obj: Arc, ctx: Arc) -> Result +where + T: Resource + + ResourceExt + + Clone + + Serialize + + DeserializeOwned + + fmt::Debug + + Reconcile, + ::DynamicType: Default, +{ + debug!(name = obj.name_any(), "Reconcile"); + + let namespace = obj.namespace().expect("Resource is namespace scoped"); + let service_users = Api::::namespaced(ctx.client.clone(), &namespace); + + Ok( + finalizer(&service_users, &ctx.controller_name, obj, |event| async { + match event { + finalizer::Event::Apply(obj) => obj.reconcile(ctx.clone()).await, + finalizer::Event::Cleanup(obj) => obj.cleanup(ctx.clone()).await, + } + }) + .await?, + ) +} diff --git a/src/resources.rs b/src/resources/service_user.rs similarity index 79% rename from src/resources.rs rename to src/resources/service_user.rs index 80eb246..88ca83b 100644 --- a/src/resources.rs +++ b/src/resources/service_user.rs @@ -1,4 +1,3 @@ -use core::fmt; use std::collections::BTreeMap; use std::str::from_utf8; use std::sync::Arc; @@ -8,43 +7,19 @@ use async_trait::async_trait; use chrono::{DateTime, Utc}; use k8s_openapi::api::core::v1::Secret; use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference; -use k8s_openapi::NamespaceResourceScope; use kube::api::{ObjectMeta, Patch, PatchParams, PostParams}; use kube::runtime::controller::Action; -use kube::runtime::finalizer; -use kube::{Api, CustomResource, Resource, ResourceExt}; +use kube::{Api, CustomResource, Resource}; use passwords::PasswordGenerator; use schemars::JsonSchema; -use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use serde_json::json; -use tracing::{debug, instrument, trace, warn}; +use tracing::{debug, trace, warn}; +use super::{Error, Reconcile, Result}; use crate::context::{Context, ControllerEvents}; use crate::lldap; -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error("Failed to commit: {0}")] - Commit(#[from] kube::api::entry::CommitError), - #[error("Kube api error: {0}")] - Kube(#[from] kube::Error), - #[error("LLDAP error: {0}")] - Lldap(#[from] lldap::Error), - #[error("Finalizer error: {0}")] - Finalizer(#[source] Box>), - #[error("MissingObjectKey: {0}")] - MissingObjectKey(&'static str), -} - -impl From> for Error { - fn from(error: finalizer::Error) -> Self { - Self::Finalizer(Box::new(error)) - } -} - -pub type Result = std::result::Result; - #[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)] #[kube( kind = "ServiceUser", @@ -97,41 +72,6 @@ fn new_secret(username: &str, oref: OwnerReference) -> Secret { } } -#[async_trait] -trait Reconcile { - async fn reconcile(self: Arc, ctx: Arc) -> Result; - - async fn cleanup(self: Arc, ctx: Arc) -> Result; -} - -#[instrument(skip(obj, ctx))] -pub async fn reconcile(obj: Arc, ctx: Arc) -> Result -where - T: Resource - + ResourceExt - + Clone - + Serialize - + DeserializeOwned - + fmt::Debug - + Reconcile, - ::DynamicType: Default, -{ - debug!(name = obj.name_any(), "Reconcile"); - - let namespace = obj.namespace().expect("Resource is namespace scoped"); - let service_users = Api::::namespaced(ctx.client.clone(), &namespace); - - Ok( - finalizer(&service_users, &ctx.controller_name, obj, |event| async { - match event { - finalizer::Event::Apply(obj) => obj.reconcile(ctx.clone()).await, - finalizer::Event::Cleanup(obj) => obj.cleanup(ctx.clone()).await, - } - }) - .await?, - ) -} - fn format_username(name: &str, namespace: &str) -> String { format!("{name}.{namespace}") } diff --git a/src/snapshots/lldap_controller__resources__tests__service_user_crd_output.snap b/src/resources/snapshots/lldap_controller__resources__service_user__tests__service_user_crd_output.snap similarity index 100% rename from src/snapshots/lldap_controller__resources__tests__service_user_crd_output.snap rename to src/resources/snapshots/lldap_controller__resources__service_user__tests__service_user_crd_output.snap