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

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

Merged
merged 4 commits into from
Mar 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions operator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 18 additions & 4 deletions operator/internal/manifests/internal/rules/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"gopkg.in/yaml.v2"
)

const tenantLabel = "tenantId"

type alertingRuleSpec struct {
Groups []*lokiv1.AlertingRuleGroup `json:"groups"`
}
Expand All @@ -16,27 +18,39 @@ 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
}

// 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
Expand Down
5 changes: 3 additions & 2 deletions operator/internal/manifests/internal/rules/marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rules_test

import (
"fmt"
"testing"

lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
Expand Down Expand Up @@ -29,6 +28,7 @@ groups:
labels:
environment: production
severity: page
tenantId: a-tenant
- expr: |-
sum(rate({app="foo", env="production"} |= "error" [5m])) by (job)
/
Expand All @@ -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",
Expand Down Expand Up @@ -141,7 +143,6 @@ groups:
}

cfg, err := rules.MarshalRecordingRule(r)
fmt.Print(cfg)
require.NoError(t, err)
require.YAMLEq(t, expCfg, cfg)
}