Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions internal/controller/networking/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,25 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
logger.Info("Ingress Object found")

// Gather data for the checkly check
logger.Info("Gathering data for the check")
apiCheckResources, err := r.gatherApiCheckData(&ingress)
if err != nil {
logger.Error(err, "unable to gather data for the apiCheck resource", "Ingress Name", ingress.Name, "Ingress namespace", ingress.Namespace)
return ctrl.Result{}, err
}

// Do we want to do anything with the ingress?
if value, exists := ingress.Annotations[annotationEnabled]; !exists || value == "false" {
logger.Info("Checking to see if we need to delete any resources as we're not handling this ingress", "Ingress Name", ingress.Name, "Ingress namespace", ingress.Namespace)
logger.Info("Ingress not enabled for Checkly monitoring, cleaning up any existing resources", "Ingress Name", ingress.Name, "Ingress namespace", ingress.Namespace)

r.deleteIngressApiChecks(ctx, req, apiCheckResources, ingress)
// Clean up existing ApiChecks without requiring annotations
r.cleanupApiChecksForIngress(ctx, &ingress)

if ingress.GetDeletionTimestamp() == nil {
return ctrl.Result{}, nil
}
}

// Gather data for the checkly check (only if enabled)
logger.Info("Gathering data for the check")
apiCheckResources, err := r.gatherApiCheckData(&ingress)
if err != nil {
logger.Error(err, "unable to gather data for the apiCheck resource", "Ingress Name", ingress.Name, "Ingress namespace", ingress.Namespace)
return ctrl.Result{}, err
}
// ////////////////////////////////
// Delete Logic
// ///////////////////////////////
Expand All @@ -99,7 +100,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if controllerutil.ContainsFinalizer(&ingress, checklyFinalizer) {
logger.Info("Finalizer present, need to delete ApiCheck first", "Ingress Name", ingress.Name, "Ingress namespace", ingress.Namespace)

r.deleteIngressApiChecks(ctx, req, apiCheckResources, ingress)
r.cleanupApiChecksForIngress(ctx, &ingress)

// Delete finalizer logic
logger.Info("Deleting finalizer", "Ingress Name", ingress.Name, "Ingress namespace", ingress.Namespace)
Expand Down Expand Up @@ -342,6 +343,30 @@ func (r *IngressReconciler) compareApiChecks(
return
}

func (r *IngressReconciler) cleanupApiChecksForIngress(
ctx context.Context,
ingress *networkingv1.Ingress,
) {
logger := log.FromContext(ctx)

var existingApiChecks checklyv1alpha1.ApiCheckList
err := r.List(ctx, &existingApiChecks, client.InNamespace(ingress.Namespace), client.MatchingLabels{"ingress-controller": ingress.Name})
if err != nil {
logger.Error(err, "Failed to list existing ApiChecks for cleanup", "Ingress Name", ingress.Name, "Ingress namespace", ingress.Namespace)
return
}

for _, apiCheck := range existingApiChecks.Items {
logger.Info("Deleting existing ApiCheck", "ApiCheck Name", apiCheck.Name, "Ingress Name", ingress.Name)
err = r.Delete(ctx, &apiCheck)
if err != nil {
logger.Error(err, "Failed to delete ApiCheck during cleanup", "ApiCheck Name", apiCheck.Name, "Namespace", apiCheck.Namespace)
continue
}
logger.Info("Successfully deleted ApiCheck", "ApiCheck Name", apiCheck.Name)
}
}

func (r *IngressReconciler) deleteIngressApiChecks(
ctx context.Context,
req ctrl.Request,
Expand Down
Loading