Skip to content

Commit

Permalink
Add a support for ignoring the events from a namespace that matches a…
Browse files Browse the repository at this point in the history
… regular expression (kubeshop#295)
  • Loading branch information
jkremser committed Oct 2, 2020
1 parent 76c8df3 commit db4f63e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions deploy-all-in-one-tls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ data:
namespaces: # List of namespaces, "all" will watch all the namespaces
include:
- all
ignore: # List of namespaces to be ignored (omitempty), used only with include: all
- # example : include [all], ignore [x,y,z]
ignore: # List of namespaces to be ignored (omitempty), used only with include: all, can contain a wildcard (*)
- # example : include [all], ignore [x,y,secret-ns-*]
events: # List of lifecycle events you want to receive, e.g create, update, delete, error OR all
- create
- delete
Expand Down
4 changes: 2 additions & 2 deletions deploy-all-in-one.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ data:
namespaces: # List of namespaces, "all" will watch all the namespaces
include:
- all
ignore: # List of namespaces to be ignored (omitempty), used only with include: all
- # example : include [all], ignore [x,y,z]
ignore: # List of namespaces to be ignored (omitempty), used only with include: all, can contain a wildcard (*)
- # example : include [all], ignore [x,y,secret-ns-*]
events: # List of lifecycle events you want to receive, e.g create, update, delete, error OR all
- create
- delete
Expand Down
4 changes: 2 additions & 2 deletions helm/botkube/sample-res-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ config:
namespaces: # List of namespaces, "all" will watch all the namespaces
include:
- all
ignore: # List of namespaces to be ignored (omitempty), used only with include: all
- kube-system # example : include [all], ignore [x,y,z]
ignore: # List of namespaces to be ignored (omitempty), used only with include: all, can contain a wildcard (*)
- kube-system # example : include [all], ignore [x,y,secret-ns-*]
events: # List of lifecycle events you want to receive, e.g create, update, delete, error OR all
- create
- delete
Expand Down
4 changes: 2 additions & 2 deletions helm/botkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ config:
namespaces: # List of namespaces, "all" will watch all the namespaces
include:
- all
ignore: # List of namespaces to be ignored (omitempty), used only with include: all
- # example : include [all], ignore [x,y,z]
ignore: # List of namespaces to be ignored (omitempty), used only with include: all, can contain a wildcard (*)
- # example : include [all], ignore [x,y,secret-ns-*]
events: # List of lifecycle events you want to receive, e.g create, update, delete, error OR all
- create
- delete
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ type UpdateSetting struct {
// - "all" to watch all the namespaces
// Ignore contains a list of namespaces to be ignored when all namespaces are included
// It is an optional (omitempty) field which is tandem with Include [all]
// example : include [all], ignore [x,y,z]
// It can also contain a * that would expand to zero or more arbitrary characters
// example : include [all], ignore [x,y,secret-ns-*]
type Namespaces struct {
Include []string
Ignore []string `yaml:",omitempty"`
Expand Down
18 changes: 15 additions & 3 deletions pkg/filterengine/filters/namespace_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package filters

import (
"fmt"
"regexp"
"strings"

"github.com/infracloudio/botkube/pkg/config"
Expand Down Expand Up @@ -75,9 +76,20 @@ func (f NamespaceChecker) Describe() string {
func isNamespaceIgnored(resourceNamespaces config.Namespaces, eventNamespace string) bool {
if len(resourceNamespaces.Include) == 1 && resourceNamespaces.Include[0] == "all" {
if len(resourceNamespaces.Ignore) > 0 {
ignoredNamespaces := fmt.Sprintf("%#v", resourceNamespaces.Ignore)
if strings.Contains(ignoredNamespaces, eventNamespace) {
return true
for _, ignoredNamespace := range resourceNamespaces.Ignore {
// exact match
if ignoredNamespace == eventNamespace {
return true
}

// regexp
if strings.Contains(ignoredNamespace, "*") {
ns := strings.Replace(ignoredNamespace, "*", ".*", -1)
matched, err := regexp.MatchString(ns, eventNamespace)
if matched && err == nil {
return true
}
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/filterengine/filters/namespace_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ func TestIsNamespaceIgnored(t *testing.T) {
eventNamespace string
expected bool
}{
`include all and ignore few --> watch all except ignored`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{"demo", "abc"}}, "demo", true},
`include all and ignore is "" --> watch all`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{""}}, "demo", false},
`include all and ignore is [] --> watch all`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{}}, "demo", false},
`include all and ignore few --> watch all except ignored`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{"demo", "abc"}}, "demo", true},
`include all and ignore is "" --> watch all`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{""}}, "demo", false},
`include all and ignore is [] --> watch all`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{}}, "demo", false},
`include all and ignore with reqexp --> watch all except matched`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{"my-*"}}, "my-ns", true},
`include all and ignore few combined with regexp --> watch all except ignored`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{"demo", "ignored-*-ns"}}, "ignored-42-ns", true},
`include all and ignore with regexp that doesn't match anything --> watch all`: {config.Namespaces{Include: []string{"all"}, Ignore: []string{"demo-*"}}, "demo", false},
// utils.AllowedEventKindsMap inherently handles remaining test case
}
for name, test := range tests {
Expand Down
4 changes: 2 additions & 2 deletions resource_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ resources:
namespaces: # List of namespaces, "all" will watch all the namespaces
include:
- all
ignore: # List of namespaces to be ignored (omitempty), used only with include: all
- # example : include [all], ignore [x,y,z]
ignore: # List of namespaces to be ignored (omitempty), used only with include: all, can contain a wildcard (*)
- # example : include [all], ignore [x,y,secret-ns-*]
events: # List of lifecycle events you want to receive, e.g create, update, delete, error OR all
- create
- delete
Expand Down
4 changes: 2 additions & 2 deletions test/resource_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ resources:
namespaces: # List of namespaces, "all" will watch all the namespaces
include:
- all
ignore: # List of namespaces to be ignored (omitempty), used only with include: all
- # example : include [all], ignore [x,y,z]
ignore: # List of namespaces to be ignored (omitempty), used only with include: all, can contain a wildcard (*)
- # example : include [all], ignore [x,y,secret-ns-*]
events: # List of lifecycle events you want to receive, e.g create, update, delete, error OR all
- create
- delete
Expand Down

0 comments on commit db4f63e

Please sign in to comment.