operator: Add alertingrule tenant id label for all rules (#8748)

pull/8815/head
Periklis Tsirakidis 2 years ago committed by GitHub
parent cb6f7a1a77
commit 52f6b8253c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      operator/CHANGELOG.md
  2. 22
      operator/internal/manifests/internal/rules/marshal.go
  3. 5
      operator/internal/manifests/internal/rules/marshal_test.go

@ -1,5 +1,6 @@
## Main
- [8748](https://github.com/grafana/loki/pull/8748) **periklis**: Add alertingrule tenant id label for all rules
- [8743](https://github.com/grafana/loki/pull/8743) **periklis**: Add alerting style guide validation
- [8192](https://github.com/grafana/loki/pull/8192) **jotak**: Allow multiple matchers for multi-tenancy with Network tenant (OpenShift)
- [8800](https://github.com/grafana/loki/pull/8800) **aminesnow**: Promote AlertingRules, RecordingRules and RulerConfig from v1beta1 to v1

@ -6,6 +6,8 @@ import (
"gopkg.in/yaml.v2"
)
const tenantLabel = "tenantId"
type alertingRuleSpec struct {
Groups []*lokiv1.AlertingRuleGroup `json:"groups"`
}
@ -16,13 +18,24 @@ type recordingRuleSpec struct {
// MarshalAlertingRule returns the alerting rule groups marshaled into YAML or an error.
func MarshalAlertingRule(a lokiv1.AlertingRule) (string, error) {
aa := a.DeepCopy()
ar := alertingRuleSpec{
Groups: a.Spec.Groups,
Groups: aa.Spec.Groups,
}
for _, group := range ar.Groups {
for _, rule := range group.Rules {
if rule.Labels == nil {
rule.Labels = map[string]string{}
}
rule.Labels[tenantLabel] = aa.Spec.TenantID
}
}
content, err := yaml.Marshal(ar)
if err != nil {
return "", kverrors.Wrap(err, "failed to marshal alerting rule", "name", a.Name, "namespace", a.Namespace)
return "", kverrors.Wrap(err, "failed to marshal alerting rule", "name", aa.Name, "namespace", aa.Namespace)
}
return string(content), nil
@ -30,13 +43,14 @@ func MarshalAlertingRule(a lokiv1.AlertingRule) (string, error) {
// MarshalRecordingRule returns the recording rule groups marshaled into YAML or an error.
func MarshalRecordingRule(a lokiv1.RecordingRule) (string, error) {
aa := a.DeepCopy()
ar := recordingRuleSpec{
Groups: a.Spec.Groups,
Groups: aa.Spec.Groups,
}
content, err := yaml.Marshal(ar)
if err != nil {
return "", kverrors.Wrap(err, "failed to marshal recording rule", "name", a.Name, "namespace", a.Namespace)
return "", kverrors.Wrap(err, "failed to marshal recording rule", "name", aa.Name, "namespace", aa.Namespace)
}
return string(content), nil

@ -1,7 +1,6 @@
package rules_test
import (
"fmt"
"testing"
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
@ -29,6 +28,7 @@ groups:
labels:
environment: production
severity: page
tenantId: a-tenant
- expr: |-
sum(rate({app="foo", env="production"} |= "error" [5m])) by (job)
/
@ -42,10 +42,12 @@ groups:
labels:
environment: production
severity: low
tenantId: a-tenant
`
a := lokiv1.AlertingRule{
Spec: lokiv1.AlertingRuleSpec{
TenantID: "a-tenant",
Groups: []*lokiv1.AlertingRuleGroup{
{
Name: "an-alert",
@ -141,7 +143,6 @@ groups:
}
cfg, err := rules.MarshalRecordingRule(r)
fmt.Print(cfg)
require.NoError(t, err)
require.YAMLEq(t, expCfg, cfg)
}

Loading…
Cancel
Save