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

fix(operator): Add missing groupBy label for all rules on OpenShift #14279

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 25 additions & 16 deletions operator/internal/manifests/openshift/alertingrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ 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
appendAlertingRuleLabels(ar, map[string]string{
opaDefaultLabelMatcher: ar.Namespace,
ocpMonitoringGroupByLabel: ar.Namespace,
})
case tenantInfrastructure, tenantAudit, tenantNetwork:
appendAlertingRuleLabels(ar, map[string]string{
ocpMonitoringGroupByLabel: ar.Namespace,
})
default:
// Do nothing
}
}

func appendAlertingRuleLabels(ar *lokiv1.AlertingRule, labels map[string]string) {
for groupIdx, group := range ar.Spec.Groups {
for ruleIdx, rule := range group.Rules {
if rule.Labels == nil {
rule.Labels = map[string]string{}
}

for name, value := range labels {
rule.Labels[name] = value
}

group.Rules[ruleIdx] = rule
}
ar.Spec.Groups[groupIdx] = group
}
}
36 changes: 35 additions & 1 deletion operator/internal/manifests/openshift/alertingrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
{
Alert: "alert",
Labels: map[string]string{
opaDefaultLabelMatcher: "test-ns",
opaDefaultLabelMatcher: "test-ns",
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
Expand All @@ -57,6 +58,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: tenantInfrastructure,
Groups: []*lokiv1.AlertingRuleGroup{
Expand All @@ -72,6 +76,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: tenantInfrastructure,
Groups: []*lokiv1.AlertingRuleGroup{
Expand All @@ -80,6 +87,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
Rules: []*lokiv1.AlertingRuleGroupSpec{
{
Alert: "alert",
Labels: map[string]string{
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
},
Expand All @@ -89,6 +99,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: tenantAudit,
Groups: []*lokiv1.AlertingRuleGroup{
Expand All @@ -104,6 +117,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: tenantAudit,
Groups: []*lokiv1.AlertingRuleGroup{
Expand All @@ -112,6 +128,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
Rules: []*lokiv1.AlertingRuleGroupSpec{
{
Alert: "alert",
Labels: map[string]string{
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
},
Expand All @@ -121,6 +140,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: tenantNetwork,
Groups: []*lokiv1.AlertingRuleGroup{
Expand All @@ -136,6 +158,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: tenantNetwork,
Groups: []*lokiv1.AlertingRuleGroup{
Expand All @@ -144,6 +169,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
Rules: []*lokiv1.AlertingRuleGroupSpec{
{
Alert: "alert",
Labels: map[string]string{
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
},
Expand All @@ -153,6 +181,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: "unknown",
Groups: []*lokiv1.AlertingRuleGroup{
Expand All @@ -168,6 +199,9 @@ func TestAlertingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.AlertingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.AlertingRuleSpec{
TenantID: "unknown",
Groups: []*lokiv1.AlertingRuleGroup{
Expand Down
17 changes: 9 additions & 8 deletions operator/internal/manifests/openshift/opa_openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
)

const (
envRelatedImageOPA = "RELATED_IMAGE_OPA"
defaultOPAImage = "quay.io/observatorium/opa-openshift:latest"
opaContainerName = "opa"
opaDefaultPackage = "lokistack"
opaDefaultAPIGroup = "loki.grafana.com"
opaMetricsPortName = "opa-metrics"
opaDefaultLabelMatcher = "kubernetes_namespace_name"
opaNetworkLabelMatchers = "SrcK8S_Namespace,DstK8S_Namespace"
envRelatedImageOPA = "RELATED_IMAGE_OPA"
defaultOPAImage = "quay.io/observatorium/opa-openshift:latest"
opaContainerName = "opa"
opaDefaultPackage = "lokistack"
opaDefaultAPIGroup = "loki.grafana.com"
opaMetricsPortName = "opa-metrics"
opaDefaultLabelMatcher = "kubernetes_namespace_name"
opaNetworkLabelMatchers = "SrcK8S_Namespace,DstK8S_Namespace"
ocpMonitoringGroupByLabel = "namespace"
)

func newOPAOpenShiftContainer(mode lokiv1.ModeType, secretVolumeName, tlsDir, minTLSVersion, ciphers string, withTLS bool, adminGroups []string) corev1.Container {
Expand Down
36 changes: 36 additions & 0 deletions operator/internal/manifests/openshift/recordingrule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package openshift

import lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"

func RecordingRuleTenantLabels(r *lokiv1.RecordingRule) {
switch r.Spec.TenantID {
case tenantApplication:
appendRecordingRuleLabels(r, map[string]string{
opaDefaultLabelMatcher: r.Namespace,
ocpMonitoringGroupByLabel: r.Namespace,
})
case tenantInfrastructure, tenantAudit, tenantNetwork:
appendRecordingRuleLabels(r, map[string]string{
ocpMonitoringGroupByLabel: r.Namespace,
})
default:
// Do nothing
}
}

func appendRecordingRuleLabels(ar *lokiv1.RecordingRule, labels map[string]string) {
periklis marked this conversation as resolved.
Show resolved Hide resolved
for groupIdx, group := range ar.Spec.Groups {
for ruleIdx, rule := range group.Rules {
if rule.Labels == nil {
rule.Labels = map[string]string{}
}

for name, value := range labels {
rule.Labels[name] = value
}

group.Rules[ruleIdx] = rule
}
ar.Spec.Groups[groupIdx] = group
}
}
36 changes: 35 additions & 1 deletion operator/internal/manifests/openshift/recordingrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
{
Record: "record",
Labels: map[string]string{
opaDefaultLabelMatcher: "test-ns",
opaDefaultLabelMatcher: "test-ns",
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
Expand All @@ -57,6 +58,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: tenantInfrastructure,
Groups: []*lokiv1.RecordingRuleGroup{
Expand All @@ -72,6 +76,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: tenantInfrastructure,
Groups: []*lokiv1.RecordingRuleGroup{
Expand All @@ -80,6 +87,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
Rules: []*lokiv1.RecordingRuleGroupSpec{
{
Record: "record",
Labels: map[string]string{
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
},
Expand All @@ -89,6 +99,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: tenantAudit,
Groups: []*lokiv1.RecordingRuleGroup{
Expand All @@ -104,6 +117,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: tenantAudit,
Groups: []*lokiv1.RecordingRuleGroup{
Expand All @@ -112,6 +128,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
Rules: []*lokiv1.RecordingRuleGroupSpec{
{
Record: "record",
Labels: map[string]string{
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
},
Expand All @@ -121,6 +140,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: tenantNetwork,
Groups: []*lokiv1.RecordingRuleGroup{
Expand All @@ -136,6 +158,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: tenantNetwork,
Groups: []*lokiv1.RecordingRuleGroup{
Expand All @@ -144,6 +169,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
Rules: []*lokiv1.RecordingRuleGroupSpec{
{
Record: "record",
Labels: map[string]string{
ocpMonitoringGroupByLabel: "test-ns",
},
},
},
},
Expand All @@ -153,6 +181,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
{
rule: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: "unknown",
Groups: []*lokiv1.RecordingRuleGroup{
Expand All @@ -168,6 +199,9 @@ func TestRecordingRuleTenantLabels(t *testing.T) {
},
},
want: &lokiv1.RecordingRule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
},
Spec: lokiv1.RecordingRuleSpec{
TenantID: "unknown",
Groups: []*lokiv1.RecordingRuleGroup{
Expand Down
27 changes: 0 additions & 27 deletions operator/internal/manifests/openshift/recordngrule.go
periklis marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

Loading