From 45ca199fb9582a8c23e1c9fe40bc92622c54ed73 Mon Sep 17 00:00:00 2001 From: wanjunlei Date: Wed, 3 Apr 2024 14:18:30 +0800 Subject: [PATCH] fix bug regex silence will silence all alert Signed-off-by: wanjunlei --- apis/v2beta2/common.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/apis/v2beta2/common.go b/apis/v2beta2/common.go index 1ca61dd1..dbfaa89b 100644 --- a/apis/v2beta2/common.go +++ b/apis/v2beta2/common.go @@ -42,10 +42,13 @@ const ( ) func (ls *LabelSelector) Matches(label map[string]string) (bool, error) { + if label == nil { + return false, nil + } + selector := &metav1.LabelSelector{ MatchLabels: ls.MatchLabels, } - for _, requirement := range ls.MatchExpressions { if requirement.Operator != LabelSelectorOpMatch { selector.MatchExpressions = append(selector.MatchExpressions, metav1.LabelSelectorRequirement{ @@ -54,11 +57,9 @@ func (ls *LabelSelector) Matches(label map[string]string) (bool, error) { Values: requirement.Values, }) } else { - if v, ok := label[requirement.Key]; ok { - match, err := regexp.MatchString(requirement.RegexValue, v) - if err != nil || !match { - return false, err - } + match, err := regexp.MatchString(requirement.RegexValue, label[requirement.Key]) + if !match { + return false, err } } }