Skip to content

Commit

Permalink
Implement metautils.FilterList
Browse files Browse the repository at this point in the history
  • Loading branch information
adracus committed Jul 27, 2022
1 parent 745225e commit 82d8ae6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.43.0
version: v1.47.2
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.1.2 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
15 changes: 15 additions & 0 deletions metautils/metautils.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,21 @@ func EachListItem(list client.ObjectList, f func(obj client.Object) error) error
})
}

// FilterList filters the list with the given function, mutating it in-place with the filtered objects.
func FilterList(list client.ObjectList, f func(obj client.Object) bool) error {
var filtered []client.Object
if err := EachListItem(list, func(obj client.Object) error {
if f(obj) {
filtered = append(filtered, obj)
}
return nil
}); err != nil {
return fmt.Errorf("error filtering list: %w", err)
}

return SetList(list, filtered)
}

// SetLabel sets the given label on the object.
func SetLabel(obj metav1.Object, key, value string) {
labels := obj.GetLabels()
Expand Down
25 changes: 25 additions & 0 deletions metautils/metautils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package metautils_test

import (
"reflect"
"strings"

"github.com/golang/mock/gomock"
. "github.com/onmetal/controller-utils/metautils"
Expand Down Expand Up @@ -555,4 +556,28 @@ var _ = Describe("Metautils", func() {
Entry("other keys present", map[string]string{"bar": "baz"}, map[string]string{"foo": "bar"}, map[string]string{"bar": "baz", "foo": "bar"}),
Entry("partial other keys, same key", map[string]string{"foo": "baz", "bar": "baz"}, map[string]string{"foo": "bar"}, map[string]string{"foo": "bar", "bar": "baz"}),
)

Describe("FilterList", func() {
It("should filter the list with the given function", func() {
list := &corev1.SecretList{
Items: []corev1.Secret{
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"}},
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "baz"}},
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "qux"}},
{ObjectMeta: metav1.ObjectMeta{Namespace: "bar", Name: "bar"}},
},
}

Expect(FilterList(list, func(obj client.Object) bool {
return obj.GetNamespace() == "foo" && strings.HasPrefix(obj.GetName(), "b")
})).To(Succeed())

Expect(list).To(Equal(&corev1.SecretList{
Items: []corev1.Secret{
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"}},
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "baz"}},
},
}))
})
})
})

0 comments on commit 82d8ae6

Please sign in to comment.