exclude alerting rule replica label

Signed-off-by: junotx <junotx@126.com>
This commit is contained in:
junotx
2021-02-01 15:39:27 +08:00
parent a120969be7
commit 5e7f40e6b9

View File

@@ -1,7 +1,6 @@
package rules package rules
import ( import (
"kubesphere.io/kubesphere/pkg/simple/client/alerting"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
@@ -16,6 +15,7 @@ import (
"github.com/prometheus/prometheus/rules" "github.com/prometheus/prometheus/rules"
"k8s.io/klog" "k8s.io/klog"
"kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1" "kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1"
"kubesphere.io/kubesphere/pkg/simple/client/alerting"
) )
const ( const (
@@ -25,6 +25,9 @@ const (
LabelKeyInternalRuleName = "__rule_name__" LabelKeyInternalRuleName = "__rule_name__"
LabelKeyInternalRuleQuery = "__rule_query__" LabelKeyInternalRuleQuery = "__rule_query__"
LabelKeyInternalRuleDuration = "__rule_duration__" LabelKeyInternalRuleDuration = "__rule_duration__"
LabelKeyThanosRulerReplica = "thanos_ruler_replica"
LabelKeyPrometheusReplica = "prometheus_replica"
) )
func FormatExpr(expr string) (string, error) { func FormatExpr(expr string) (string, error) {
@@ -100,16 +103,21 @@ func GenEndpointRuleId(group string, epRule *alerting.AlertingRule,
} }
duration := parseDurationSeconds(epRule.Duration) duration := parseDurationSeconds(epRule.Duration)
var labelsMap map[string]string var extLabels map[string]string
if externalLabels == nil { if externalLabels != nil {
labelsMap = epRule.Labels extLabels = externalLabels()
} else { }
labelsMap = make(map[string]string) labelsMap := make(map[string]string)
extLabels := externalLabels() for key, value := range epRule.Labels {
for key, value := range epRule.Labels { if key == LabelKeyPrometheusReplica || key == LabelKeyThanosRulerReplica {
if v, ok := extLabels[key]; !(ok && value == v) { continue
labelsMap[key] = value }
} if extLabels == nil {
labelsMap[key] = value
continue
}
if v, ok := extLabels[key]; !(ok && value == v) {
labelsMap[key] = value
} }
} }
@@ -148,11 +156,11 @@ func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk,
continue continue
} }
for _, epRule := range group.Rules { for i, epRule := range group.Rules {
if eid, err := GenEndpointRuleId(group.Name, epRule, extLabels); err != nil { if eid, err := GenEndpointRuleId(group.Name, epRule, extLabels); err != nil {
return nil, errors.Wrap(err, ErrGenRuleId) return nil, errors.Wrap(err, ErrGenRuleId)
} else { } else {
idEpRules[eid] = epRule idEpRules[eid] = group.Rules[i]
nameIds[epRule.Name] = append(nameIds[epRule.Name], eid) nameIds[epRule.Name] = append(nameIds[epRule.Name], eid)
} }
} }