Skip to content

Commit

Permalink
Support complex regexes for event constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Feb 1, 2023
1 parent a329c8e commit d89c64b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/alexflint/go-arg v1.4.3
github.com/aws/aws-sdk-go v1.44.20
github.com/bwmarrin/discordgo v0.25.0
github.com/dlclark/regexp2 v1.8.0
github.com/dustin/go-humanize v1.0.0
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ github.com/dhui/dktest v0.3.7/go.mod h1:nYMOkafiA07WchSwKnKFUSbGMb2hMm5DrCGiXYG6
github.com/die-net/lrucache v0.0.0-20181227122439-19a39ef22a11/go.mod h1:ew0MSjCVDdtGMjF3kzLK9hwdgF5mOE8SbYVF3Rc7mkU=
github.com/disintegration/imaging v1.6.0/go.mod h1:xuIt+sRxDFrHS0drzXUlCJthkJ8k7lkkUojDSR247MQ=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/dlclark/regexp2 v1.8.0 h1:rJD5HeGIT/2b5CDk63FVCwZA3qgYElfg+oQK7uH5pfE=
github.com/dlclark/regexp2 v1.8.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY=
github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
Expand Down
22 changes: 17 additions & 5 deletions internal/source/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"
"reflect"
"regexp"
"strings"

"github.com/dlclark/regexp2"
"github.com/sirupsen/logrus"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -208,18 +208,30 @@ func matchRegexForStringsIfDefined(regexStr string, str []string) (bool, error)
return true, nil
}

regex, err := regexp.Compile(regexStr)
regex, err := regexp2.Compile(regexStr, regexp2.None)
if err != nil {
return false, fmt.Errorf("while compiling regex: %w", err)
}

if len(str) == 0 {
// no messages, so let's check if regex matches empty string
str = append(str, "")
}

errs := multierror.New()
for _, s := range str {
if regex.MatchString(s) {
return true, nil
match, err := regex.MatchString(s)
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("while matching regex %s: %w", regexStr, err))
continue
}

if match {
return true, errs.ErrorOrNil()
}
}

return false, nil
return false, errs.ErrorOrNil()
}

func kvsSatisfiedForMap(expectedKV, obj map[string]string) bool {
Expand Down
89 changes: 86 additions & 3 deletions internal/source/registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func TestSourcesForEvent(t *testing.T) {
resourceName: "^Created",
namespaces: allNsCfg,
},
{
source: "fail2",
event: config.KubernetesEvent{
Reason: "^(?!NodeNotReady)$",
},
namespaces: allNsCfg,
},
},
Event: event.Event{
Name: "test-one",
Expand Down Expand Up @@ -68,7 +75,7 @@ func TestSourcesForEvent(t *testing.T) {
ExpectedResult: []string{"success"},
ExpectedErrMessage: heredoc.Docf(`
1 error occurred:
* while compiling regex: error parsing regexp: missing closing ]: %s`, "`[`"),
* while compiling regex: error parsing regexp: unterminated [] set in %s`, "`[`"),
},
{
Name: "Event message - success",
Expand Down Expand Up @@ -105,6 +112,77 @@ func TestSourcesForEvent(t *testing.T) {
},
ExpectedResult: []string{"success", "success2"},
},
{
Name: "Event message - negative lookahead",
Routes: []route{
{
source: "success",
event: config.KubernetesEvent{
Message: "^(?!Back-off).*$",
},
namespaces: allNsCfg,
},
{
source: "success2",
event: config.KubernetesEvent{
Message: "^(?!Back-off restarting failed container)$",
},
namespaces: allNsCfg,
},
{
source: "empty",
event: config.KubernetesEvent{
Message: "",
},
namespaces: allNsCfg,
},
},
Event: event.Event{
Name: "test-one",
Messages: []string{
"Back-off restarting failed container",
},
},
ExpectedResult: []string{"empty"},
},
{
Name: "Event message - empty",
Routes: []route{
{
source: "success",
event: config.KubernetesEvent{
Message: "^(?!Back-off).*$",
},
namespaces: allNsCfg,
},
{
source: "success2",
event: config.KubernetesEvent{
Message: "^(?!Back-off restarting failed container)$",
},
namespaces: allNsCfg,
},
{
source: "success3",
event: config.KubernetesEvent{
Message: "",
},
namespaces: allNsCfg,
},
{
source: "fail",
event: config.KubernetesEvent{
Message: "^Back-off",
},
namespaces: allNsCfg,
},
},
Event: event.Event{
Name: "test-one",
Messages: nil,
},
ExpectedResult: []string{"success", "success2", "success3"},
},
{
Name: "Event message - error",
Routes: []route{
Expand Down Expand Up @@ -134,7 +212,7 @@ func TestSourcesForEvent(t *testing.T) {
ExpectedResult: []string{"success"},
ExpectedErrMessage: heredoc.Docf(`
1 error occurred:
* while compiling regex: error parsing regexp: missing closing ]: %s`, "`[`"),
* while compiling regex: error parsing regexp: unterminated [] set in %s`, "`[`"),
},
{
Name: "Resource name - success",
Expand All @@ -149,6 +227,11 @@ func TestSourcesForEvent(t *testing.T) {
resourceName: "^one-.*",
namespaces: allNsCfg,
},
{
source: "fail2",
resourceName: "^(?!^test-).*$",
namespaces: allNsCfg,
},
},
Event: event.Event{
Name: "test-one",
Expand All @@ -175,7 +258,7 @@ func TestSourcesForEvent(t *testing.T) {
ExpectedResult: []string{"success"},
ExpectedErrMessage: heredoc.Docf(`
1 error occurred:
* while compiling regex: error parsing regexp: missing closing ]: %s`, "`[`"),
* while compiling regex: error parsing regexp: unterminated [] set in %s`, "`[`"),
},
{
Name: "Namespace",
Expand Down
20 changes: 17 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
_ "embed"
"fmt"
"os"
"regexp"
"strings"
"time"

"github.com/dlclark/regexp2"
"github.com/knadh/koanf"
koanfyaml "github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
Expand Down Expand Up @@ -424,7 +424,7 @@ func (n *Namespaces) IsAllowed(givenNs string) bool {
}

// regexp
matched, err := regexp.MatchString(excludeNamespace, givenNs)
matched, err := n.matchString(excludeNamespace, givenNs)
if err == nil && matched {
return false
}
Expand All @@ -444,7 +444,7 @@ func (n *Namespaces) IsAllowed(givenNs string) bool {
}

// regexp
matched, err := regexp.MatchString(includeNamespace, givenNs)
matched, err := n.matchString(includeNamespace, givenNs)
if err == nil && matched {
return true
}
Expand All @@ -455,6 +455,20 @@ func (n *Namespaces) IsAllowed(givenNs string) bool {
return false
}

func (n *Namespaces) matchString(regexStr, s string) (bool, error) {
regex, err := regexp2.Compile(regexStr, regexp2.None)
if err != nil {
return false, fmt.Errorf("while compiling regex: %w", err)
}

match, err := regex.MatchString(s)
if err != nil {
return false, fmt.Errorf("while matching regex: %w", err)
}

return match, nil
}

// Notification holds notification configuration.
type Notification struct {
Type NotificationType
Expand Down

0 comments on commit d89c64b

Please sign in to comment.