From bfff0b281aa06c587e01e5a4bcd1529335c2e1bc Mon Sep 17 00:00:00 2001 From: Abdullah Almariah Date: Thu, 26 Nov 2020 15:30:13 +0200 Subject: [PATCH] Replace resource kind exclusion with resource ID and supporting regex --- README.md | 11 ++++++++++- main.go | 12 +++++++++--- untrak.yaml | 8 ++++---- utils/strings.go | 11 +++++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f90952e..5f6c50d 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ out: args: ["get", "cm,deploy,svc,ing", "-o", "yaml", "-n", "api"] exclude: -- namespace +- /namespace/.* ``` To show untracked resources in your cluster (out) simply launch `untrak` like so: @@ -55,6 +55,15 @@ $ untrak -c untrak.yaml -o text - api/Ingress/my-ingress ``` +The untracked resources have the following format: +* *Namespaced resources*: `namespace/kind/name` +* *Non-namespaced resources*: `/kind/name` + +You can exclude any resource (with the previous format) form tracking by setting `exclude` section in the config file with list of excluded resources. The excluded resource could be a full resource name or regular expressions. For examples: +* to exclude any namespace resource: `/namespace/.*` +* to exclude secret `sec` in namespace `deafult`: `deafult/secret/sec` +* to exclude any config map: `.*/configmap/.*` + If your manifests have the namespace set to non-namespaced resource, untrak will skip the namespace. A list of supported non-namespaced resource types that will be skipped are defined by default. If you have installed more non-namespaced resource types (eg., `CRDs`), you could add extra resource types to skip namespace in comparison: ```yaml # untrak.yaml diff --git a/main.go b/main.go index 9fba4aa..b789504 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/exec" + "regexp" "sync" "github.com/yanc0/untrak/outputs" @@ -135,11 +136,16 @@ func getKubernetesResources(cfgs []*config.CommandConfig) ([]*kubernetes.Resourc return resources, nil } -func listUntrackedResources(in []*kubernetes.Resource, out []*kubernetes.Resource, kindExclude []string, nonNamespaced []string) []*kubernetes.Resource { +func listUntrackedResources(in []*kubernetes.Resource, out []*kubernetes.Resource, excludeRegexp []string, nonNamespaced []string) []*kubernetes.Resource { + var compiledExcludeRegexp []*regexp.Regexp + for _, reg := range excludeRegexp { + compiledExcludeRegexp = append(compiledExcludeRegexp, regexp.MustCompile("(?i)"+reg)) + } + var untrackedResources []*kubernetes.Resource for _, resourceOut := range out { - // Resource is in the exlude list, skip it - if utils.StringInListCaseInsensitive(kindExclude, resourceOut.Kind) { + // Resource is in the exclude list, skip it + if utils.StringListRegexpMatch(compiledExcludeRegexp, resourceOut.ID()) { continue } found := false diff --git a/untrak.yaml b/untrak.yaml index b92ce77..91e02b4 100644 --- a/untrak.yaml +++ b/untrak.yaml @@ -24,11 +24,11 @@ out: # - cmd: "cat" # args: ["example/$SOME_FILE_NAME"] -# You can exclude some resource type from the comparison +# You can exclude some resource from the comparison using resource name or regular expression exclude: -- namespace -- secret -- configmap +- /namespace/.* +- deafult/secret/sec +- .*/configmap/.* # Declare non-namespaced resource types to be considered in resource comparison. # There are some defined resource types by default by default like namespace, diff --git a/utils/strings.go b/utils/strings.go index ad4837b..e8e2d37 100644 --- a/utils/strings.go +++ b/utils/strings.go @@ -2,6 +2,7 @@ package utils import ( "strings" + "regexp" ) // StringInListCaseInsensitive return true if str is in the list (case insensitive) @@ -13,3 +14,13 @@ func StringInListCaseInsensitive(list []string, str string) bool { } return false } + +// StringListRegexpMatch return true if str is matching any of the regexp in the list +func StringListRegexpMatch(list []*regexp.Regexp, str string) bool { + for _, r := range list { + if r.MatchString(strings.ToLower(str)) { + return true + } + } + return false +}