Skip to content

Commit

Permalink
Ignore case of cluster monitoring label
Browse files Browse the repository at this point in the history
This commit makes check for the monitoring label on the WMCO namespace accept
all valid string representations of booleans to provide better user experience.
Previously, not adhering to an all lowercase `true` value would result in WMCO
ignoring the label and not configuring Prometheus metrics for Windows nodes.
  • Loading branch information
saifshaikh48 authored and openshift-cherrypick-robot committed Mar 16, 2023
1 parent 428c6a6 commit 1cd8e73
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"context"
"encoding/json"
"strconv"

"github.com/pkg/errors"
monclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
Expand Down Expand Up @@ -236,21 +237,29 @@ func (c *Config) Configure(ctx context.Context) error {
return nil
}

// validate will verify if cluster monitoring is enabled in the operator namespace.
// If the label is not present, it will log and send warning events to the user.
// validate will verify if cluster monitoring is enabled in the operator namespace. If the label is set to false or not
// present, it will log and send warning events to the user. If the label holds a non-boolean value, returns an error.
func (c *Config) validate(ctx context.Context) (bool, error) {
// validate if metrics label is added to namespace
wmcoNamespace, err := c.CoreV1().Namespaces().Get(ctx, c.namespace, metav1.GetOptions{})
if err != nil {
return false, errors.Wrap(err, "error getting operator namespace")
}
if wmcoNamespace.Labels["openshift.io/cluster-monitoring"] != "true" {
metricsEnabled = false

labelValue := false
// if the label exists, update value from default of false
if value, ok := wmcoNamespace.Labels["openshift.io/cluster-monitoring"]; ok {
labelValue, err = strconv.ParseBool(value)
if err != nil {
return false, errors.Wrap(err, "monitoring label must have a boolean value")
}
}
if !labelValue {
c.recorder.Eventf(wmcoNamespace, v1.EventTypeWarning, "labelValidationFailed",
"Cluster monitoring openshift.io/cluster-monitoring=true label is not enabled in %s namespace", c.namespace)
return false, nil
}
return true, nil
metricsEnabled = labelValue
return metricsEnabled, nil
}

// createEndpoint creates an endpoint object in the operator namespace.
Expand Down

0 comments on commit 1cd8e73

Please sign in to comment.