Merge pull request #3674 from junotx/ca
fix case-insensitive keyword query and consistent state for alerting
This commit is contained in:
@@ -192,7 +192,7 @@ func (q *AlertingRuleQueryParams) Filter(rules []*GettableAlertingRule) []*Getta
|
||||
}
|
||||
|
||||
func (q *AlertingRuleQueryParams) matches(rule *GettableAlertingRule) bool {
|
||||
if q.NameContainFilter != "" && !strings.Contains(rule.Name, q.NameContainFilter) {
|
||||
if q.NameContainFilter != "" && !containsCaseInsensitive(rule.Name, q.NameContainFilter) {
|
||||
return false
|
||||
}
|
||||
if q.State != "" && q.State != rule.State {
|
||||
@@ -210,7 +210,7 @@ func (q *AlertingRuleQueryParams) matches(rule *GettableAlertingRule) bool {
|
||||
}
|
||||
}
|
||||
for k, v := range q.LabelContainFilters {
|
||||
if fv, ok := rule.Labels[k]; !ok || !strings.Contains(fv, v) {
|
||||
if fv, ok := rule.Labels[k]; !ok || !containsCaseInsensitive(fv, v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -340,7 +340,7 @@ func (q *AlertQueryParams) matches(alert *Alert) bool {
|
||||
}
|
||||
}
|
||||
for k, v := range q.LabelContainFilters {
|
||||
if fv, ok := alert.Labels[k]; !ok || !strings.Contains(fv, v) {
|
||||
if fv, ok := alert.Labels[k]; !ok || !containsCaseInsensitive(fv, v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -526,3 +526,10 @@ func NewBulkItemErrorServerResponse(ruleName string, err error) *BulkItemRespons
|
||||
Error: err,
|
||||
}
|
||||
}
|
||||
|
||||
// containsCaseInsensitive reports whether substr is case-insensitive within s.
|
||||
func containsCaseInsensitive(s, substr string) bool {
|
||||
return strings.Contains(
|
||||
strings.ToLower(s),
|
||||
strings.ToLower(substr))
|
||||
}
|
||||
|
||||
@@ -216,7 +216,8 @@ func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroup
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var epRules = make(map[string]*alerting.AlertingRule)
|
||||
var epRule *alerting.AlertingRule
|
||||
out:
|
||||
for _, group := range epRuleGroups {
|
||||
fileShort := strings.TrimSuffix(filepath.Base(group.File), filepath.Ext(group.File))
|
||||
if !strings.HasPrefix(fileShort, ruleNamespace+"-") {
|
||||
@@ -226,34 +227,24 @@ func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroup
|
||||
continue
|
||||
}
|
||||
|
||||
for _, epRule := range group.Rules {
|
||||
if eid, err := GenEndpointRuleId(group.Name, epRule, extLabels); err != nil {
|
||||
if group.Name != rule.Group {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, epr := range group.Rules {
|
||||
if epr.Name != rule.Alert { // first check name to speed up the hit
|
||||
continue
|
||||
}
|
||||
if eid, err := GenEndpointRuleId(group.Name, epr, extLabels); err != nil {
|
||||
return nil, errors.Wrap(err, ErrGenRuleId)
|
||||
} else {
|
||||
if rule.Rule.Alert == epRule.Name {
|
||||
epRules[eid] = epRule
|
||||
if rule.Id == eid {
|
||||
epRule = epr
|
||||
break out
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var epRule *alerting.AlertingRule
|
||||
if rule.Custom {
|
||||
// guarantees the stability of the get operations.
|
||||
var ids []string
|
||||
for k, _ := range epRules {
|
||||
ids = append(ids, k)
|
||||
}
|
||||
if l := len(ids); l > 0 {
|
||||
if l > 1 {
|
||||
sort.Slice(ids, func(i, j int) bool {
|
||||
return v2alpha1.AlertingRuleIdCompare(ids[i], ids[j])
|
||||
})
|
||||
}
|
||||
epRule = epRules[ids[0]]
|
||||
}
|
||||
} else {
|
||||
epRule = epRules[rule.Id]
|
||||
}
|
||||
|
||||
return getAlertingRuleStatus(&rule.ResourceRuleItem, epRule, rule.Custom, rule.Level), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user