-
Notifications
You must be signed in to change notification settings - Fork 208
/
factory.go
307 lines (269 loc) · 12.7 KB
/
factory.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package factory
import (
"context"
"fmt"
"time"
"github.com/robfig/cron"
"k8s.io/apimachinery/pkg/runtime"
errorutil "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/cache"
"github.com/openshift/library-go/pkg/operator/events"
operatorv1helpers "github.com/openshift/library-go/pkg/operator/v1helpers"
)
// DefaultQueueKey is the queue key used for string trigger based controllers.
const DefaultQueueKey = "key"
// DefaultQueueKeysFunc returns a slice with a single element - the DefaultQueueKey
func DefaultQueueKeysFunc(_ runtime.Object) []string {
return []string{DefaultQueueKey}
}
// Factory is generator that generate standard Kubernetes controllers.
// Factory is really generic and should be only used for simple controllers that does not require special stuff..
type Factory struct {
sync SyncFunc
syncContext SyncContext
syncDegradedClient operatorv1helpers.OperatorClient
resyncInterval time.Duration
resyncSchedules []string
informers []filteredInformers
informerQueueKeys []informersWithQueueKey
bareInformers []Informer
postStartHooks []PostStartHook
namespaceInformers []*namespaceInformer
cachesToSync []cache.InformerSynced
}
// Informer represents any structure that allow to register event handlers and informs if caches are synced.
// Any SharedInformer will comply.
type Informer interface {
AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error)
HasSynced() bool
}
type namespaceInformer struct {
informer Informer
nsFilter EventFilterFunc
}
type informersWithQueueKey struct {
informers []Informer
filter EventFilterFunc
queueKeyFn ObjectQueueKeysFunc
}
type filteredInformers struct {
informers []Informer
filter EventFilterFunc
}
// PostStartHook specify a function that will run after controller is started.
// The context is cancelled when the controller is asked to shutdown and the post start hook should terminate as well.
// The syncContext allow access to controller queue and event recorder.
type PostStartHook func(ctx context.Context, syncContext SyncContext) error
// ObjectQueueKeyFunc is used to make a string work queue key out of the runtime object that is passed to it.
// This can extract the "namespace/name" if you need to or just return "key" if you building controller that only use string
// triggers.
// DEPRECATED: use ObjectQueueKeysFunc instead
type ObjectQueueKeyFunc func(runtime.Object) string
// ObjectQueueKeysFunc is used to make a string work queue keys out of the runtime object that is passed to it.
// This can extract the "namespace/name" if you need to or just return "key" if you building controller that only use string
// triggers.
type ObjectQueueKeysFunc func(runtime.Object) []string
// EventFilterFunc is used to filter informer events to prevent Sync() from being called
type EventFilterFunc func(obj interface{}) bool
// New return new factory instance.
func New() *Factory {
return &Factory{}
}
// Sync is used to set the controller synchronization function. This function is the core of the controller and is
// usually hold the main controller logic.
func (f *Factory) WithSync(syncFn SyncFunc) *Factory {
f.sync = syncFn
return f
}
// WithInformers is used to register event handlers and get the caches synchronized functions.
// Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function
// is called.
func (f *Factory) WithInformers(informers ...Informer) *Factory {
f.WithFilteredEventsInformers(nil, informers...)
return f
}
// WithFilteredEventsInformers is used to register event handlers and get the caches synchronized functions.
// Pass the informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function
// is called.
// Pass filter to filter out events that should not trigger Sync() call.
func (f *Factory) WithFilteredEventsInformers(filter EventFilterFunc, informers ...Informer) *Factory {
f.informers = append(f.informers, filteredInformers{
informers: informers,
filter: filter,
})
return f
}
// WithBareInformers allow to register informer that already has custom event handlers registered and no additional
// event handlers will be added to this informer.
// The controller will wait for the cache of this informer to be synced.
// The existing event handlers will have to respect the queue key function or the sync() implementation will have to
// count with custom queue keys.
func (f *Factory) WithBareInformers(informers ...Informer) *Factory {
f.bareInformers = append(f.bareInformers, informers...)
return f
}
// WithInformersQueueKeyFunc is used to register event handlers and get the caches synchronized functions.
// Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function
// is called.
// Pass the queueKeyFn you want to use to transform the informer runtime.Object into string key used by work queue.
func (f *Factory) WithInformersQueueKeyFunc(queueKeyFn ObjectQueueKeyFunc, informers ...Informer) *Factory {
f.informerQueueKeys = append(f.informerQueueKeys, informersWithQueueKey{
informers: informers,
queueKeyFn: func(o runtime.Object) []string {
return []string{queueKeyFn(o)}
},
})
return f
}
// WithFilteredEventsInformersQueueKeyFunc is used to register event handlers and get the caches synchronized functions.
// Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function
// is called.
// Pass the queueKeyFn you want to use to transform the informer runtime.Object into string key used by work queue.
// Pass filter to filter out events that should not trigger Sync() call.
func (f *Factory) WithFilteredEventsInformersQueueKeyFunc(queueKeyFn ObjectQueueKeyFunc, filter EventFilterFunc, informers ...Informer) *Factory {
f.informerQueueKeys = append(f.informerQueueKeys, informersWithQueueKey{
informers: informers,
filter: filter,
queueKeyFn: func(o runtime.Object) []string {
return []string{queueKeyFn(o)}
},
})
return f
}
// WithInformersQueueKeysFunc is used to register event handlers and get the caches synchronized functions.
// Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function
// is called.
// Pass the queueKeyFn you want to use to transform the informer runtime.Object into string key used by work queue.
func (f *Factory) WithInformersQueueKeysFunc(queueKeyFn ObjectQueueKeysFunc, informers ...Informer) *Factory {
f.informerQueueKeys = append(f.informerQueueKeys, informersWithQueueKey{
informers: informers,
queueKeyFn: queueKeyFn,
})
return f
}
// WithFilteredEventsInformersQueueKeysFunc is used to register event handlers and get the caches synchronized functions.
// Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function
// is called.
// Pass the queueKeyFn you want to use to transform the informer runtime.Object into string key used by work queue.
// Pass filter to filter out events that should not trigger Sync() call.
func (f *Factory) WithFilteredEventsInformersQueueKeysFunc(queueKeyFn ObjectQueueKeysFunc, filter EventFilterFunc, informers ...Informer) *Factory {
f.informerQueueKeys = append(f.informerQueueKeys, informersWithQueueKey{
informers: informers,
filter: filter,
queueKeyFn: queueKeyFn,
})
return f
}
// WithPostStartHooks allows to register functions that will run asynchronously after the controller is started via Run command.
func (f *Factory) WithPostStartHooks(hooks ...PostStartHook) *Factory {
f.postStartHooks = append(f.postStartHooks, hooks...)
return f
}
// WithNamespaceInformer is used to register event handlers and get the caches synchronized functions.
// The sync function will only trigger when the object observed by this informer is a namespace and its name matches the interestingNamespaces.
// Do not use this to register non-namespace informers.
func (f *Factory) WithNamespaceInformer(informer Informer, interestingNamespaces ...string) *Factory {
f.namespaceInformers = append(f.namespaceInformers, &namespaceInformer{
informer: informer,
nsFilter: namespaceChecker(interestingNamespaces),
})
return f
}
// ResyncEvery will cause the Sync() function to be called periodically, regardless of informers.
// This is useful when you want to refresh every N minutes or you fear that your informers can be stucked.
// If this is not called, no periodical resync will happen.
// Note: The controller context passed to Sync() function in this case does not contain the object metadata or object itself.
//
// This can be used to detect periodical resyncs, but normal Sync() have to be cautious about `nil` objects.
func (f *Factory) ResyncEvery(interval time.Duration) *Factory {
f.resyncInterval = interval
return f
}
// ResyncSchedule allows to supply a Cron syntax schedule that will be used to schedule the sync() call runs.
// This allows more fine-tuned controller scheduling than ResyncEvery.
// Examples:
//
// factory.New().ResyncSchedule("@every 1s").ToController() // Every second
// factory.New().ResyncSchedule("@hourly").ToController() // Every hour
// factory.New().ResyncSchedule("30 * * * *").ToController() // Every hour on the half hour
//
// Note: The controller context passed to Sync() function in this case does not contain the object metadata or object itself.
//
// This can be used to detect periodical resyncs, but normal Sync() have to be cautious about `nil` objects.
func (f *Factory) ResyncSchedule(schedules ...string) *Factory {
f.resyncSchedules = append(f.resyncSchedules, schedules...)
return f
}
// WithSyncContext allows to specify custom, existing sync context for this factory.
// This is useful during unit testing where you can override the default event recorder or mock the runtime objects.
// If this function not called, a SyncContext is created by the factory automatically.
func (f *Factory) WithSyncContext(ctx SyncContext) *Factory {
f.syncContext = ctx
return f
}
// WithSyncDegradedOnError encapsulate the controller sync() function, so when this function return an error, the operator client
// is used to set the degraded condition to (eg. "ControllerFooDegraded"). The degraded condition name is set based on the controller name.
func (f *Factory) WithSyncDegradedOnError(operatorClient operatorv1helpers.OperatorClient) *Factory {
f.syncDegradedClient = operatorClient
return f
}
// Controller produce a runnable controller.
func (f *Factory) ToController(name string, eventRecorder events.Recorder) Controller {
if f.sync == nil {
panic(fmt.Errorf("WithSync() must be used before calling ToController() in %q", name))
}
var ctx SyncContext
if f.syncContext != nil {
ctx = f.syncContext
} else {
ctx = NewSyncContext(name, eventRecorder)
}
var cronSchedules []cron.Schedule
if len(f.resyncSchedules) > 0 {
var errors []error
for _, schedule := range f.resyncSchedules {
if s, err := cron.ParseStandard(schedule); err != nil {
errors = append(errors, err)
} else {
cronSchedules = append(cronSchedules, s)
}
}
if err := errorutil.NewAggregate(errors); err != nil {
panic(fmt.Errorf("failed to parse controller schedules for %q: %v", name, err))
}
}
c := &baseController{
name: name,
syncDegradedClient: f.syncDegradedClient,
sync: f.sync,
resyncEvery: f.resyncInterval,
resyncSchedules: cronSchedules,
cachesToSync: append([]cache.InformerSynced{}, f.cachesToSync...),
syncContext: ctx,
postStartHooks: f.postStartHooks,
cacheSyncTimeout: defaultCacheSyncTimeout,
}
for i := range f.informerQueueKeys {
for d := range f.informerQueueKeys[i].informers {
informer := f.informerQueueKeys[i].informers[d]
queueKeyFn := f.informerQueueKeys[i].queueKeyFn
informer.AddEventHandler(c.syncContext.(syncContext).eventHandler(queueKeyFn, f.informerQueueKeys[i].filter))
c.cachesToSync = append(c.cachesToSync, informer.HasSynced)
}
}
for i := range f.informers {
for d := range f.informers[i].informers {
informer := f.informers[i].informers[d]
informer.AddEventHandler(c.syncContext.(syncContext).eventHandler(DefaultQueueKeysFunc, f.informers[i].filter))
c.cachesToSync = append(c.cachesToSync, informer.HasSynced)
}
}
for i := range f.bareInformers {
c.cachesToSync = append(c.cachesToSync, f.bareInformers[i].HasSynced)
}
for i := range f.namespaceInformers {
f.namespaceInformers[i].informer.AddEventHandler(c.syncContext.(syncContext).eventHandler(DefaultQueueKeysFunc, f.namespaceInformers[i].nsFilter))
c.cachesToSync = append(c.cachesToSync, f.namespaceInformers[i].informer.HasSynced)
}
return c
}