mirror of https://github.com/grafana/loki
operator: Add rules labels filters for openshift application tenant (#9600)
Co-authored-by: Mohamed-Amine Bouqsimi <bouqsimiamine@gmail.com>pull/9751/head
parent
24e9efb907
commit
89a094c5fe
@ -0,0 +1,27 @@ |
|||||||
|
package openshift |
||||||
|
|
||||||
|
import lokiv1 "github.com/grafana/loki/operator/apis/loki/v1" |
||||||
|
|
||||||
|
func AlertingRuleTenantLabels(ar *lokiv1.AlertingRule) { |
||||||
|
switch ar.Spec.TenantID { |
||||||
|
case tenantApplication: |
||||||
|
for groupIdx, group := range ar.Spec.Groups { |
||||||
|
group := group |
||||||
|
for ruleIdx, rule := range group.Rules { |
||||||
|
rule := rule |
||||||
|
if rule.Labels == nil { |
||||||
|
rule.Labels = map[string]string{} |
||||||
|
} |
||||||
|
rule.Labels[opaDefaultLabelMatcher] = ar.Namespace |
||||||
|
group.Rules[ruleIdx] = rule |
||||||
|
} |
||||||
|
ar.Spec.Groups[groupIdx] = group |
||||||
|
} |
||||||
|
case tenantInfrastructure, tenantAudit: |
||||||
|
// Do nothing
|
||||||
|
case tenantNetwork: |
||||||
|
// Do nothing
|
||||||
|
default: |
||||||
|
// Do nothing
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,195 @@ |
|||||||
|
package openshift |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1" |
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||||
|
) |
||||||
|
|
||||||
|
func TestAlertingRuleTenantLabels(t *testing.T) { |
||||||
|
tt := []struct { |
||||||
|
rule *lokiv1.AlertingRule |
||||||
|
want *lokiv1.AlertingRule |
||||||
|
}{ |
||||||
|
{ |
||||||
|
rule: &lokiv1.AlertingRule{ |
||||||
|
ObjectMeta: metav1.ObjectMeta{ |
||||||
|
Namespace: "test-ns", |
||||||
|
}, |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantApplication, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
want: &lokiv1.AlertingRule{ |
||||||
|
ObjectMeta: metav1.ObjectMeta{ |
||||||
|
Namespace: "test-ns", |
||||||
|
}, |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantApplication, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
Labels: map[string]string{ |
||||||
|
opaDefaultLabelMatcher: "test-ns", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
rule: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantInfrastructure, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
want: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantInfrastructure, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
rule: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantAudit, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
want: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantAudit, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
rule: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantNetwork, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
want: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: tenantNetwork, |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
rule: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: "unknown", |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
want: &lokiv1.AlertingRule{ |
||||||
|
Spec: lokiv1.AlertingRuleSpec{ |
||||||
|
TenantID: "unknown", |
||||||
|
Groups: []*lokiv1.AlertingRuleGroup{ |
||||||
|
{ |
||||||
|
Name: "test-group", |
||||||
|
Rules: []*lokiv1.AlertingRuleGroupSpec{ |
||||||
|
{ |
||||||
|
Alert: "alert", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
} |
||||||
|
for _, tc := range tt { |
||||||
|
tc := tc |
||||||
|
t.Run(tc.rule.Spec.TenantID, func(t *testing.T) { |
||||||
|
t.Parallel() |
||||||
|
AlertingRuleTenantLabels(tc.rule) |
||||||
|
|
||||||
|
require.Equal(t, tc.want, tc.rule) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue