Add symmetric filter helped based on OwnerRefable. (#2032)

Most resources stamped out by knative controllers have OwnerReferences synthesized via `kmeta.NewOwnerRef`, which requires the parent resource to implement `kmeta.OwnerRefable` for accessing the `GroupVersionKind`.

However, where we setup informer watches on those same children resources we have essentially relied on direct synthesis of the `Group[Version]Kind` where we could instead provide an empty instance of the controller resource and leverage `GetGroupVersionKind` to provide the GVK used for filtration.

So where before folks would write:

```golang
FilterFunc: controller.FilterControllerGK(v1alpha1.WithKind("MyType"))
```

They may now write:

```golang
FilterFunc: controller.FilterController(&v1alpha1.MyType{})
```

The latter is preferable in part because it is more strongly typed, but also shorter.
This commit is contained in:
Matt Moore 2021-02-19 08:47:03 -08:00 committed by GitHub
parent 29092fe5b8
commit 8a9bf766d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 0 deletions

View File

@ -152,6 +152,13 @@ func FilterControllerGK(gk schema.GroupKind) func(obj interface{}) bool {
}
}
// FilterController makes it simple to create FilterFunc's for use with
// cache.FilteringResourceEventHandler that filter based on the
// controlling resource.
func FilterController(r kmeta.OwnerRefable) func(obj interface{}) bool {
return FilterControllerGK(r.GetGroupVersionKind().GroupKind())
}
// FilterWithName makes it simple to create FilterFunc's for use with
// cache.FilteringResourceEventHandler that filter based on a name.
func FilterWithName(name string) func(obj interface{}) bool {