forked from rook/rook
monitoring: allow overriding monitoring labels
The templates for the mgr-generated ServiceMonitor and PrometheusRule objects included the labels prometheus and team, making it impossible to override them as user. This adds a new method `OverwriteApplyToObjectMeta` to pkg/apis/ceph.rook.io/v1.Labels, which, contrary to the existing `ApplyToObjectMeta` method, overwrites existing labels. Closes: https://github.com/rook/rook/issues/8502 Signed-off-by: Mara Sophie Grosch <littlefox@lf-net.org>
This commit is contained in:
@@ -88,6 +88,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.
|
||||
|
||||
@@ -153,6 +153,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",
|
||||
|
||||
@@ -465,7 +465,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
|
||||
@@ -501,7 +501,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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user