diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index e11b427..5697574 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -17,4 +17,4 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.43.0 + version: v1.47.2 diff --git a/go.mod b/go.mod index 1a274f6..f8c358d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 6b9a8c9..8ed5675 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/metautils/metautils.go b/metautils/metautils.go index 5f4f168..b2bcc26 100644 --- a/metautils/metautils.go +++ b/metautils/metautils.go @@ -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() diff --git a/metautils/metautils_test.go b/metautils/metautils_test.go index f08d187..dea09b2 100644 --- a/metautils/metautils_test.go +++ b/metautils/metautils_test.go @@ -16,6 +16,7 @@ package metautils_test import ( "reflect" + "strings" "github.com/golang/mock/gomock" . "github.com/onmetal/controller-utils/metautils" @@ -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"}}, + }, + })) + }) + }) })