Skip to content

Commit

Permalink
Ignore pods by pattern (#270)
Browse files Browse the repository at this point in the history
Add a config option to ignore events by pods matching a regex pattern
  • Loading branch information
fschlager authored Jan 31, 2024
1 parent e3dea34 commit d103fdd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ kubectl apply -f https://raw.githubusercontent.com/abahmed/kwatch/v0.8.4/deploy/
| `reasons` | Optional comma separated list of reasons that you want to watch or forbid, if it's not provided it will watch all reasons. If you want to forbid a reason, configure it with `!<reason>`. You can either set forbidden reasons or allowed, not both. |
| `ignoreFailedGracefulShutdown` | If set to true, containers which are forcefully killed during shutdown (as their graceful shutdown failed) are not reported as error |
| `ignoreContainerNames` | Optional comma separated list of container names to ignore |
| `ignorePodNames` | Optional list of pod name regexp patterns to ignore |

### App

Expand Down
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package config

import (
"regexp"
)

type Config struct {
// App general configuration
App App `yaml:"app"`
Expand Down Expand Up @@ -34,6 +38,9 @@ type Config struct {
// IgnoreContainerNames optional list of container names to ignore
IgnoreContainerNames []string `yaml:"ignoreContainerNames"`

// IgnorePodNames optional list of pod name regexp patterns to ignore
IgnorePodNames []string `yaml:"ignorePodNames"`

// Alert is a map contains a map of each provider configuration
// e.g. {"slack": {"webhook": "URL"}}
Alert map[string]map[string]interface{} `yaml:"alert"`
Expand All @@ -47,6 +54,9 @@ type Config struct {
// Reasons configuration
AllowedReasons []string
ForbiddenReasons []string

// Patterns are compiled from IgnorePodNames after loading
IgnorePodNamePatterns []*regexp.Regexp
}

// App confing struct
Expand Down
21 changes: 21 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,24 @@ func TestConfigFromFile(t *testing.T) {
_, err := LoadConfig()
assert.NotNil(err)
}

func TestGetCompiledIgnorePodNamePatterns(t *testing.T) {
assert := assert.New(t)

validPatterns := []string{
"my-fancy-pod-[0-9]",
}

compiledPatterns, err := getCompiledIgnorePodNamePatterns(validPatterns)

assert.Nil(err)
assert.True(compiledPatterns[0].MatchString("my-fancy-pod-8"))

invalidPatterns := []string{
"my-fancy-pod-[.*",
}

compiledPatterns, err = getCompiledIgnorePodNamePatterns(invalidPatterns)

assert.NotNil(err)
}
25 changes: 25 additions & 0 deletions config/loadConfig.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"fmt"
"os"
"regexp"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -46,6 +48,13 @@ func LoadConfig() (*Config, error) {
"Can't set both")
}

// Prepare ignored pod name patters
config.IgnorePodNamePatterns, err =
getCompiledIgnorePodNamePatterns(config.IgnorePodNames)
if err != nil {
logrus.Errorf("Failed to compile pod name pattern: %s", err.Error())
}

// Parse proxy config
if len(config.App.ProxyURL) > 0 {
os.Setenv("HTTPS_PROXY", config.App.ProxyURL)
Expand All @@ -67,3 +76,19 @@ func getAllowForbidSlices(items []string) (allow []string, forbid []string) {
}
return allow, forbid
}

func getCompiledIgnorePodNamePatterns(patterns []string) (compiledPatterns []*regexp.Regexp, err error) {
compiledPatterns = make([]*regexp.Regexp, 0)

for _, pattern := range patterns {
compiledPattern, err := regexp.Compile(pattern)

if err != nil {
return nil, fmt.Errorf("failed to compile pattern '%s'", pattern)
}

compiledPatterns = append(compiledPatterns, compiledPattern)
}

return compiledPatterns, nil
}
13 changes: 12 additions & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,22 @@ func (c *Controller) processPod(key string, pod *v1.Pod) {
if len(c.config.IgnoreContainerNames) > 0 &&
slices.Contains(c.config.IgnoreContainerNames, container.Name) {
logrus.Infof(
"skip pod %s as in container ignore list",
"skip container %s as in container ignore list",
container.Name)
return
}

if len(c.config.IgnorePodNames) > 0 {
for _, pattern := range c.config.IgnorePodNamePatterns {
if pattern.MatchString(pod.Name) {
logrus.Infof(
"skip pod %s as in pod name patterns ignore list",
container.Name)
return
}
}
}

// get logs for this container
previous := true
if reason == "Error" {
Expand Down

0 comments on commit d103fdd

Please sign in to comment.