Skip to content

Commit

Permalink
Refactor to DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-karpenko committed Mar 24, 2024
1 parent 0c8dd99 commit c977377
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ target/

#
/*.yaml
TODO.*
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"cSpell.words": [
"chrono",
"clustergitrepos",
"cntrlr",
"crdgen",
"Finalizer",
"gitrepo",
"gitrepos",
Expand Down
22 changes: 13 additions & 9 deletions src/controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,18 @@ where
}

pub(crate) trait Reconcilable {
async fn reconcile_2(&self, ctx: Arc<Context>) -> Result<ReconcileAction>;
async fn cleanup_2(&self, ctx: Arc<Context>) -> Result<ReconcileAction>;
fn finalizer_name(&self) -> &str;
async fn reconcile(&self, ctx: Arc<Context>) -> Result<ReconcileAction>;
async fn cleanup(&self, ctx: Arc<Context>) -> Result<ReconcileAction>;
fn finalizer_name(&self) -> String;
fn kind(&self) -> &str;
}

// TODO: restrict K to Reconcilable trait
fn error_policy<K>(_resource: Arc<K>, error: &Error, _ctx: Arc<Context>) -> ReconcileAction {
fn error_policy<K: Reconcilable>(
_resource: Arc<K>,
error: &Error,
_ctx: Arc<Context>,
) -> ReconcileAction {
warn!("reconcile failed: {:?}", error);
ReconcileAction::await_change()
}
Expand All @@ -223,19 +227,19 @@ where
let resource_api: Api<K> = Api::namespaced(ctx.client.clone(), &ns);

info!(
"Reconciling {} `{}` in {}",
"Reconciling {} `{}/{}`",
resource.kind(),
resource.name_any(),
ns
);
finalizer(
&resource_api,
resource.finalizer_name(),
resource.clone(),
resource.finalizer_name().as_str(),
resource,
|event| async {
match event {
Finalizer::Apply(resource) => resource.reconcile_2(ctx.clone()).await,
Finalizer::Cleanup(resource) => resource.cleanup_2(ctx.clone()).await,
Finalizer::Apply(resource) => resource.reconcile(ctx.clone()).await,
Finalizer::Cleanup(resource) => resource.cleanup(ctx.clone()).await,
}
},
)
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ impl ActionJob {
}

impl Reconcilable for Action {
async fn reconcile_2(&self, _ctx: Arc<Context>) -> Result<ReconcileAction> {
async fn reconcile(&self, _ctx: Arc<Context>) -> Result<ReconcileAction> {
// If no events were received, check back 30 minutes
Ok(ReconcileAction::await_change())
}

async fn cleanup_2(&self, _ctx: Arc<Context>) -> Result<ReconcileAction> {
async fn cleanup(&self, _ctx: Arc<Context>) -> Result<ReconcileAction> {
info!(
"Cleanup Action `{}` in {}",
self.name_any(),
Expand All @@ -114,8 +114,8 @@ impl Reconcilable for Action {
Ok(ReconcileAction::await_change())
}

fn finalizer_name(&self) -> &str {
"actions.git-events-runner.rs"
fn finalizer_name(&self) -> String {
String::from("actions.git-events-runner.rs")
}

fn kind(&self) -> &str {
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/git_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ enum RepoUriSchema {
}

impl Reconcilable for GitRepo {
async fn reconcile_2(&self, ctx: Arc<Context>) -> Result<ReconcileAction> {
async fn reconcile(&self, ctx: Arc<Context>) -> Result<ReconcileAction> {
let client = ctx.client.clone();
let recorder = &ctx.diagnostics.read().await.recorder(client.clone(), self);
let ns = self.namespace().unwrap();
Expand Down Expand Up @@ -304,7 +304,7 @@ impl Reconcilable for GitRepo {
Ok(ReconcileAction::requeue(Duration::from_secs(30 * 60)))
}

async fn cleanup_2(&self, _ctx: Arc<Context>) -> Result<ReconcileAction> {
async fn cleanup(&self, _ctx: Arc<Context>) -> Result<ReconcileAction> {
info!(
"Cleanup GitRepo `{}` in {}",
self.name_any(),
Expand All @@ -313,8 +313,8 @@ impl Reconcilable for GitRepo {
Ok(ReconcileAction::await_change())
}

fn finalizer_name(&self) -> &str {
"gitrepos.git-events-runner.rs"
fn finalizer_name(&self) -> String {
String::from("gitrepos.git-events-runner.rs")
}

fn kind(&self) -> &str {
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub enum TriggerActionKind {
}

impl Reconcilable for Trigger {
async fn reconcile_2(&self, ctx: Arc<Context>) -> Result<ReconcileAction> {
async fn reconcile(&self, ctx: Arc<Context>) -> Result<ReconcileAction> {
let client = ctx.client.clone();
let recorder = &ctx.diagnostics.read().await.recorder(client.clone(), self);
let ns = self.namespace().unwrap();
Expand Down Expand Up @@ -365,7 +365,7 @@ impl Reconcilable for Trigger {
Ok(ReconcileAction::requeue(Duration::from_secs(30 * 60)))
}

async fn cleanup_2(&self, ctx: Arc<Context>) -> Result<ReconcileAction> {
async fn cleanup(&self, ctx: Arc<Context>) -> Result<ReconcileAction> {
info!(
"Cleanup Trigger `{}` in {}",
self.name_any(),
Expand All @@ -392,8 +392,8 @@ impl Reconcilable for Trigger {
Ok(ReconcileAction::await_change())
}

fn finalizer_name(&self) -> &str {
"triggers.git-events-runner.rs"
fn finalizer_name(&self) -> String {
String::from("triggers.git-events-runner.rs")
}

fn kind(&self) -> &str {
Expand Down

0 comments on commit c977377

Please sign in to comment.