Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitoring: allow overriding monitoring labels #9240

Merged
merged 1 commit into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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