|
|
|
@ -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 |
|
|
|
|