...
1
16
17
18
19 package externalversions
20
21 import (
22 reflect "reflect"
23 sync "sync"
24 time "time"
25
26 versioned "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
27 internalinterfaces "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions/internalinterfaces"
28 k8scnicncfio "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions/k8s.cni.cncf.io"
29 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30 runtime "k8s.io/apimachinery/pkg/runtime"
31 schema "k8s.io/apimachinery/pkg/runtime/schema"
32 cache "k8s.io/client-go/tools/cache"
33 )
34
35
36 type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
37
38 type sharedInformerFactory struct {
39 client versioned.Interface
40 namespace string
41 tweakListOptions internalinterfaces.TweakListOptionsFunc
42 lock sync.Mutex
43 defaultResync time.Duration
44 customResync map[reflect.Type]time.Duration
45
46 informers map[reflect.Type]cache.SharedIndexInformer
47
48
49 startedInformers map[reflect.Type]bool
50 }
51
52
53 func WithCustomResyncConfig(resyncConfig map[v1.Object]time.Duration) SharedInformerOption {
54 return func(factory *sharedInformerFactory) *sharedInformerFactory {
55 for k, v := range resyncConfig {
56 factory.customResync[reflect.TypeOf(k)] = v
57 }
58 return factory
59 }
60 }
61
62
63 func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
64 return func(factory *sharedInformerFactory) *sharedInformerFactory {
65 factory.tweakListOptions = tweakListOptions
66 return factory
67 }
68 }
69
70
71 func WithNamespace(namespace string) SharedInformerOption {
72 return func(factory *sharedInformerFactory) *sharedInformerFactory {
73 factory.namespace = namespace
74 return factory
75 }
76 }
77
78
79 func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
80 return NewSharedInformerFactoryWithOptions(client, defaultResync)
81 }
82
83
84
85
86
87 func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory {
88 return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
89 }
90
91
92 func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
93 factory := &sharedInformerFactory{
94 client: client,
95 namespace: v1.NamespaceAll,
96 defaultResync: defaultResync,
97 informers: make(map[reflect.Type]cache.SharedIndexInformer),
98 startedInformers: make(map[reflect.Type]bool),
99 customResync: make(map[reflect.Type]time.Duration),
100 }
101
102
103 for _, opt := range options {
104 factory = opt(factory)
105 }
106
107 return factory
108 }
109
110
111 func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
112 f.lock.Lock()
113 defer f.lock.Unlock()
114
115 for informerType, informer := range f.informers {
116 if !f.startedInformers[informerType] {
117 go informer.Run(stopCh)
118 f.startedInformers[informerType] = true
119 }
120 }
121 }
122
123
124 func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
125 informers := func() map[reflect.Type]cache.SharedIndexInformer {
126 f.lock.Lock()
127 defer f.lock.Unlock()
128
129 informers := map[reflect.Type]cache.SharedIndexInformer{}
130 for informerType, informer := range f.informers {
131 if f.startedInformers[informerType] {
132 informers[informerType] = informer
133 }
134 }
135 return informers
136 }()
137
138 res := map[reflect.Type]bool{}
139 for informType, informer := range informers {
140 res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
141 }
142 return res
143 }
144
145
146
147 func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
148 f.lock.Lock()
149 defer f.lock.Unlock()
150
151 informerType := reflect.TypeOf(obj)
152 informer, exists := f.informers[informerType]
153 if exists {
154 return informer
155 }
156
157 resyncPeriod, exists := f.customResync[informerType]
158 if !exists {
159 resyncPeriod = f.defaultResync
160 }
161
162 informer = newFunc(f.client, resyncPeriod)
163 f.informers[informerType] = informer
164
165 return informer
166 }
167
168
169
170 type SharedInformerFactory interface {
171 internalinterfaces.SharedInformerFactory
172 ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
173 WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
174
175 K8sCniCncfIo() k8scnicncfio.Interface
176 }
177
178 func (f *sharedInformerFactory) K8sCniCncfIo() k8scnicncfio.Interface {
179 return k8scnicncfio.New(f, f.namespace, f.tweakListOptions)
180 }
181
View as plain text