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

Replace resource kind exclusion with resource ID and supporting regex #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/exec"
"regexp"
"sync"

"github.com/yanc0/untrak/outputs"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions untrak.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"strings"
"regexp"
)

// StringInListCaseInsensitive return true if str is in the list (case insensitive)
Expand All @@ -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
}