-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinformer.go
83 lines (70 loc) · 2.1 KB
/
informer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package kool
import (
"context"
"reflect"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
type Informer[T any] interface {
Informer() cache.SharedIndexInformer
Lister() Lister[T]
}
type informer[T any] struct {
informer cache.SharedIndexInformer
}
func (m *informer[T]) Informer() cache.SharedIndexInformer {
return m.informer
}
func (m *informer[T]) Lister() Lister[T] {
return NewLister[T](m.Informer().GetIndexer())
}
func NewInformer[T any](client *rest.RESTClient, resyncPeriod time.Duration) Informer[T] {
return &informer[T]{newSharedIndexInformer[T](client, "", resyncPeriod)}
}
type NamespacedInformer[T any] interface {
Informer() cache.SharedIndexInformer
Lister() NamespacedLister[T]
}
type namespacedInformer[T any] struct {
ns string
informer cache.SharedIndexInformer
}
func (m *namespacedInformer[T]) Informer() cache.SharedIndexInformer {
return m.informer
}
func (m *namespacedInformer[T]) Lister() NamespacedLister[T] {
return &namespacedLister[T]{
ns: m.ns,
indexer: m.Informer().GetIndexer(),
}
}
func NewNamespacedInformer[T any](client *rest.RESTClient, ns string, resyncPeriod time.Duration) NamespacedInformer[T] {
return &namespacedInformer[T]{
ns: ns,
informer: newSharedIndexInformer[T](client, ns, resyncPeriod),
}
}
func newSharedIndexInformer[T any](client *rest.RESTClient, ns string, resyncPeriod time.Duration) cache.SharedIndexInformer {
obj := mustBeRuntimeObject(new(T))
kind := reflect.TypeOf(obj).Elem().Name()
resource := plural.Plural(strings.ToLower(kind))
c := NewTypedClient[T](client, ns, resource)
return cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return c.List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return c.Watch(context.TODO(), options)
},
},
obj,
resyncPeriod,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
)
}