Skip to content

Commit e1c4c21

Browse files
committed
feat(auto-monitoring): delete all auto-resources when disabling
Delete all automatically created Dash0Monitoring resources when autoMonitorNamespaces is switched from enabled to disabled, or when the operator configuration resource is removed.
1 parent b4f5fac commit e1c4c21

4 files changed

Lines changed: 235 additions & 49 deletions

File tree

internal/controller/auto_namespace_monitoring_controller.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (r *AutoNamespaceMonitoringReconciler) Reconcile(ctx context.Context, req c
113113
} else if checkResourceResult.ResourceDoesNotExist {
114114
logger.Debug("operator configuration resource does not exist, stopping namespace watch")
115115
r.ensureNamespaceWatchIsStopped(ctx, logger)
116+
r.deleteAllAutoMonitoringResourcesInCluster(ctx, logger)
116117
return ctrl.Result{}, nil
117118
} else if checkResourceResult.StopReconcile {
118119
return ctrl.Result{}, nil
@@ -121,6 +122,9 @@ func (r *AutoNamespaceMonitoringReconciler) Reconcile(ctx context.Context, req c
121122
operatorConfigurationResource := checkResourceResult.Resource.(*dash0v1alpha1.Dash0OperatorConfiguration)
122123

123124
if !operatorConfigurationResource.IsAvailable() {
125+
// Note: Deliberately not deleting auto-monitoring resources here. An unavailable operator configuration may be a transient
126+
// state; deleting all auto-monitoring resources could introduce a lot of unnecessary churn. This will resolve itself if
127+
// either the unavailable resource becomes available again or is deleted.
124128
logger.Debug("operator configuration unavailable, stopping namespace watch")
125129
r.ensureNamespaceWatchIsStopped(ctx, logger)
126130
return ctrl.Result{}, nil
@@ -138,6 +142,7 @@ func (r *AutoNamespaceMonitoringReconciler) Reconcile(ctx context.Context, req c
138142
} else {
139143
logger.Debug("AutoMonitorNamespaces disabled, stopping namespace watch")
140144
r.ensureNamespaceWatchIsStopped(ctx, logger)
145+
r.deleteAllAutoMonitoringResourcesInCluster(ctx, logger)
141146
}
142147
return ctrl.Result{}, nil
143148
}
@@ -349,6 +354,36 @@ func (r *AutoNamespaceMonitoringReconciler) unmonitorNamespacesThatNoLongerMatch
349354
}()
350355
}
351356

357+
// deleteAllAutoMonitoringResourcesInCluster deletes all Dash0Monitoring resources across the cluster that were created by the
358+
// auto-namespace-monitoring controller (identified by the dash0.com/auto-monitored-namespace=true label). The list is
359+
// filtered server/cache-side by label, so it returns only the auto-monitored resources. Per-item delete failures are
360+
// logged and iteration continues; the next reconcile retries any remaining resources.
361+
func (r *AutoNamespaceMonitoringReconciler) deleteAllAutoMonitoringResourcesInCluster(ctx context.Context, logger logd.Logger) {
362+
go func() {
363+
list := &dash0v1beta1.Dash0MonitoringList{}
364+
if err := r.List(ctx, list, client.MatchingLabels{util.AutoMonitoredNamespaceLabel: util.TrueString}); err != nil {
365+
logger.Error(err, "cannot list auto-monitoring resources for deletion after auto-namespace-monitoring has been disabled")
366+
return
367+
}
368+
for i := range list.Items {
369+
resource := &list.Items[i]
370+
logger.Info(
371+
"removing monitoring resource after auto-namespace-monitoring has been disabled",
372+
"namespace", resource.Namespace,
373+
"name", resource.Name,
374+
)
375+
if err := r.Delete(ctx, resource); err != nil && !apierrors.IsNotFound(err) {
376+
logger.Error(
377+
err,
378+
"cannot delete auto-monitoring resource after auto-namespace-monitoring has been disabled",
379+
"namespace", resource.Namespace,
380+
"name", resource.Name,
381+
)
382+
}
383+
}
384+
}()
385+
}
386+
352387
func (r *AutoNamespaceMonitoringReconciler) updateAllAutoMonitoringResourcesWithNewTemplate(
353388
ctx context.Context,
354389
monitoringTemplate dash0v1alpha1.MonitoringTemplate,
@@ -646,12 +681,15 @@ func (w *NamespaceWatcher) ensureNamespaceIsUnmonitored(
646681
return err
647682
}
648683
for i := range existingList.Items {
684+
resource := &existingList.Items[i]
649685
logger.Info(
650-
"removing monitoring resource from namespace after the auto-namespace-monitoring label selector has changed",
686+
"removing auto-monitoring resource from namespace",
651687
"namespace",
652688
ns.Name,
689+
"name",
690+
resource.Name,
653691
)
654-
if err := w.Delete(ctx, &existingList.Items[i]); err != nil && !apierrors.IsNotFound(err) {
692+
if err := w.Delete(ctx, resource); err != nil && !apierrors.IsNotFound(err) {
655693
return err
656694
}
657695
}

0 commit comments

Comments
 (0)