Skip to content

Commit

Permalink
LOG-6280: Only cleanup dashboard when owned by 'this' operator version
Browse files Browse the repository at this point in the history
  • Loading branch information
jcantrill committed Jan 10, 2025
1 parent 18f8a02 commit 552e818
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}()
Expand Down Expand Up @@ -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
Expand Down
35 changes: 25 additions & 10 deletions internal/metrics/dashboard/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit 552e818

Please sign in to comment.