Alerting: Prevent updating rule uid matcher for silences (#88519)

Prevents updating the `__alert_rule_uid__` equality matcher (used for rule-specific silences) on existing silences
This commit is contained in:
Matthew Jacobson
2024-06-03 17:39:06 -04:00
committed by GitHub
parent f458e57523
commit 31d5dd0a12
4 changed files with 185 additions and 0 deletions
+24
View File
@@ -16,6 +16,7 @@ import (
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
alertingModels "github.com/grafana/alerting/models"
"github.com/grafana/grafana/pkg/expr"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/folder"
@@ -1054,6 +1055,29 @@ func (n SilenceMutators) WithMatcher(name, value string, matchType labels.MatchT
s.Silence.Matchers = append(s.Silence.Matchers, &m)
}
}
func (n SilenceMutators) WithRuleUID(value string) Mutator[Silence] {
return func(s *Silence) {
name := alertingModels.RuleUIDLabel
m := amv2.Matcher{
Name: &name,
Value: &value,
IsRegex: util.Pointer(false),
IsEqual: util.Pointer(true),
}
for _, matcher := range s.Silence.Matchers {
if isRuleUIDMatcher(*matcher) {
*matcher = m
return
}
}
s.Silence.Matchers = append(s.Silence.Matchers, &m)
}
}
func (n SilenceMutators) Expired() Mutator[Silence] {
return func(s *Silence) {
s.EndsAt = util.Pointer(strfmt.DateTime(time.Now().Add(-time.Minute)))
}
}
func (n SilenceMutators) WithEmptyId() Mutator[Silence] {
return func(s *Silence) {