diff --git a/controllers/logging/elasticsearch_controller.go b/controllers/logging/elasticsearch_controller.go index c326c0f03..ee37e104a 100644 --- a/controllers/logging/elasticsearch_controller.go +++ b/controllers/logging/elasticsearch_controller.go @@ -41,6 +41,7 @@ func (r *ElasticsearchReconciler) Reconcile(request ctrl.Request) (ctrl.Result, if apierrors.IsNotFound(err) { log.Info("Flushing nodes", "objectKey", request.NamespacedName) k8shandler.FlushNodes(request.NamespacedName.Name, request.NamespacedName.Namespace) + k8shandler.RemoveDashboardConfigMap(r.Client) return ctrl.Result{}, nil } diff --git a/internal/k8shandler/dashboards.go b/internal/k8shandler/dashboards.go index 05d4b4eae..f4ef8b395 100644 --- a/internal/k8shandler/dashboards.go +++ b/internal/k8shandler/dashboards.go @@ -1,16 +1,22 @@ package k8shandler import ( + "context" "io/ioutil" "github.com/ViaQ/logerr/kverrors" + "github.com/ViaQ/logerr/log" "github.com/openshift/elasticsearch-operator/internal/utils" v1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" ) const ( defaultElasticDashboardFile = "/etc/elasticsearch-operator/files/dashboards/logging-dashboard-elasticsearch.json" + grafanaCMName = "grafana-dashboard-elasticsearch" + grafanaCMNameSpace = "openshift-config-managed" ) // CreateOrUpdateDashboards creates/updates the cluster logging dashboard ConfigMap @@ -26,8 +32,8 @@ func (er *ElasticsearchRequest) CreateOrUpdateDashboards() error { APIVersion: v1.SchemeGroupVersion.String(), }, ObjectMeta: metav1.ObjectMeta{ - Name: "grafana-dashboard-elasticsearch", - Namespace: "openshift-config-managed", + Name: grafanaCMName, + Namespace: grafanaCMNameSpace, Labels: map[string]string{ "console.openshift.io/dashboard": "true", }, @@ -37,7 +43,20 @@ func (er *ElasticsearchRequest) CreateOrUpdateDashboards() error { }, } - er.cluster.AddOwnerRefTo(cm) - return er.CreateOrUpdateConfigMap(cm) } + +// RemoveDashboardConfigMap removes the config map in the grafana dashboard +func RemoveDashboardConfigMap(client client.Client) { + cm := getConfigmap(grafanaCMName, grafanaCMNameSpace, client) + if cm == nil { + return + } + err := client.Delete(context.TODO(), cm) + if err != nil { + if apierrors.IsNotFound(err) { + return + } + log.Error(err, "error deleting grafana configmap") + } +}