-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyacht.go
344 lines (300 loc) · 9.92 KB
/
yacht.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package yacht
import (
"context"
"fmt"
"sync"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/leaderelection"
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
utilpointer "k8s.io/utils/pointer"
"github.com/dixudx/yacht/utils"
)
type Controller struct {
// name is the name of this controller
name string
// workers indicates the number of workers
workers *int
// enqueueFunc defines the function to enqueue the work item
enqueueFunc EnqueueFunc
// enqueueFilterFunc defines the filter function before enqueueing the work item
enqueueFilterFunc EnqueueFilterFunc
// queue is a rate limited work queue.
queue workqueue.RateLimitingInterface
// informersSynced records a group of cacheSyncs
// The workers will not start working before all the caches are synced successfully
informersSynced []cache.InformerSynced
// handlerContextFunc defines the handler to process the work item
handlerContextFunc HandlerContextFunc
// le specifies the LeaderElector to use
le *leaderelection.LeaderElector
// runFlag indicates whether the workers start working
runFlag bool
once sync.Once
}
var _ Interface = &Controller{}
// NewController creates a new Controller
func NewController(name string) *Controller {
return &Controller{
name: name,
workers: utilpointer.Int(2),
enqueueFunc: DefaultEnqueueFunc,
queue: workqueue.NewRateLimitingQueueWithConfig(
workqueue.DefaultControllerRateLimiter(),
workqueue.RateLimitingQueueConfig{
Name: name,
}),
informersSynced: []cache.InformerSynced{},
}
}
// WithWorkers sets the number of workers to process work items off work queue
func (c *Controller) WithWorkers(workers int) *Controller {
if c.runFlag {
panic(fmt.Errorf("can not mutate workers when controller %s is running", c.name))
}
if workers < 0 {
panic(fmt.Errorf("can not set negative workers %d", workers))
}
c.workers = utilpointer.Int(workers)
return c
}
// WithQueue replaces the default queue with the desired one to store work items.
func (c *Controller) WithQueue(queue workqueue.RateLimitingInterface) *Controller {
if c.runFlag {
panic(fmt.Errorf("can not mutate queue when controller %s is running", c.name))
}
c.queue = queue
return c
}
// WithEnqueueFilterFunc sets customize enqueueFilterFunc
func (c *Controller) WithEnqueueFilterFunc(enqueueFilterFunc EnqueueFilterFunc) *Controller {
if c.runFlag {
panic(fmt.Errorf("can not mutate enqueueFilterFunc when controller %s is running", c.name))
}
c.enqueueFilterFunc = enqueueFilterFunc
return c
}
// WithEnqueueFunc sets customize enqueueFunc
func (c *Controller) WithEnqueueFunc(enqueueFunc EnqueueFunc) *Controller {
if c.runFlag {
panic(fmt.Errorf("can not mutate enqueueFunc when controller %s is running", c.name))
}
if enqueueFunc != nil {
c.enqueueFunc = enqueueFunc
}
return c
}
func (c *Controller) DefaultResourceEventHandlerFuncs() cache.ResourceEventHandlerFuncs {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if c.applyEnqueueFilterFunc(nil, obj, cache.Added) {
c.Enqueue(obj)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
if c.applyEnqueueFilterFunc(oldObj, newObj, cache.Updated) {
c.Enqueue(newObj)
}
},
DeleteFunc: func(obj interface{}) {
if c.applyEnqueueFilterFunc(obj, nil, cache.Deleted) {
c.Enqueue(obj)
}
},
}
}
func (c *Controller) applyEnqueueFilterFunc(oldObj, newObj interface{}, operation cache.DeltaType) bool {
if c.enqueueFilterFunc == nil {
obj := oldObj
if obj == nil {
obj = newObj
}
utils.DepthLogging(nil, "info", fmt.Sprintf("[%s] enqueue resource", operation), obj)
return true
}
var err error
var ok bool
switch operation {
case cache.Added:
ok, err = c.enqueueFilterFunc(nil, newObj)
case cache.Deleted:
ok, err = c.enqueueFilterFunc(oldObj, nil)
case cache.Updated:
ok, err = c.enqueueFilterFunc(oldObj, newObj)
default:
utils.DepthLogging(nil, "error", fmt.Sprintf("[%s] unexpected resource event type", operation), oldObj)
return false
}
if err != nil {
utils.DepthLogging(err, "error", fmt.Sprintf("[%s] failed to apply enqueueFilterFunc", operation), oldObj)
return false
}
if !ok {
utils.DepthLogging(nil, "warning", fmt.Sprintf("[%s] not enqueue resource", operation), oldObj)
return false
}
if operation == cache.Deleted {
utils.DepthLogging(nil, "info", fmt.Sprintf("[%s] enqueue resource", operation), oldObj)
} else {
utils.DepthLogging(nil, "info", fmt.Sprintf("[%s] enqueue resource", operation), newObj)
}
return true
}
// WithHandlerFunc sets a handler function to process the work item off the work queue
// Deprecated: Use WithHandlerContextFunc instead.
func (c *Controller) WithHandlerFunc(handlerFunc HandlerFunc) *Controller {
if c.runFlag {
panic(fmt.Errorf("can not mutate handlerContextFunc when controller %s is running", c.name))
}
if handlerFunc != nil {
c.handlerContextFunc = func(ctx context.Context, key interface{}) (requeueAfter *time.Duration, err error) {
select {
case <-ctx.Done():
return
default:
return handlerFunc(key)
}
}
}
return c
}
// WithHandlerContextFunc sets a handler function to process the work item off the work queue
func (c *Controller) WithHandlerContextFunc(handlerContextFunc HandlerContextFunc) *Controller {
if c.runFlag {
panic(fmt.Errorf("can not mutate handlerContextFunc when controller %s is running", c.name))
}
if handlerContextFunc != nil {
c.handlerContextFunc = handlerContextFunc
}
return c
}
// WithLeaderElection uses leader election to get the lock
func (c *Controller) WithLeaderElection(leaseLock rl.Interface, leaseDuration, renewDeadline, retryPeriod time.Duration) *Controller {
if c.runFlag {
panic(fmt.Errorf("can not mutate leaderElection when controller %s is running", c.name))
}
lec := leaderelection.LeaderElectionConfig{
Lock: leaseLock,
// IMPORTANT: you MUST ensure that any code you have that is protected by the lease must terminate **before**
// you call cancel. Otherwise, you could have a background loop still running and another process could
// get elected before your background loop finished, violating the stated goal of the lease.
ReleaseOnCancel: true,
LeaseDuration: leaseDuration,
RenewDeadline: renewDeadline,
RetryPeriod: retryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
c.run(ctx)
},
OnStoppedLeading: func() {
klog.Errorf("leader election got lost for controller %s", c.name)
},
OnNewLeader: func(identity string) {
// gets notified when new leader is elected
if identity == leaseLock.Identity() {
// I just got the lock
return
}
klog.Infof("new leader %s is elected for controller %s", identity, c.name)
},
},
}
le, err := leaderelection.NewLeaderElector(lec)
if err != nil {
panic(fmt.Errorf("failed to create a LeaderElector for controller %s: %v", c.name, err))
}
c.le = le
return c
}
// WithCacheSynced sets all the resource cacheSynced
func (c *Controller) WithCacheSynced(informersSynced ...cache.InformerSynced) *Controller {
c.informersSynced = append(c.informersSynced, informersSynced...)
return c
}
// Enqueue takes an object and converts it into a key (could be a string, or a struct) which is then put onto the
// work queue.
func (c *Controller) Enqueue(obj interface{}) {
key, err := c.enqueueFunc(obj)
if err != nil {
utilruntime.HandleError(err)
return
}
c.queue.Add(key)
}
// Run will start multiple workers to process work items from work queue. It will block until ctx is closed.
func (c *Controller) Run(ctx context.Context) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
if c.handlerContextFunc == nil {
panic(fmt.Errorf("please set handlerContextFunc for controller %s", c.name))
}
c.once.Do(func() {
if c.le != nil {
wait.UntilWithContext(ctx, c.le.Run, time.Duration(0))
return
}
c.run(ctx)
})
}
func (c *Controller) run(ctx context.Context) {
klog.Infof("starting controller %s", c.name)
defer klog.Infof("shutting down controller %s", c.name)
c.runFlag = true
// Wait for all the caches to be synced before starting workers
if !cache.WaitForNamedCacheSync(c.name, ctx.Done(), c.informersSynced...) {
return
}
klog.V(4).Infof("starting %d workers for controller %s", *c.workers, c.name)
// Launch workers to process work items from queue
for i := 0; i < *c.workers; i++ {
go wait.UntilWithContext(ctx, c.runWorker, time.Second)
}
<-ctx.Done()
klog.V(4).Infof("stopped %d workers for controller %s", *c.workers, c.name)
}
// runWorker starts an infinite loop on processing the work item until the work queue is shut down.
func (c *Controller) runWorker(ctx context.Context) {
for c.processNextWorkItem(ctx) {
}
}
// processNextWorkItem reads a single work item from the work queue
func (c *Controller) processNextWorkItem(ctx context.Context) bool {
item, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(item)
requeueAfter, err := c.handlerContextFunc(ctx, item)
if err == nil {
c.queue.Forget(item)
if requeueAfter != nil {
// Sometimes we may want to re-visit this object after a while.
// Put the item back on the work queue with delay.
c.queue.AddAfter(item, *requeueAfter)
}
return true
}
if apierrors.IsNotFound(err) {
c.queue.Forget(item)
return true
}
utilruntime.HandleError(err)
// put the item back on the work queue to handle any transient errors
if requeueAfter != nil {
c.queue.AddAfter(item, *requeueAfter)
} else {
c.queue.AddRateLimited(item)
}
return true
}
// DefaultEnqueueFunc uses a default namespacedKey as its KeyFunc.
// The key uses the format <namespace>/<name> unless <namespace> is empty, then
// it's just <name>.
func DefaultEnqueueFunc(obj interface{}) (interface{}, error) {
return cache.MetaNamespaceKeyFunc(obj)
}