alerting rules default to sort by update time
Signed-off-by: junotx <junotx@126.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
promresourcesv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
|
||||
@@ -470,6 +471,8 @@ func (o *operator) CreateCustomAlertingRule(ctx context.Context, namespace strin
|
||||
return v2alpha1.ErrAlertingRuleAlreadyExists
|
||||
}
|
||||
|
||||
setRuleUpdateTime(rule, time.Now())
|
||||
|
||||
return ruler.AddAlertingRule(ctx, ruleNamespace, extraRuleResourceSelector,
|
||||
customRuleGroupDefault, parseToPrometheusRule(rule), ruleResourceLabels)
|
||||
}
|
||||
@@ -521,6 +524,8 @@ func (o *operator) UpdateCustomAlertingRule(ctx context.Context, namespace, name
|
||||
return v2alpha1.ErrAlertingRuleNotFound
|
||||
}
|
||||
|
||||
setRuleUpdateTime(rule, time.Now())
|
||||
|
||||
return ruler.UpdateAlertingRule(ctx, ruleNamespace, extraRuleResourceSelector,
|
||||
resourceRule.Group, parseToPrometheusRule(rule), ruleResourceLabels)
|
||||
}
|
||||
@@ -633,3 +638,13 @@ func pageAlerts(alertingRules []*v2alpha1.GettableAlertingRule,
|
||||
Items: queryParams.Sub(alerts),
|
||||
}
|
||||
}
|
||||
|
||||
func setRuleUpdateTime(rule *v2alpha1.PostableAlertingRule, t time.Time) {
|
||||
if rule.Annotations == nil {
|
||||
rule.Annotations = make(map[string]string)
|
||||
}
|
||||
if t.IsZero() {
|
||||
t = time.Now()
|
||||
}
|
||||
rule.Annotations[v2alpha1.AnnotationKeyRuleUpdateTime] = t.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user