Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a support for ignoring the events from a namespace that matches regexp #386

Merged
merged 3 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 err == nil && matched {
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