Skip to content

Commit

Permalink
Merge pull request #9240 from LittleFox94/backport-9180
Browse files Browse the repository at this point in the history
monitoring: allow overriding monitoring labels
  • Loading branch information
leseb committed Nov 24, 2021
2 parents ec6f55e + 7da43aa commit 9c0eb3e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pkg/apis/rook.io/labels.go
Expand Up @@ -44,6 +44,16 @@ func (a Labels) ApplyToObjectMeta(t *metav1.ObjectMeta) {
}
}

// OverwriteApplyToObjectMeta adds labels to object meta, overwriting keys that are already defined.
func (a Labels) OverwriteApplyToObjectMeta(t *metav1.ObjectMeta) {
if t.Labels == nil {
t.Labels = map[string]string{}
}
for k, v := range a {
t.Labels[k] = v
}
}

// Merge returns a Labels which results from merging the attributes of the
// original Labels with the attributes of the supplied one. The supplied
// Labels attributes will override the original ones if defined.
Expand Down
Expand Up @@ -77,6 +77,60 @@ func TestLabelsApply(t *testing.T) {
}
}

func TestLabelsOverwriteApply(t *testing.T) {
tcs := []struct {
name string
target *metav1.ObjectMeta
input Labels
expected Labels
}{
{
name: "it should be able to update meta with no label",
target: &metav1.ObjectMeta{},
input: Labels{
"foo": "bar",
},
expected: Labels{
"foo": "bar",
},
},
{
name: "it should keep the original labels when new labels are set",
target: &metav1.ObjectMeta{
Labels: Labels{
"foo": "bar",
},
},
input: Labels{
"hello": "world",
},
expected: Labels{
"foo": "bar",
"hello": "world",
},
},
{
name: "it should overwrite the existing keys",
target: &metav1.ObjectMeta{
Labels: Labels{
"foo": "bar",
},
},
input: Labels{
"foo": "baz",
},
expected: Labels{
"foo": "baz",
},
},
}

for _, tc := range tcs {
tc.input.OverwriteApplyToObjectMeta(tc.target)
assert.Equal(t, map[string]string(tc.expected), tc.target.Labels)
}
}

func TestLabelsMerge(t *testing.T) {
testLabelsPart1 := Labels{
"foo": "bar",
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/ceph/cluster/mgr/mgr.go
Expand Up @@ -469,7 +469,7 @@ func (c *Cluster) EnableServiceMonitor(activeDaemon string) error {
}
serviceMonitor.SetName(AppName)
serviceMonitor.SetNamespace(c.clusterInfo.Namespace)
cephv1.GetMonitoringLabels(c.spec.Labels).ApplyToObjectMeta(&serviceMonitor.ObjectMeta)
cephv1.GetMonitoringLabels(c.spec.Labels).OverwriteApplyToObjectMeta(&serviceMonitor.ObjectMeta)

if c.spec.External.Enable {
serviceMonitor.Spec.Endpoints[0].Port = controller.ServiceExternalMetricName
Expand Down Expand Up @@ -505,7 +505,7 @@ func (c *Cluster) DeployPrometheusRule(name, namespace string) error {
if err != nil {
return errors.Wrapf(err, "failed to set owner reference to prometheus rule %q", prometheusRule.Name)
}
cephv1.GetMonitoringLabels(c.spec.Labels).ApplyToObjectMeta(&prometheusRule.ObjectMeta)
cephv1.GetMonitoringLabels(c.spec.Labels).OverwriteApplyToObjectMeta(&prometheusRule.ObjectMeta)
if _, err := k8sutil.CreateOrUpdatePrometheusRule(prometheusRule); err != nil {
return errors.Wrap(err, "prometheus rule could not be deployed")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/k8sutil/prometheus_test.go
Expand Up @@ -32,6 +32,7 @@ func TestGetServiceMonitor(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "rook-ceph-mgr", servicemonitor.GetName())
assert.Equal(t, "rook-ceph", servicemonitor.GetNamespace())
assert.NotNil(t, servicemonitor.GetLabels())
assert.NotNil(t, servicemonitor.Spec.NamespaceSelector.MatchNames)
assert.NotNil(t, servicemonitor.Spec.Endpoints)
}
Expand Down

0 comments on commit 9c0eb3e

Please sign in to comment.