handle_cluster_deletion

Function handle_cluster_deletion 

Source
pub async fn handle_cluster_deletion<T>(
    client: &Client,
    resource: &T,
    finalizer: &str,
) -> Result<()>
where T: Resource<DynamicType = (), Scope = ClusterResourceScope> + ResourceExt + FinalizerCleanup + Clone + Debug + Serialize + for<'de> Deserialize<'de>,
Expand description

Handle cluster-scoped resource deletion with cleanup and finalizer removal.

This function orchestrates the complete deletion process for cluster-scoped resources:

  1. Logs that the resource is being deleted
  2. Calls the resource’s cleanup() method to perform cleanup operations
  3. Removes the finalizer to allow Kubernetes to delete the resource

This function should be called when a cluster-scoped resource has a deletion timestamp and the finalizer is still present.

§Arguments

  • client - Kubernetes client for accessing the API
  • resource - The cluster-scoped resource being deleted
  • finalizer - The finalizer string to check and remove

§Returns

Returns Ok(()) if cleanup and finalizer removal succeeded.

§Errors

Returns an error if:

  • The cleanup operation fails
  • The finalizer removal fails

If an error occurs, the finalizer will remain on the resource and deletion will be blocked until the operation succeeds on a subsequent reconciliation.

§Example

use bindy::reconcilers::finalizers::{handle_cluster_deletion, FinalizerCleanup};
use bindy::crd::ClusterBind9Provider;
use kube::Client;
use anyhow::Result;

const FINALIZER: &str = "bind9globalcluster.dns.firestoned.io/finalizer";

async fn reconcile(client: Client, cluster: ClusterBind9Provider) -> Result<()> {
    if cluster.metadata.deletion_timestamp.is_some() {
        return handle_cluster_deletion(&client, &cluster, FINALIZER).await;
    }
    // Normal reconciliation...
    Ok(())
}