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

feat: filter groups #71

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pkg/filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ should be applied to all resources.

It has a special key called `__global__`.

This only works when you are defining it as a resource type as part of the `Filters` `map[string][]Filter` type.
This only works when you are defining it as a resource type as part of the `Filters` `map[string][]Filter` type.
9 changes: 9 additions & 0 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
// Values allows for multiple values to be specified for a filter
Values []string

Group string

// Invert is a flag to invert the filter
Invert string
}
Expand Down Expand Up @@ -163,6 +165,7 @@
if unmarshal(&value) == nil {
f.Type = Exact
f.Value = value
f.Group = "default"
return nil
}

Expand All @@ -173,6 +176,12 @@
return err
}

if m["group"] == nil {
f.Group = "default"
} else {
f.Group = m["group"].(string)

Check warning on line 182 in pkg/filter/filter.go

View check run for this annotation

Codecov / codecov/patch

pkg/filter/filter.go#L182

Added line #L182 was not covered by tests
}

if m["type"] == nil {
f.Type = Exact
} else {
Expand Down
8 changes: 4 additions & 4 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func TestFilter_GlobalYAML(t *testing.T) {

expected := filter.Filters{
"Resource1": []filter.Filter{
{Property: "prop3", Type: filter.Exact, Value: "value3", Values: []string{}},
{Property: "prop1", Type: filter.Exact, Value: "value1", Values: []string{}},
{Property: "prop3", Type: filter.Exact, Value: "value3", Values: []string{}, Group: "default"},
{Property: "prop1", Type: filter.Exact, Value: "value1", Values: []string{}, Group: "default"},
},
"Resource2": []filter.Filter{
{Property: "prop3", Type: filter.Exact, Value: "value3", Values: []string{}},
{Property: "prop2", Type: filter.Exact, Value: "value2", Values: []string{}},
{Property: "prop3", Type: filter.Exact, Value: "value3", Values: []string{}, Group: "default"},
{Property: "prop2", Type: filter.Exact, Value: "value2", Values: []string{}, Group: "default"},
},
}

Expand Down
37 changes: 33 additions & 4 deletions pkg/nuke/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,19 @@ func (n *Nuke) Filter(item *queue.Item) error {
return nil
}

isFiltered := false
filterGroups := make(map[string]int)

for _, f := range itemFilters {
filterGroups[f.Group] = 0
}

for _, f := range itemFilters {
log.
WithField("prop", f.Property).
WithField("type", f.Type).
WithField("value", f.Value).
WithField("group", f.Group).
Trace("filter details")

prop, err := item.GetProperty(f.Property)
Expand All @@ -480,16 +488,37 @@ func (n *Nuke) Filter(item *queue.Item) error {
if utils.IsTrue(f.Invert) {
log.WithField("orig", match).WithField("new", !match).Trace("filter inverted")
match = !match

log.Tracef("match inverted: %t", match)
}

if match {
log.Trace("filter matched")
item.State = queue.ItemStateFiltered
item.Reason = "filtered by config"
return nil
filterGroups[f.Group]++
}
}

if len(filterGroups) > 0 {
var groupsMatched int
for _, count := range filterGroups {
if count > 0 {
groupsMatched++
}
}

log.Tracef("groups matched: %d -> %d", groupsMatched, len(filterGroups))

if groupsMatched == len(filterGroups) {
isFiltered = true
}
}

if isFiltered {
log.Trace("filter matched by group")
item.State = queue.ItemStateFiltered
item.Reason = "filtered by config"
return nil
}

return nil
}

Expand Down
201 changes: 201 additions & 0 deletions pkg/nuke/nuke_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,204 @@ func Test_Nuke_Filters_Filtered(t *testing.T) {
})
}
}

func Test_Nuke_Filters_FilterGroups(t *testing.T) {
cases := []struct {
name string
error bool
resources []resource.Resource
filters filter.Filters
expected string
}{
{
name: "exact-group-matched",
expected: "filtered by config",
resources: []resource.Resource{
&TestResourceFilter{
Props: types.Properties{
"Name": "test",
"Other": "test2",
},
},
},
filters: filter.Filters{
TestResourceType2: []filter.Filter{
{
Type: filter.Exact,
Property: "Name",
Value: "test",
Group: "one",
},
{
Type: filter.Exact,
Property: "Other",
Value: "test2",
Group: "two",
},
},
},
},
{
name: "exact-group-non-matched",
expected: "",
resources: []resource.Resource{
&TestResourceFilter{
Props: types.Properties{
"Name": "test",
"Other": "test2",
},
},
},
filters: filter.Filters{
TestResourceType2: []filter.Filter{
{
Type: filter.Exact,
Property: "Name",
Value: "test1",
Group: "one",
},
{
Type: filter.Exact,
Property: "Other",
Value: "test2",
Group: "two",
},
},
},
},
{
name: "group-invert-date-no-match",
expected: "",
resources: []resource.Resource{
&TestResourceFilter{
Props: types.Properties{
"Name": "just-something-else",
"CreatedAt": time.Now().UTC().Format(time.RFC3339),
},
},
},
filters: filter.Filters{
TestResourceType2: []filter.Filter{
{
Type: filter.Exact,
Property: "Name",
Value: "test",
Invert: "true",
Group: "one",
},
{
Type: filter.Exact,
Property: "Name",
Value: "test-testing",
Invert: "true",
Group: "two",
},
{
Type: filter.DateOlderThan,
Property: "CreatedAt",
Value: "-72h",
Group: "three",
},
},
},
},
{
name: "group-invert-date-no-match",
expected: "filtered by config",
resources: []resource.Resource{
&TestResourceFilter{
Props: types.Properties{
"Name": "test-exclude",
"CreatedAt": "2024-08-20T18:24:21Z",
},
},
},
filters: filter.Filters{
TestResourceType2: []filter.Filter{
{
Type: filter.Exact,
Property: "Name",
Value: "test",
Invert: "true",
Group: "one",
},
{
Type: filter.Exact,
Property: "Name",
Value: "test-testing",
Invert: "true",
Group: "two",
},
{
Type: filter.DateOlderThan,
Property: "CreatedAt",
Value: "-72h",
Invert: "true",
Group: "three",
},
},
},
},
{
name: "group-invert-date-no-match",
expected: "",
resources: []resource.Resource{
&TestResourceFilter{
Props: types.Properties{
"Name": "test-testing",
"CreatedAt": "2024-08-20T18:24:21Z",
},
},
},
filters: filter.Filters{
TestResourceType2: []filter.Filter{
{
Type: filter.Exact,
Property: "Name",
Value: "test",
Invert: "true",
Group: "one",
},
{
Type: filter.Exact,
Property: "Name",
Value: "test-testing",
Invert: "true",
Group: "two",
},
{
Type: filter.DateOlderThan,
Property: "CreatedAt",
Value: "-72h",
Invert: "true",
Group: "three",
},
},
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
n := New(testParameters, tc.filters, nil)
n.SetLogger(logrus.WithField("test", true))
n.SetRunSleep(time.Millisecond * 5)

for _, r := range tc.resources {
i := &queue.Item{
Resource: r,
Type: TestResourceType2,
}

err := n.Filter(i)
if tc.error == true {
assert.Error(t, err)
continue
}

assert.NoError(t, err)
assert.Equal(t, tc.expected, i.Reason)
}
})
}
}
Loading