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 16, 2025
1 parent 18f8a02 commit b8b5bd1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 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
33 changes: 23 additions & 10 deletions internal/metrics/dashboard/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ 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/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 +60,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 +85,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()

current := &corev1.ConfigMap{}
key := client.ObjectKeyFromObject(cm)
if err := r.Get(context.TODO(), key, current); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get %v configmap: %v", key, err)
}
return nil
}

if runtime.Labels(current).Includes(map[string]string{
constants.LabelK8sManagedBy: constants.ClusterLoggingOperator,
constants.LabelK8sVersion: version.Version,
}) {
return c.Delete(context.TODO(), cm)
}
return c.Delete(context.TODO(), cm)
return nil
}

// SetupWithManager sets up the controller with the Manager
Expand Down
21 changes: 19 additions & 2 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,25 @@ func GroupVersionKind(o runtime.Object) schema.GroupVersionKind {
return gvk
}

// Labels returns the labels map for object, guaratneed to be non-nil.
func Labels(o runtime.Object) map[string]string {
type ObjectLabels map[string]string

// Includes compares an object labels to those given and returns true if
// the object set includes the keys and also matches the values
func (objLabels ObjectLabels) Includes(other ObjectLabels) bool {
for includeKey, includeValue := range other {
if value, found := objLabels[includeKey]; found {
if value != includeValue {
return false
}
} else {
return false
}
}
return true
}

// Labels returns the labels map for object, guaranteed to be non-nil.
func Labels(o runtime.Object) ObjectLabels {
m := Meta(o)
l := m.GetLabels()
if l == nil {
Expand Down
33 changes: 33 additions & 0 deletions internal/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/openshift/cluster-logging-operator/internal/constants"
"github.com/openshift/cluster-logging-operator/test"
. "github.com/openshift/cluster-logging-operator/test/matchers"
"github.com/openshift/cluster-logging-operator/version"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -35,6 +37,37 @@ var _ = Describe("Object", func() {
Entry("JSON string ns", test.JSONLine(nsFoo), nsFoo),
)

DescribeTable("#Labels.Includes",
func(success bool, matches map[string]string) {
obj := NewNamespace("test")
obj.Labels = map[string]string{
"foo": "bar",
constants.LabelK8sManagedBy: constants.ClusterLoggingOperator,
constants.LabelK8sVersion: version.Version,
}
Expect(Labels(obj).Includes(matches)).To(Equal(success))
},
Entry("matches a subset", true, map[string]string{
constants.LabelK8sManagedBy: constants.ClusterLoggingOperator,
constants.LabelK8sVersion: version.Version,
}),
Entry("matches entire set", true, map[string]string{
"foo": "bar",
constants.LabelK8sManagedBy: constants.ClusterLoggingOperator,
constants.LabelK8sVersion: version.Version,
}),
Entry("fails a superset", false, map[string]string{
"foo": "bar",
constants.LabelK8sManagedBy: constants.ClusterLoggingOperator,
constants.LabelK8sVersion: version.Version,
"xyz": "abc",
}),
Entry("fails a subset", false, map[string]string{
constants.LabelK8sManagedBy: constants.ClusterLoggingOperator,
constants.LabelK8sVersion: "someother",
}),
)

It("panics on bad manifest string", func() {
Expect(func() { _ = Decode("bad manifest") }).To(Panic())
})
Expand Down

0 comments on commit b8b5bd1

Please sign in to comment.