Skip to content

Commit

Permalink
Merge pull request #399 from csibbitt/OSPRH-6753/cleanup_dashboards
Browse files Browse the repository at this point in the history
Cleanup dashboards on disable/delete
  • Loading branch information
openshift-merge-bot[bot] authored Jun 11, 2024
2 parents 73eba08 + 25a354f commit 286eda2
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 28 deletions.
17 changes: 12 additions & 5 deletions controllers/metricstorage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ func (r *MetricStorageReconciler) reconcileDelete(
) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service delete")

if res, err := metricstorage.DeleteDashboardObjects(ctx, instance, helper); err != nil {
return res, err
}

// Service is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
Expand Down Expand Up @@ -490,14 +495,15 @@ func (r *MetricStorageReconciler) reconcileNormal(
instance.Status.Conditions.MarkTrue(telemetryv1.ScrapeConfigReadyCondition, condition.ReadyMessage)

if !instance.Spec.MonitoringStack.DashboardsEnabled {
if res, err := metricstorage.DeleteDashboardObjects(ctx, instance, helper); err != nil {
return res, err
}
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardPrometheusRuleReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardDatasourceReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardDefinitionReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardPluginReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
} else {

const dashboardArtifactsNamespace = "openshift-config-managed"

// Deploy dashboard UI plugin from OBO
// TODO: Use the following instead of Unstructured{} after COO 0.2.0
// =====
Expand Down Expand Up @@ -536,6 +542,7 @@ func (r *MetricStorageReconciler) reconcileNormal(
if op != controllerutil.OperationResultNone {
Log.Info(fmt.Sprintf("Dashboard Plugin definition %s successfully changed - operation: %s", uiPluginObj.GetName(), string(op)))
}

// Deploy PrometheusRule for dashboards
err = r.ensureWatches(ctx, "prometheusrules.monitoring.rhobs", &monv1.PrometheusRule{}, eventHandler)
if err != nil {
Expand Down Expand Up @@ -572,15 +579,15 @@ func (r *MetricStorageReconciler) reconcileNormal(
datasourceCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: datasourceName,
Namespace: dashboardArtifactsNamespace,
Namespace: metricstorage.DashboardArtifactsNamespace,
},
}
dataSourceSuccess := false
op, err = controllerutil.CreateOrPatch(ctx, r.Client, datasourceCM, func() error {
datasourceCM.ObjectMeta.Labels = map[string]string{
"console.openshift.io/dashboard-datasource": "true",
}
datasourceCM.Data, err = metricstorage.DashboardDatasourceData(ctx, r.Client, instance, datasourceName, dashboardArtifactsNamespace)
datasourceCM.Data, err = metricstorage.DashboardDatasourceData(ctx, r.Client, instance, datasourceName, metricstorage.DashboardArtifactsNamespace)
return err
})
if err != nil {
Expand Down Expand Up @@ -610,7 +617,7 @@ func (r *MetricStorageReconciler) reconcileNormal(
dashboardCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: dashboardName,
Namespace: dashboardArtifactsNamespace,
Namespace: metricstorage.DashboardArtifactsNamespace,
},
}
op, err = controllerutil.CreateOrPatch(ctx, r.Client, dashboardCM, func() error {
Expand Down
28 changes: 5 additions & 23 deletions controllers/telemetry_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
ceilometer "github.com/openstack-k8s-operators/telemetry-operator/pkg/ceilometer"
logging "github.com/openstack-k8s-operators/telemetry-operator/pkg/logging"
utils "github.com/openstack-k8s-operators/telemetry-operator/pkg/utils"
obov1 "github.com/rhobs/observability-operator/pkg/apis/monitoring/v1alpha1"
)

Expand Down Expand Up @@ -230,25 +231,6 @@ func (r *TelemetryReconciler) reconcileNormal(ctx context.Context, instance *tel
return ctrl.Result{}, nil
}

// ensureDeleted - Delete the object which in turn will clean the sub resources
func ensureDeleted(ctx context.Context, helper *helper.Helper, obj client.Object) (ctrl.Result, error) {
key := client.ObjectKeyFromObject(obj)
if err := helper.GetClient().Get(ctx, key, obj); err != nil {
if k8s_errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
// Delete the object
if obj.GetDeletionTimestamp().IsZero() {
if err := helper.GetClient().Delete(ctx, obj); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil

}

// reconcileCeilometer ...
func (r TelemetryReconciler) reconcileCeilometer(ctx context.Context, instance *telemetryv1.Telemetry, helper *helper.Helper) (ctrl.Result, error) {
const (
Expand All @@ -263,7 +245,7 @@ func (r TelemetryReconciler) reconcileCeilometer(ctx context.Context, instance *
}

if !instance.Spec.Ceilometer.Enabled {
if res, err := ensureDeleted(ctx, helper, ceilometerInstance); err != nil {
if res, err := utils.EnsureDeleted(ctx, helper, ceilometerInstance); err != nil {
return res, err
}
instance.Status.Conditions.Remove(telemetryv1.CeilometerReadyCondition)
Expand Down Expand Up @@ -340,7 +322,7 @@ func (r TelemetryReconciler) reconcileAutoscaling(ctx context.Context, instance
}

if !instance.Spec.Autoscaling.Enabled {
if res, err := ensureDeleted(ctx, helper, autoscalingInstance); err != nil {
if res, err := utils.EnsureDeleted(ctx, helper, autoscalingInstance); err != nil {
return res, err
}
instance.Status.Conditions.Remove(telemetryv1.AutoscalingReadyCondition)
Expand Down Expand Up @@ -415,7 +397,7 @@ func (r TelemetryReconciler) reconcileMetricStorage(ctx context.Context, instanc
}

if !instance.Spec.MetricStorage.Enabled {
if res, err := ensureDeleted(ctx, helper, metricStorageInstance); err != nil {
if res, err := utils.EnsureDeleted(ctx, helper, metricStorageInstance); err != nil {
return res, err
}
instance.Status.Conditions.Remove(telemetryv1.MetricStorageReadyCondition)
Expand Down Expand Up @@ -501,7 +483,7 @@ func (r TelemetryReconciler) reconcileLogging(ctx context.Context, instance *tel
}

if !instance.Spec.Logging.Enabled {
if res, err := ensureDeleted(ctx, helper, loggingInstance); err != nil {
if res, err := utils.EnsureDeleted(ctx, helper, loggingInstance); err != nil {
return res, err
}
instance.Status.Conditions.Remove(telemetryv1.LoggingReadyCondition)
Expand Down
72 changes: 72 additions & 0 deletions pkg/metricstorage/dashboard_objects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metricstorage

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
utils "github.com/openstack-k8s-operators/telemetry-operator/pkg/utils"
monv1 "github.com/rhobs/obo-prometheus-operator/pkg/apis/monitoring/v1"
)

const DashboardArtifactsNamespace = "openshift-config-managed"

func DeleteDashboardObjects(ctx context.Context, instance *telemetryv1.MetricStorage, helper *helper.Helper) (ctrl.Result, error) {
fmt.Printf("%v %v %v", ctx, instance, helper)
promRule := &monv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Name,
Namespace: instance.Namespace,
},
}
if res, err := utils.EnsureDeleted(ctx, helper, promRule); err != nil {
return res, err
}

datasourceName := instance.Namespace + "-" + instance.Name + "-datasource"
datasourceCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: datasourceName,
Namespace: DashboardArtifactsNamespace,
},
}
if res, err := utils.EnsureDeleted(ctx, helper, datasourceCM); err != nil {
return res, err
}

var dashboards = []string{"grafana-dashboard-openstack-cloud", "grafana-dashboard-openstack-node", "grafana-dashboard-openstack-vm"}
for _, name := range dashboards {
dashboardCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: DashboardArtifactsNamespace,
},
}
if res, err := utils.EnsureDeleted(ctx, helper, dashboardCM); err != nil {
return res, err
}
}

return ctrl.Result{}, nil
}
46 changes: 46 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

import (
"context"

k8s_errors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
)

// EnsureDeleted - Delete the object which in turn will clean the sub resources
func EnsureDeleted(ctx context.Context, helper *helper.Helper, obj client.Object) (ctrl.Result, error) {
key := client.ObjectKeyFromObject(obj)
if err := helper.GetClient().Get(ctx, key, obj); err != nil {
if k8s_errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
// Delete the object
if obj.GetDeletionTimestamp().IsZero() {
if err := helper.GetClient().Delete(ctx, obj); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil

}
14 changes: 14 additions & 0 deletions tests/kuttl/suites/metricstorage/tests/02-disable-dashboards.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: telemetry.openstack.org/v1beta1
kind: MetricStorage
metadata:
name: telemetry-kuttl
spec:
monitoringStack:
alertingEnabled: true
dashboardsEnabled: false
scrapeInterval: 30s
storage:
strategy: persistent
retention: 24h
persistent:
pvcStorageRequest: 20G
32 changes: 32 additions & 0 deletions tests/kuttl/suites/metricstorage/tests/02-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: monitoring.rhobs/v1
kind: PrometheusRule
metadata:
name: telemetry-kuttl
spec:
groups:
- name: osp-node-exporter-dashboard.rules
- name: osp-ceilometer-dashboard.rules
---
apiVersion: v1
kind: ConfigMap
metadata:
name: telemetry-kuttl-tests-telemetry-kuttl-datasource
namespace: openshift-config-managed
---
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-dashboard-openstack-cloud
namespace: openshift-config-managed
---
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-dashboard-openstack-node
namespace: openshift-config-managed
---
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-dashboard-openstack-vm
namespace: openshift-config-managed

0 comments on commit 286eda2

Please sign in to comment.