Skip to content

Commit

Permalink
preserve input order of filtered resources (#24)
Browse files Browse the repository at this point in the history
fixes #23
  • Loading branch information
ryane authored Feb 6, 2022
1 parent 2397e7c commit 1fedb7f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
11 changes: 11 additions & 0 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package filter

import (
"sort"

"github.com/ryane/kfilt/pkg/resource"
)

Expand All @@ -23,6 +25,11 @@ func (f *Filter) Filter(resources []resource.Resource) ([]resource.Resource, err
var err error
filtered := append([]resource.Resource{}, resources...)

ordermap := map[string]int{}
for i, res := range resources {
ordermap[res.ID()] = i
}

// excludes
for _, matcher := range f.Exclude {
filtered, err = exclude(filtered, matcher)
Expand Down Expand Up @@ -51,6 +58,10 @@ func (f *Filter) Filter(resources []resource.Resource) ([]resource.Resource, err
filtered = included
}

sort.Slice(filtered, func(i, j int) bool {
return ordermap[filtered[i].ID()] < ordermap[filtered[j].ID()]
})

return filtered, nil
}

Expand Down
64 changes: 42 additions & 22 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ func TestFilter(t *testing.T) {
},
noError,
},
{
"preserve order of returned resources",
excludeMatchers{},
includeMatchers{
{
Kind: "Pod",
},
{
Kind: "ServiceAccount",
},
},
expectIDs{
"/v1:serviceaccount::test-sa",
"/v1:serviceaccount::test-sa-2",
"/v1:pod:test-ns:test-pod",
},
noError,
},
{
"label key/value selector",
excludeMatchers{},
Expand Down Expand Up @@ -277,32 +295,34 @@ func TestFilter(t *testing.T) {
}

for _, test := range tests {
f := filter.New()
for _, m := range test.include {
f.AddInclude(m)
}
for _, m := range test.exclude {
f.AddExclude(m)
}

results, err := f.Filter(input)
if !test.expectedError(err) {
t.Errorf("unexpected error for %s: %v", test.name, err)
t.FailNow()
}
t.Run(test.name, func(t *testing.T) {
f := filter.New()
for _, m := range test.include {
f.AddInclude(m)
}
for _, m := range test.exclude {
f.AddExclude(m)
}

if len(results) != len(test.expectIDs) {
t.Errorf("%s: expected %d results, got %d\nincludes: %+v, excludes: %+v\nresults: %v", test.name, len(test.expectIDs), len(results), f.Include, f.Exclude, resourceIDs(results))
t.FailNow()
}
results, err := f.Filter(input)
if !test.expectedError(err) {
t.Errorf("unexpected error for %s: %v", test.name, err)
t.FailNow()
}

for i, res := range results {
id := res.ID()
if id != test.expectIDs[i] {
t.Errorf("%s: expected %s, got %s\nincludes: %v, excludes: %v", test.name, test.expectIDs[i], id, f.Include, f.Exclude)
if len(results) != len(test.expectIDs) {
t.Errorf("%s: expected %d results, got %d\nincludes: %+v, excludes: %+v\nresults: %v", test.name, len(test.expectIDs), len(results), f.Include, f.Exclude, resourceIDs(results))
t.FailNow()
}
}

for i, res := range results {
id := res.ID()
if id != test.expectIDs[i] {
t.Errorf("%s: expected %s, got %s\nincludes: %v, excludes: %v", test.name, test.expectIDs[i], id, f.Include, f.Exclude)
t.FailNow()
}
}
})
}
}

Expand Down

0 comments on commit 1fedb7f

Please sign in to comment.