Alerting: Add rule conversion package (#100224)

This commit is contained in:
Alexander Akhmetov
2025-02-12 19:38:48 +02:00
committed by GitHub
parent e2a101cde3
commit 3cc4320aa9
6 changed files with 584 additions and 6 deletions
+11 -1
View File
@@ -295,7 +295,8 @@ type AlertRule struct {
}
type AlertRuleMetadata struct {
EditorSettings EditorSettings `json:"editor_settings"`
EditorSettings EditorSettings `json:"editor_settings"`
PrometheusStyleRule *PrometheusStyleRule `json:"prometheus_style_rule,omitempty"`
}
type EditorSettings struct {
@@ -303,6 +304,10 @@ type EditorSettings struct {
SimplifiedNotificationsSection bool `json:"simplified_notifications_section"`
}
type PrometheusStyleRule struct {
OriginalRuleDefinition string `json:"original_rule_definition,omitempty"`
}
// Namespaced describes a class of resources that are stored in a specific namespace.
type Namespaced interface {
GetNamespaceUID() string
@@ -748,6 +753,11 @@ func (alertRule *AlertRule) Copy() *AlertRule {
}
}
if alertRule.Metadata.PrometheusStyleRule != nil {
prometheusStyleRule := *alertRule.Metadata.PrometheusStyleRule
result.Metadata.PrometheusStyleRule = &prometheusStyleRule
}
for _, s := range alertRule.NotificationSettings {
result.NotificationSettings = append(result.NotificationSettings, CopyNotificationSettings(s))
}
+30 -5
View File
@@ -841,7 +841,7 @@ func TestDiff(t *testing.T) {
}
})
t.Run("should detect changes in Metadata", func(t *testing.T) {
t.Run("should detect changes in Metadata.EditorSettings", func(t *testing.T) {
rule1 := RuleGen.With(RuleGen.WithMetadata(AlertRuleMetadata{EditorSettings: EditorSettings{
SimplifiedQueryAndExpressionsSection: false,
SimplifiedNotificationsSection: false,
@@ -858,6 +858,21 @@ func TestDiff(t *testing.T) {
"Metadata.EditorSettings.SimplifiedNotificationsSection",
}, diff.Paths())
})
t.Run("should detect changes in Metadata.PrometheusStyleRule", func(t *testing.T) {
rule1 := RuleGen.With(RuleGen.WithMetadata(AlertRuleMetadata{PrometheusStyleRule: &PrometheusStyleRule{
OriginalRuleDefinition: "data",
}})).GenerateRef()
rule2 := CopyRule(rule1, RuleGen.WithMetadata(AlertRuleMetadata{PrometheusStyleRule: &PrometheusStyleRule{
OriginalRuleDefinition: "updated data",
}}))
diff := rule1.Diff(rule2)
assert.ElementsMatch(t, []string{
"Metadata.PrometheusStyleRule.OriginalRuleDefinition",
}, diff.Paths())
})
}
func TestSortByGroupIndex(t *testing.T) {
@@ -940,11 +955,21 @@ func TestAlertRuleGetKeyWithGroup(t *testing.T) {
}
func TestAlertRuleCopy(t *testing.T) {
for i := 0; i < 100; i++ {
rule := RuleGen.GenerateRef()
t.Run("should return a copy of the rule", func(t *testing.T) {
for i := 0; i < 100; i++ {
rule := RuleGen.GenerateRef()
copied := rule.Copy()
require.Empty(t, rule.Diff(copied))
}
})
t.Run("should create a copy of the prometheus rule definition from the metadata", func(t *testing.T) {
rule := RuleGen.With(RuleGen.WithMetadata(AlertRuleMetadata{PrometheusStyleRule: &PrometheusStyleRule{
OriginalRuleDefinition: "data",
}})).GenerateRef()
copied := rule.Copy()
require.Empty(t, rule.Diff(copied))
}
require.NotSame(t, rule.Metadata.PrometheusStyleRule, copied.Metadata.PrometheusStyleRule)
})
}
// This test makes sure the default generator