diff --git a/controllers/backplaneconfig_controller.go b/controllers/backplaneconfig_controller.go index bf88a0a52..7192b2177 100644 --- a/controllers/backplaneconfig_controller.go +++ b/controllers/backplaneconfig_controller.go @@ -306,6 +306,11 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R return result, err } + err = r.removeLegacyCLCPrometheusConfig(ctx) + if err != nil { + return ctrl.Result{}, err + } + result, err = r.ensureToggleableComponents(ctx, backplaneConfig) if err != nil { return result, err diff --git a/controllers/backplaneconfig_controller_test.go b/controllers/backplaneconfig_controller_test.go index dc3b79625..88af4cb2b 100644 --- a/controllers/backplaneconfig_controller_test.go +++ b/controllers/backplaneconfig_controller_test.go @@ -1051,5 +1051,64 @@ var _ = Describe("BackplaneConfig controller", func() { }) }) + Context("Legacy clean up tasks", func() { + It("Removes the legacy CLC Prometheus configuration", func() { + By("creating the backplane config with nonexistant secret") + backplaneConfig := &v1.MultiClusterEngine{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "multicluster.openshift.io/v1", + Kind: "MultiClusterEngine", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: BackplaneConfigName, + }, + Spec: v1.MultiClusterEngineSpec{ + TargetNamespace: DestinationNamespace, + ImagePullSecret: "nonexistant", + }, + } + createCtx := context.Background() + Expect(k8sClient.Create(createCtx, backplaneConfig)).Should(Succeed()) + By("Creating the legacy CLC ServiceMonitor") + sm := &unstructured.Unstructured{ + Object: map[string]interface{}{ + "spec": map[string]interface{}{ + "endpoints": []interface{}{ + map[string]interface{}{ + "path": "/some/path", + }, + }, + "selector": map[string]interface{}{ + "matchLabels": map[string]interface{}{ + "app": "grc", + }, + }, + }, + }, + } + sm.SetGroupVersionKind(schema.GroupVersionKind{ + Group: "monitoring.coreos.com", + Kind: "ServiceMonitor", + Version: "v1", + }) + sm.SetName("clusterlifecycle-state-metrics-v2") + sm.SetNamespace("openshift-monitoring") + + err := k8sClient.Create(context.TODO(), sm) + Expect(err).To(BeNil()) + + By("Running the cleanup of the legacy Prometheus configuration") + err = reconciler.removeLegacyCLCPrometheusConfig(context.TODO()) + Expect(err).To(BeNil()) + + By("Verifying that the legacy CLC ServiceMonitor is deleted") + err = k8sClient.Get(context.TODO(), client.ObjectKeyFromObject(sm), sm) + Expect(errors.IsNotFound(err)).To(BeTrue()) + + By("Running the cleanup of the legacy Prometheus configuration again should do nothing") + err = reconciler.removeLegacyCLCPrometheusConfig(context.TODO()) + Expect(err).To(BeNil()) + }) + }) }) }) diff --git a/controllers/uninstall.go b/controllers/uninstall.go index fde8a9b02..d99c2abe7 100644 --- a/controllers/uninstall.go +++ b/controllers/uninstall.go @@ -8,6 +8,8 @@ import ( backplanev1 "github.com/stolostron/backplane-operator/api/v1" "github.com/stolostron/backplane-operator/pkg/toggle" + apimeta "k8s.io/apimachinery/pkg/api/meta" + "sigs.k8s.io/controller-runtime/pkg/log" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -132,3 +134,33 @@ func (r *MultiClusterEngineReconciler) uninstall(backplaneConfig *backplanev1.Mu } return false, nil } + +// removeLegacyCLCPrometheusConfig will remove the CLC PrometheusRule and ServiceMonitor in the openshift-monitoring +// namespace. This configuration should be in the controller namespace instead. +func (r *MultiClusterEngineReconciler) removeLegacyCLCPrometheusConfig(ctx context.Context) error { + log := log.FromContext(ctx) + obj := &unstructured.Unstructured{} + obj.SetGroupVersionKind(schema.GroupVersionKind{ + Group: "monitoring.coreos.com", + Kind: "ServiceMonitor", + Version: "v1", + }) + obj.SetName("clusterlifecycle-state-metrics-v2") + obj.SetNamespace("openshift-monitoring") + + err := r.Client.Delete(ctx, obj) + if err != nil { + if !errors.IsNotFound(err) && !apimeta.IsNoMatchError(err) { + log.Error( + err, + "Error while deleting ServiceMonitor: clusterlifecycle-state-metrics-v2", + ) + + return err + } + } else { + log.Info("Deleted the legacy CLC Prometheus configuration", "kind", "ServiceMonitor", "name", obj.GetName()) + } + + return nil +}