Skip to content

Commit

Permalink
Remove service monitor from openshift-monitoring ns (#526)
Browse files Browse the repository at this point in the history
test fix

Signed-off-by: Cameron Wall <[email protected]>
  • Loading branch information
cameronmwall authored Oct 3, 2023
1 parent f22631c commit 4d41ef7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/backplaneconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions controllers/backplaneconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})
})
32 changes: 32 additions & 0 deletions controllers/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 4d41ef7

Please sign in to comment.