Skip to content

Commit

Permalink
delete service monitor in openshift montitoring namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros committed Oct 17, 2023
1 parent 53229c1 commit bb1e1bf
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"reflect"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -316,6 +317,11 @@ func (r *MultiClusterObservabilityReconciler) Reconcile(ctx context.Context, req
reqLogger.Error(err, "Failed to delete the specific PrometheusRule in the openshift-monitoring namespace")
return ctrl.Result{}, err
}
// Delete ServiceMonitor from openshft-monitoring namespace
if err := r.deleteServiceMonitorInOpenshiftMonitoringNamespace(ctx); err != nil {
reqLogger.Error(err, "Failed to delete service monitor in the openshift-monitoring namespace")
return ctrl.Result{}, err
}
}

//update status
Expand Down Expand Up @@ -950,6 +956,7 @@ func cleanUpClusterScopedResources(
return nil
}

// Delete the PrometheusRule in openshift-monitoring namespace
func (r *MultiClusterObservabilityReconciler) deleteSpecificPrometheusRule(ctx context.Context) error {
promRule := &monitoringv1.PrometheusRule{}
err := r.Client.Get(ctx, client.ObjectKey{Name: "acm-observability-alert-rules",
Expand All @@ -968,3 +975,25 @@ func (r *MultiClusterObservabilityReconciler) deleteSpecificPrometheusRule(ctx c

return nil
}

// Delete the ServiceMonitor in openshift-monitoring namespace
func (r *MultiClusterObservabilityReconciler) deleteServiceMonitorInOpenshiftMonitoringNamespace(ctx context.Context) error {
serviceMonitorList := &monitoringv1.ServiceMonitorList{}
err := r.Client.List(ctx, serviceMonitorList, client.InNamespace("openshift-monitoring"))
if !apierrors.IsNotFound(err) && err != nil {
log.Error(err, "Failed to fetch ServiceMonitors")
return err
}

for _, sm := range serviceMonitorList.Items {
if strings.HasPrefix(sm.Name, "observability-") {
err = r.Client.Delete(ctx, sm)
if err != nil {
log.Error(err, "Failed to delete ServiceMonitor", "ServiceMonitorName", sm.Name)
return err
}
log.Info("Deleted ServiceMonitor", "ServiceMonitorName", sm.Name)
}
}
return nil
}

0 comments on commit bb1e1bf

Please sign in to comment.