-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathworkqueue.go
206 lines (170 loc) · 5.28 KB
/
workqueue.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
package kubehandler
import (
"context"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
const (
WorkqueueAddEvent string = "add"
WorkqueueUpdateEvent string = "update"
WorkqueueDeleteEvent string = "delete"
)
//WorkQueueHandler defines the contract of a handler function
type WorkQueueHandler func(ctx context.Context, namespace, name string) error
//WorkQueue manages the rate limiting interface
type WorkQueue interface {
Run(ctx context.Context, threadiness int) error
AddSynced(cache.InformerSynced)
EnqueueAdd(kind string, obj interface{})
EnqueueUpdate(kind string, obj interface{})
EnqueueDelete(kind string, obj interface{})
RegisterAddHandler(kind string, handler WorkQueueHandler)
RegisterUpdateHandler(kind string, handler WorkQueueHandler)
RegisterDeleteHandler(kind string, handler WorkQueueHandler)
Length() int
}
type workQueue struct {
name string
workqueue workqueue.RateLimitingInterface
informerSynced []cache.InformerSynced
addHandlerMap map[string]WorkQueueHandler
updateHandlerMap map[string]WorkQueueHandler
deleteHandlerMap map[string]WorkQueueHandler
}
func (q *workQueue) shutDown() {
q.workqueue.ShutDown()
}
func (q *workQueue) Length() int {
return q.workqueue.Len()
}
func (q *workQueue) AddSynced(informer cache.InformerSynced) {
q.informerSynced = append(q.informerSynced, informer)
}
//Run is the WorkQueue entry point
func (q *workQueue) Run(ctx context.Context, threadiness int) error {
defer runtime.HandleCrash()
defer q.shutDown()
if ok := cache.WaitForCacheSync(ctx.Done(), q.informerSynced...); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
for i := 0; i < threadiness; i++ {
go q.runWorker(ctx)
}
<-ctx.Done()
return nil
}
func (q *workQueue) runWorker(ctx context.Context) {
out:
for {
select {
case <-ctx.Done():
break out
default:
q.processNextWorkItem(ctx)
}
}
}
func (q *workQueue) handleAdd(ctx context.Context, kind, namespace, name string) error {
return q.addHandlerMap[kind](ctx, namespace, name)
}
func (q *workQueue) handleUpdate(ctx context.Context, kind, namespace, name string) error {
return q.updateHandlerMap[kind](ctx, namespace, name)
}
func (q *workQueue) handleDelete(ctx context.Context, kind, namespace, name string) error {
return q.deleteHandlerMap[kind](ctx, namespace, name)
}
func (q *workQueue) syncHandler(ctx context.Context, key string) error {
splitKey := strings.Split(key, ":")
eventType, kind, nsKey := splitKey[0], splitKey[1], splitKey[2]
namespace, name, err := cache.SplitMetaNamespaceKey(nsKey)
if err != nil {
runtime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
switch eventType {
case WorkqueueAddEvent:
return q.handleAdd(ctx, kind, namespace, name)
case WorkqueueUpdateEvent:
return q.handleUpdate(ctx, kind, namespace, name)
case WorkqueueDeleteEvent:
return q.handleDelete(ctx, kind, namespace, name)
default:
runtime.HandleError(fmt.Errorf("invalid event type: %s", eventType))
return nil
}
}
func (q *workQueue) processNextWorkItem(ctx context.Context) bool {
obj, shutdown := q.workqueue.Get()
if shutdown {
return false
}
err := func(obj interface{}) error {
defer q.workqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
q.workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
if err := q.syncHandler(ctx, key); err != nil {
return fmt.Errorf("error syncing '%s': %s", key, err.Error())
}
q.workqueue.Forget(obj)
return nil
}(obj)
if err != nil {
runtime.HandleError(err)
return true
}
return true
}
func (q *workQueue) EnqueueAdd(kind string, obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
q.workqueue.AddRateLimited(fmt.Sprintf("%s:%s:%s", WorkqueueAddEvent, kind, key))
}
func (q *workQueue) EnqueueUpdate(kind string, obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
q.workqueue.AddRateLimited(fmt.Sprintf("%s:%s:%s", WorkqueueUpdateEvent, kind, key))
}
func (q *workQueue) EnqueueDelete(kind string, obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
q.workqueue.AddRateLimited(fmt.Sprintf("%s:%s:%s", WorkqueueDeleteEvent, kind, key))
}
func (q *workQueue) RegisterAddHandler(kind string, handler WorkQueueHandler) {
q.addHandlerMap[kind] = handler
}
func (q *workQueue) RegisterUpdateHandler(kind string, handler WorkQueueHandler) {
q.updateHandlerMap[kind] = handler
}
func (q *workQueue) RegisterDeleteHandler(kind string, handler WorkQueueHandler) {
q.deleteHandlerMap[kind] = handler
}
//NewWorkQueue creates a WorkQueue with a name
func NewWorkQueue(name string) WorkQueue {
return &workQueue{
name: name,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
addHandlerMap: make(map[string]WorkQueueHandler),
updateHandlerMap: make(map[string]WorkQueueHandler),
deleteHandlerMap: make(map[string]WorkQueueHandler),
}
}