Skip to content

Commit

Permalink
Merge pull request #987 from wzshiming/clean/informer
Browse files Browse the repository at this point in the history
Refactor informer
  • Loading branch information
wzshiming authored Mar 15, 2024
2 parents 643b90e + 55aa08e commit 7c7daed
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pkg/utils/informer/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package informer
import (
"context"
"fmt"
"reflect"
"sync"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -110,10 +110,6 @@ func (i *Informer[T, L]) WatchWithCache(ctx context.Context, opt Option, events

func newCacheInformer[T runtime.Object](listWatch cache.ListerWatcher, opt Option, events chan<- Event[T]) (cache.Store, cache.Controller) {
var t T
typ := reflect.TypeOf(t)
if typ.Kind() == reflect.Ptr {
t = reflect.New(typ.Elem()).Interface().(T)
}
eventHandler := cache.ResourceEventHandlerFuncs{}
if events != nil {
eventHandler = cache.ResourceEventHandlerFuncs{
Expand Down Expand Up @@ -154,7 +150,7 @@ func newCacheInformer[T runtime.Object](listWatch cache.ListerWatcher, opt Optio
return listWatch.Watch(opts)
},
},
t,
objType(t),
0,
eventHandler,
)
Expand All @@ -176,10 +172,6 @@ func (i *Informer[T, L]) Watch(ctx context.Context, opt Option, events chan<- Ev

func newDummyInformer[T runtime.Object](listWatch cache.ListerWatcher, opt Option, events chan<- Event[T]) *cache.Reflector {
var t T
typ := reflect.TypeOf(t)
if typ.Kind() == reflect.Ptr {
t = reflect.New(typ.Elem()).Interface().(T)
}
informer := cache.NewReflectorWithOptions(
&cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
Expand All @@ -191,7 +183,7 @@ func newDummyInformer[T runtime.Object](listWatch cache.ListerWatcher, opt Optio
return listWatch.Watch(opts)
},
},
t,
objType(t),
dummyCache(events, opt),
cache.ReflectorOptions{},
)
Expand Down Expand Up @@ -323,3 +315,13 @@ func (l *lazyGetter[T]) List() (list []T) {
}
return list
}

func objType(expectedType runtime.Object) runtime.Object {
switch expectedType.(type) {
default:
return expectedType
case *unstructured.Unstructured:
var obj unstructured.Unstructured
return &obj
}
}

0 comments on commit 7c7daed

Please sign in to comment.