From db30464aef69addb479b8ccd01d6cfaf5a440ad6 Mon Sep 17 00:00:00 2001 From: rasmus Date: Wed, 9 Nov 2022 21:09:15 +0200 Subject: [PATCH] implement filters --- pkg/watcher/watcher.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index d628290..9544d33 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -5,6 +5,7 @@ import ( "log" "os" "path/filepath" + "strings" "sync" "git.k-space.ee/k-space/logmower-shipper/pkg/file" @@ -31,8 +32,8 @@ var App = &cli.App{ //TODO: &cli.BoolFlag{Name: "normalize-log-level", Usage: "Normalize log.level values to Syslog defined keywords"}, //TODO: &cli.BoolFlag{Name: "parse-json"}, // - // &cli.StringFlag{Category: "k8s metadata", Name: "pod-namespace", EnvVars: []string{"KUBE_POD_NAMESPACE"}}, - // &cli.StringFlag{Category: "k8s metadata", Name: "node-name", EnvVars: []string{"KUBE_NODE_NAME"}, Required: true}, + &cli.StringSliceFlag{Category: "selectors", Name: "pod-namespace", EnvVars: []string{"KUBE_POD_NAMESPACE"}}, + &cli.StringSliceFlag{Category: "selectors", Name: "pod-prefix", EnvVars: []string{"KUBE_NODE_NAME"}}, // &cli.StringFlag{Category: "secrets", Name: "mongo-uri", EnvVars: []string{"MONGO_URI"}, Usage: "mongodb://foo:bar@host:27017/database", Required: true}, }, @@ -48,6 +49,7 @@ var App = &cli.App{ }, Action: func(ctx *cli.Context) error { + filterNamespace, filterPodPrefixes := sliceToMap(ctx.StringSlice("pod-namespace")), ctx.StringSlice("pod-prefix") var wg sync.WaitGroup log.Printf("%s %s starting", ctx.App.Name, ctx.App.Version) @@ -94,6 +96,14 @@ var App = &cli.App{ continue } + if _, ok := filterNamespace[kubeInfo.Namespace]; !ok { + continue + } + + if ok := hasSlicePrefix(kubeInfo.Pod, filterPodPrefixes); !ok { + continue + } + promWatcherFilesStarted.Add(1) wg.Add(1) @@ -156,3 +166,22 @@ func simulateInitialCreates(dirName string, eventChan chan<- fsnotify.Event) err return nil } + +func sliceToMap[T comparable](sl []T) map[T]interface{} { + m := make(map[T]interface{}) + + for _, k := range sl { + m[k] = nil + } + + return m +} + +func hasSlicePrefix(s string, sl []string) bool { + for _, prefix := range sl { + if strings.HasPrefix(s, prefix) { + return true + } + } + return false +}