From 552e81806ebe0ec0c41e191d7feea6c33bb52cdb Mon Sep 17 00:00:00 2001 From: Jeff Cantrill Date: Fri, 10 Jan 2025 16:10:43 -0500 Subject: [PATCH] LOG-6280: Only cleanup dashboard when owned by 'this' operator version --- cmd/main.go | 6 ++-- internal/metrics/dashboard/dashboards.go | 35 +++++++++++++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index a40939b811..076dcc7abd 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -125,7 +125,7 @@ func main() { // Clean up defer func() { - if err := cleanUpResources(mgr.GetClient()); err != nil { + if err := cleanUpResources(mgr.GetClient(), mgr.GetAPIReader()); err != nil { log.V(3).Error(err, "error with resource cleanup") } }() @@ -219,9 +219,9 @@ func migrateManifestResources(k8sClient client.Client) { } } -func cleanUpResources(k8sClient client.Client) error { +func cleanUpResources(k8sClient client.Client, apiReader client.Reader) error { // Remove the dashboard config map - if err := dashboard.RemoveDashboardConfigMap(k8sClient); err != nil { + if err := dashboard.RemoveDashboardConfigMap(k8sClient, apiReader); err != nil { return err } return nil diff --git a/internal/metrics/dashboard/dashboards.go b/internal/metrics/dashboard/dashboards.go index dfbb21a1d3..a7feb24c34 100644 --- a/internal/metrics/dashboard/dashboards.go +++ b/internal/metrics/dashboard/dashboards.go @@ -4,18 +4,20 @@ import ( "context" _ "embed" "fmt" - staticlog "github.com/ViaQ/logerr/v2/log/static" + "github.com/openshift/cluster-logging-operator/internal/constants" "github.com/openshift/cluster-logging-operator/internal/reconcile" "github.com/openshift/cluster-logging-operator/internal/runtime" "github.com/openshift/cluster-logging-operator/internal/utils" "github.com/openshift/cluster-logging-operator/internal/utils/comparators" + "github.com/openshift/cluster-logging-operator/version" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/selection" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/handler" "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -60,7 +62,9 @@ func newDashboardConfigMap() *corev1.ConfigMap { ) runtime.NewConfigMapBuilder(cm). AddLabel("console.openshift.io/dashboard", "true"). - AddLabel(DashboardHashName, hash) + AddLabel(DashboardHashName, hash). + AddLabel(constants.LabelK8sVersion, version.Version). + AddLabel(constants.LabelK8sManagedBy, constants.ClusterLoggingOperator) return cm } @@ -83,14 +87,25 @@ func ReconcileForDashboards(k8sClient client.Client, reader client.Reader) error } // RemoveDashboardConfigMap removes the config map in the grafana dashboard -func RemoveDashboardConfigMap(c client.Client) (err error) { - cm := &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: DashboardName, - Namespace: DashboardNS, - }, +func RemoveDashboardConfigMap(c client.Client, r client.Reader) (err error) { + cm := newDashboardConfigMap() + s := labels.NewSelector() + for k, v := range cm.Labels { + r, _ := labels.NewRequirement(k, selection.Equals, []string{v}) + s = s.Add(*r) + } + options := &client.ListOptions{ + LabelSelector: s, + Namespace: DashboardNS, } - return c.Delete(context.TODO(), cm) + configMaps := &corev1.ConfigMapList{} + if err := r.List(context.TODO(), configMaps, options); err != nil { + return err + } + if len(configMaps.Items) > 0 { + return c.Delete(context.TODO(), cm) + } + return nil } // SetupWithManager sets up the controller with the Manager