From bd820c21a68bca96cc1f69358fb8985511c6426a Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 26 Jun 2024 18:45:37 +0545 Subject: [PATCH] fix: whitespace in kubernetes exclusions --- scrapers/kubernetes/kubernetes.go | 15 +++++++- scrapers/kubernetes/kubernetes_test.go | 51 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/scrapers/kubernetes/kubernetes.go b/scrapers/kubernetes/kubernetes.go index 415a4eac..143da54b 100644 --- a/scrapers/kubernetes/kubernetes.go +++ b/scrapers/kubernetes/kubernetes.go @@ -322,7 +322,7 @@ func ExtractResults(ctx api.ScrapeContext, config v1.Kubernetes, objs []*unstruc } if changeTypExclusion != "" { - if collections.MatchItems(change.ChangeType, strings.Split(changeTypExclusion, ",")...) { + if collections.MatchItems(change.ChangeType, splitTrimmed(changeTypExclusion, ",")...) { ctx.Logger.V(4).Infof("excluding event object %s/%s/%s due to change type matched in annotation %s=%s", event.InvolvedObject.Namespace, event.InvolvedObject.Name, event.InvolvedObject.Kind, AnnotationIgnoreChangeByType, changeTypExclusion) @@ -331,7 +331,7 @@ func ExtractResults(ctx api.ScrapeContext, config v1.Kubernetes, objs []*unstruc } if changeSeverityExclusion != "" { - if collections.MatchItems(change.Severity, strings.Split(changeSeverityExclusion, ",")...) { + if collections.MatchItems(change.Severity, splitTrimmed(changeSeverityExclusion, ",")...) { ctx.Logger.V(4).Infof("excluding event object %s/%s/%s due to severity matches in annotation %s=%s", event.InvolvedObject.Namespace, event.InvolvedObject.Name, event.InvolvedObject.Kind, AnnotationIgnoreChangeBySeverity, changeSeverityExclusion) @@ -811,3 +811,14 @@ func parseAzureURI(uri string) (string, string) { return subscriptionID, vmScaleSetID } + +func splitTrimmed(input, cut string) []string { + if strings.TrimSpace(input) == "" { + return []string{} + } + + splits := strings.Split(input, cut) + return lo.Map(splits, func(split string, _ int) string { + return strings.TrimSpace(split) + }) +} diff --git a/scrapers/kubernetes/kubernetes_test.go b/scrapers/kubernetes/kubernetes_test.go index 325c9364..429b9df5 100644 --- a/scrapers/kubernetes/kubernetes_test.go +++ b/scrapers/kubernetes/kubernetes_test.go @@ -2,6 +2,8 @@ package kubernetes import ( "testing" + + "github.com/google/go-cmp/cmp" ) func Test_extractAccountIDFromARN(t *testing.T) { @@ -67,3 +69,52 @@ func Test_extractAzureSubscriptionIDFromProvider(t *testing.T) { }) } } + +func TestSplitTrimmed(t *testing.T) { + tests := []struct { + name string + input string + cut string + expected []string + }{ + { + name: "empty input", + input: "", + cut: ",", + expected: []string{}, + }, + { + name: "single element", + input: "hello", + cut: ",", + expected: []string{"hello"}, + }, + { + name: "multiple elements", + input: "hello,world,foo", + cut: ",", + expected: []string{"hello", "world", "foo"}, + }, + { + name: "leading and trailing spaces", + input: " hello , world , foo ", + cut: ",", + expected: []string{"hello", "world", "foo"}, + }, + { + name: "different delimiter", + input: "hello|world|foo", + cut: "|", + expected: []string{"hello", "world", "foo"}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result := splitTrimmed(test.input, test.cut) + if diff := cmp.Diff(result, test.expected); diff != "" { + t.Errorf("diff = %v", diff) + } + }) + } +}