Skip to content

Commit

Permalink
fix: predicates with incremental informer
Browse files Browse the repository at this point in the history
Signed-off-by: KevFan <[email protected]>
  • Loading branch information
KevFan committed Nov 29, 2024
1 parent ca213ee commit 370bbb0
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions controller/runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/event"

ctrlruntime "sigs.k8s.io/controller-runtime"
ctrlruntimehandler "sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -77,29 +78,29 @@ func Watch[T Object](obj T, resource schema.GroupVersionResource, namespace stri
return o.Builder(obj, resource, namespace, options...)
}

func IncrementalInformer[T Object](obj T, resource schema.GroupVersionResource, namespace string, options ...RunnableBuilderOption[T]) RunnableBuilder {
o := &RunnableBuilderOptions[T]{}
func IncrementalInformer[T Object](_ T, resource schema.GroupVersionResource, namespace string, options ...RunnableBuilderOption[T]) RunnableBuilder {
opts := &RunnableBuilderOptions[T]{}
for _, f := range options {
f(o)
f(opts)
}
return func(controller *Controller) Runnable {
informer := cache.NewSharedInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
if o.LabelSelector != "" {
options.LabelSelector = o.LabelSelector
if opts.LabelSelector != "" {
options.LabelSelector = opts.LabelSelector
}
if o.FieldSelector != "" {
options.FieldSelector = o.FieldSelector
if opts.FieldSelector != "" {
options.FieldSelector = opts.FieldSelector
}
return controller.client.Resource(resource).Namespace(namespace).List(context.Background(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
if o.LabelSelector != "" {
options.LabelSelector = o.LabelSelector
if opts.LabelSelector != "" {
options.LabelSelector = opts.LabelSelector
}
if o.FieldSelector != "" {
options.FieldSelector = o.FieldSelector
if opts.FieldSelector != "" {
options.FieldSelector = opts.FieldSelector
}
return controller.client.Resource(resource).Namespace(namespace).Watch(context.Background(), options)
},
Expand All @@ -110,15 +111,30 @@ func IncrementalInformer[T Object](obj T, resource schema.GroupVersionResource,
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(o any) {
obj := o.(T)
for _, p := range opts.Predicates {
if !p.Create(event.TypedCreateEvent[T]{Object: obj}) {
return
}
}
controller.add(obj)
},
UpdateFunc: func(o, newO any) {
oldObj := o.(T)
newObj := newO.(T)
for _, p := range opts.Predicates {
if !p.Update(event.TypedUpdateEvent[T]{ObjectOld: oldObj, ObjectNew: newObj}) {
return
}
}
controller.update(oldObj, newObj)
},
DeleteFunc: func(o any) {
obj := o.(T)
for _, p := range opts.Predicates {
if !p.Delete(event.TypedDeleteEvent[T]{Object: obj}) {
return
}
}
controller.delete(obj)
},
})
Expand Down

0 comments on commit 370bbb0

Please sign in to comment.