-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterquotamapping.go
396 lines (341 loc) · 11.7 KB
/
clusterquotamapping.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package clusterquotamapping
import (
"fmt"
"time"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/framework"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/workqueue"
ocache "github.com/openshift/origin/pkg/client/cache"
"github.com/openshift/origin/pkg/controller/shared"
quotaapi "github.com/openshift/origin/pkg/quota/api"
)
// Look out, here there be dragons!
// There is a race when dealing with the DeltaFifo compression used to back a reflector for a controller that uses two
// SharedInformers for both their watch events AND their caches. The scenario looks like this
//
// 1. Add, Delete a namespace really fast, *before* the add is observed by the controller using the reflector.
// 2. Add or Update a quota that matches the Add namespace
// 3. The cache had the intermediate state for the namespace for some period of time. This makes the quota update the mapping indicating a match.
// 4. The ns Delete is compressed out and never delivered to the controller, so the improper match is never cleared.
//
// This sounds pretty bad, however, we fail in the "safe" direction and the consequences are detectable.
// When going from quota to namespace, you can get back a namespace that doesn't exist. There are no resource in a non-existance
// namespace, so you know to clear all referenced resources. In addition, this add/delete has to happen so fast
// that it would be nearly impossible for any resources to be created. If you do create resources, then we must be observing
// their deletes. When quota is replenished, we'll see that we need to clear any charges.
//
// When going from namespace to quota, you can get back a quota that doesn't exist. Since the cache is shared,
// we know that a missing quota means that there isn't anything for us to bill against, so we can skip it.
//
// If the mapping cache is wrong and a previously deleted quota or namespace is created, this controller
// correctly adds the items back to the list and clears out all previous mappings.
//
// In addition to those constraints, the timing threshold for actually hitting this problem is really tight. It's
// basically a script that is creating and deleting things as fast as it possibly can. Sub-millisecond in the fuzz
// test where I caught the problem.
// NewClusterQuotaMappingController builds a mapping between namespaces and clusterresourcequotas
func NewClusterQuotaMappingController(namespaceInformer shared.NamespaceInformer, quotaInformer shared.ClusterResourceQuotaInformer) *ClusterQuotaMappingController {
c := &ClusterQuotaMappingController{
namespaceQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "controller_clusterquotamappingcontroller_namespaces"),
quotaQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "controller_clusterquotamappingcontroller_clusterquotas"),
clusterQuotaMapper: NewClusterQuotaMapper(),
}
namespaceInformer.Informer().AddEventHandler(framework.ResourceEventHandlerFuncs{
AddFunc: c.addNamespace,
UpdateFunc: c.updateNamespace,
DeleteFunc: c.deleteNamespace,
})
c.namespaceLister = namespaceInformer.Lister()
c.namespacesSynced = namespaceInformer.Informer().HasSynced
quotaInformer.Informer().AddEventHandler(framework.ResourceEventHandlerFuncs{
AddFunc: c.addQuota,
UpdateFunc: c.updateQuota,
DeleteFunc: c.deleteQuota,
})
c.quotaLister = quotaInformer.Lister()
c.quotasSynced = quotaInformer.Informer().HasSynced
return c
}
type ClusterQuotaMappingController struct {
namespaceQueue workqueue.RateLimitingInterface
namespaceLister *ocache.IndexerToNamespaceLister
namespacesSynced func() bool
quotaQueue workqueue.RateLimitingInterface
quotaLister *ocache.IndexerToClusterResourceQuotaLister
quotasSynced func() bool
clusterQuotaMapper *clusterQuotaMapper
}
func (c *ClusterQuotaMappingController) GetClusterQuotaMapper() ClusterQuotaMapper {
return c.clusterQuotaMapper
}
func (c *ClusterQuotaMappingController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
// Wait for the stores to sync before starting any work in this controller.
ready := make(chan struct{})
go c.waitForSyncedStores(ready, stopCh)
select {
case <-ready:
case <-stopCh:
return
}
glog.V(4).Infof("Starting workers for quota mapping controller workers")
for i := 0; i < workers; i++ {
go wait.Until(c.namespaceWorker, time.Second, stopCh)
go wait.Until(c.quotaWorker, time.Second, stopCh)
}
<-stopCh
glog.Infof("Shutting down quota mapping controller")
c.namespaceQueue.ShutDown()
c.quotaQueue.ShutDown()
}
func (c *ClusterQuotaMappingController) syncQuota(quota *quotaapi.ClusterResourceQuota) error {
matcherFunc, err := quotaapi.GetMatcher(quota.Spec.Selector)
if err != nil {
return err
}
allNamespaces, err := c.namespaceLister.List(kapi.ListOptions{})
if err != nil {
return err
}
for i := range allNamespaces {
namespace := allNamespaces[i]
// attempt to set the mapping. The quotas never collide with each other (same quota is never processed twice in parallel)
// so this means that the project we have is out of date, pull a more recent copy from the cache and retest
for {
matches, err := matcherFunc(namespace)
if err != nil {
utilruntime.HandleError(err)
break
}
success, quotaMatches, _ := c.clusterQuotaMapper.setMapping(quota, namespace, !matches)
if success {
break
}
// if the quota is mismatched, then someone has updated the quota or has deleted the entry entirely.
// if we've been updated, we'll be rekicked, if we've been deleted we should stop. Either way, this
// execution is finished
if !quotaMatches {
return nil
}
namespace, err = c.namespaceLister.Get(namespace.Name)
if kapierrors.IsNotFound(err) {
// if the namespace is gone, then the deleteNamespace path will be called, just continue
break
}
if err != nil {
utilruntime.HandleError(err)
break
}
}
}
c.clusterQuotaMapper.completeQuota(quota)
return nil
}
func (c *ClusterQuotaMappingController) syncNamespace(namespace *kapi.Namespace) error {
allQuotas, err1 := c.quotaLister.List(kapi.ListOptions{})
if err1 != nil {
return err1
}
for i := range allQuotas {
quota := allQuotas[i]
for {
matcherFunc, err := quotaapi.GetMatcher(quota.Spec.Selector)
if err != nil {
utilruntime.HandleError(err)
break
}
// attempt to set the mapping. The namespaces never collide with each other (same namespace is never processed twice in parallel)
// so this means that the quota we have is out of date, pull a more recent copy from the cache and retest
matches, err := matcherFunc(namespace)
if err != nil {
utilruntime.HandleError(err)
break
}
success, _, namespaceMatches := c.clusterQuotaMapper.setMapping(quota, namespace, !matches)
if success {
break
}
// if the namespace is mismatched, then someone has updated the namespace or has deleted the entry entirely.
// if we've been updated, we'll be rekicked, if we've been deleted we should stop. Either way, this
// execution is finished
if !namespaceMatches {
return nil
}
quota, err = c.quotaLister.Get(quota.Name)
if kapierrors.IsNotFound(err) {
// if the quota is gone, then the deleteQuota path will be called, just continue
break
}
if err != nil {
utilruntime.HandleError(err)
break
}
}
}
c.clusterQuotaMapper.completeNamespace(namespace)
return nil
}
func (c *ClusterQuotaMappingController) quotaWork() bool {
key, quit := c.quotaQueue.Get()
if quit {
return true
}
defer c.quotaQueue.Done(key)
quota, exists, err := c.quotaLister.GetByKey(key.(string))
if !exists {
c.quotaQueue.Forget(key)
return false
}
if err != nil {
utilruntime.HandleError(err)
return false
}
err = c.syncQuota(quota.(*quotaapi.ClusterResourceQuota))
outOfRetries := c.quotaQueue.NumRequeues(key) > 5
switch {
case err != nil && outOfRetries:
utilruntime.HandleError(err)
c.quotaQueue.Forget(key)
case err != nil && !outOfRetries:
c.quotaQueue.AddRateLimited(key)
default:
c.quotaQueue.Forget(key)
}
return false
}
func (c *ClusterQuotaMappingController) quotaWorker() {
for {
if quit := c.quotaWork(); quit {
return
}
}
}
func (c *ClusterQuotaMappingController) namespaceWork() bool {
key, quit := c.namespaceQueue.Get()
if quit {
return true
}
defer c.namespaceQueue.Done(key)
namespace, exists, err := c.namespaceLister.GetByKey(key.(string))
if !exists {
c.namespaceQueue.Forget(key)
return false
}
if err != nil {
utilruntime.HandleError(err)
return false
}
err = c.syncNamespace(namespace.(*kapi.Namespace))
outOfRetries := c.namespaceQueue.NumRequeues(key) > 5
switch {
case err != nil && outOfRetries:
utilruntime.HandleError(err)
c.namespaceQueue.Forget(key)
case err != nil && !outOfRetries:
c.namespaceQueue.AddRateLimited(key)
default:
c.namespaceQueue.Forget(key)
}
return false
}
func (c *ClusterQuotaMappingController) namespaceWorker() {
for {
if quit := c.namespaceWork(); quit {
return
}
}
}
func (c *ClusterQuotaMappingController) waitForSyncedStores(ready chan<- struct{}, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
for !c.namespacesSynced() || !c.quotasSynced() {
glog.V(4).Infof("Waiting for the caches to sync before starting the quota mapping controller workers")
select {
case <-time.After(100 * time.Millisecond):
case <-stopCh:
return
}
}
close(ready)
}
func (c *ClusterQuotaMappingController) deleteNamespace(obj interface{}) {
ns, ok1 := obj.(*kapi.Namespace)
if !ok1 {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %v", obj))
return
}
ns, ok = tombstone.Obj.(*kapi.Namespace)
if !ok {
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a Namespace %v", obj))
return
}
}
c.clusterQuotaMapper.removeNamespace(ns.Name)
}
func (c *ClusterQuotaMappingController) addNamespace(cur interface{}) {
c.enqueueNamespace(cur)
}
func (c *ClusterQuotaMappingController) updateNamespace(old, cur interface{}) {
c.enqueueNamespace(cur)
}
func (c *ClusterQuotaMappingController) enqueueNamespace(obj interface{}) {
ns, ok := obj.(*kapi.Namespace)
if !ok {
utilruntime.HandleError(fmt.Errorf("not a Namespace %v", obj))
return
}
if !c.clusterQuotaMapper.requireNamespace(ns) {
return
}
key, err := controller.KeyFunc(ns)
if err != nil {
utilruntime.HandleError(err)
return
}
c.namespaceQueue.Add(key)
}
func (c *ClusterQuotaMappingController) deleteQuota(obj interface{}) {
quota, ok1 := obj.(*quotaapi.ClusterResourceQuota)
if !ok1 {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %v", obj))
return
}
quota, ok = tombstone.Obj.(*quotaapi.ClusterResourceQuota)
if !ok {
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a Quota %v", obj))
return
}
}
c.clusterQuotaMapper.removeQuota(quota.Name)
}
func (c *ClusterQuotaMappingController) addQuota(cur interface{}) {
c.enqueueQuota(cur)
}
func (c *ClusterQuotaMappingController) updateQuota(old, cur interface{}) {
c.enqueueQuota(cur)
}
func (c *ClusterQuotaMappingController) enqueueQuota(obj interface{}) {
quota, ok := obj.(*quotaapi.ClusterResourceQuota)
if !ok {
utilruntime.HandleError(fmt.Errorf("not a Quota %v", obj))
return
}
if !c.clusterQuotaMapper.requireQuota(quota) {
return
}
key, err := controller.KeyFunc(quota)
if err != nil {
utilruntime.HandleError(err)
return
}
c.quotaQueue.Add(key)
}