Skip to content

Commit

Permalink
Merge pull request #352 from periklis/backport-pr-14279-5.9
Browse files Browse the repository at this point in the history
[release-5.9] Backport PR grafana#14279
  • Loading branch information
periklis authored Oct 1, 2024
2 parents 89fb029 + 2ea9c0c commit a09b42c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 53 deletions.
8 changes: 8 additions & 0 deletions operator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Main

## Release 5.9.8

- [14279](https://github.com/grafana/loki/pull/14279) **periklis**: fix(operator): Add missing groupBy label for all rules on OpenShift

## Release 5.9.7

No changes.

## Release 5.9.6

- [14042](https://github.com/grafana/loki/pull/14042) **periklis**: feat(operator): Update Loki operand to v3.1.1
Expand Down
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(r *lokiv1.RecordingRule, labels map[string]string) {
for groupIdx, group := range r.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
}
r.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

This file was deleted.

0 comments on commit a09b42c

Please sign in to comment.