@@ -140,12 +140,9 @@ func NewBaseController(rsInformer appsinformers.ReplicaSetInformer, podInformer
140
140
}
141
141
142
142
rsInformer .Informer ().AddEventHandler (cache.ResourceEventHandlerFuncs {
143
- AddFunc : rsc .enqueueReplicaSet ,
143
+ AddFunc : rsc .addRS ,
144
144
UpdateFunc : rsc .updateRS ,
145
- // This will enter the sync loop and no-op, because the replica set has been deleted from the store.
146
- // Note that deleting a replica set immediately after scaling it to 0 will not work. The recommended
147
- // way of achieving this is by performing a `stop` operation on the replica set.
148
- DeleteFunc : rsc .enqueueReplicaSet ,
145
+ DeleteFunc : rsc .deleteRS ,
149
146
})
150
147
rsc .rsLister = rsInformer .Lister ()
151
148
rsc .rsListerSynced = rsInformer .Informer ().HasSynced
@@ -266,11 +263,50 @@ func (rsc *ReplicaSetController) resolveControllerRef(namespace string, controll
266
263
return rs
267
264
}
268
265
266
+ func (rsc * ReplicaSetController ) enqueueRS (rs * apps.ReplicaSet ) {
267
+ key , err := controller .KeyFunc (rs )
268
+ if err != nil {
269
+ utilruntime .HandleError (fmt .Errorf ("couldn't get key for object %#v: %v" , rs , err ))
270
+ return
271
+ }
272
+
273
+ rsc .queue .Add (key )
274
+ }
275
+
276
+ func (rsc * ReplicaSetController ) enqueueRSAfter (rs * apps.ReplicaSet , duration time.Duration ) {
277
+ key , err := controller .KeyFunc (rs )
278
+ if err != nil {
279
+ utilruntime .HandleError (fmt .Errorf ("couldn't get key for object %#v: %v" , rs , err ))
280
+ return
281
+ }
282
+
283
+ rsc .queue .AddAfter (key , duration )
284
+ }
285
+
286
+ func (rsc * ReplicaSetController ) addRS (obj interface {}) {
287
+ rs := obj .(* apps.ReplicaSet )
288
+ klog .V (4 ).Infof ("Adding %s %s/%s" , rsc .Kind , rs .Namespace , rs .Name )
289
+ rsc .enqueueRS (rs )
290
+ }
291
+
269
292
// callback when RS is updated
270
293
func (rsc * ReplicaSetController ) updateRS (old , cur interface {}) {
271
294
oldRS := old .(* apps.ReplicaSet )
272
295
curRS := cur .(* apps.ReplicaSet )
273
296
297
+ // TODO: make a KEP and fix informers to always call the delete event handler on re-create
298
+ if curRS .UID != oldRS .UID {
299
+ key , err := controller .KeyFunc (oldRS )
300
+ if err != nil {
301
+ utilruntime .HandleError (fmt .Errorf ("couldn't get key for object %#v: %v" , oldRS , err ))
302
+ return
303
+ }
304
+ rsc .deleteRS (cache.DeletedFinalStateUnknown {
305
+ Key : key ,
306
+ Obj : oldRS ,
307
+ })
308
+ }
309
+
274
310
// You might imagine that we only really need to enqueue the
275
311
// replica set when Spec changes, but it is safer to sync any
276
312
// time this function is triggered. That way a full informer
@@ -286,7 +322,36 @@ func (rsc *ReplicaSetController) updateRS(old, cur interface{}) {
286
322
if * (oldRS .Spec .Replicas ) != * (curRS .Spec .Replicas ) {
287
323
klog .V (4 ).Infof ("%v %v updated. Desired pod count change: %d->%d" , rsc .Kind , curRS .Name , * (oldRS .Spec .Replicas ), * (curRS .Spec .Replicas ))
288
324
}
289
- rsc .enqueueReplicaSet (cur )
325
+ rsc .enqueueRS (curRS )
326
+ }
327
+
328
+ func (rsc * ReplicaSetController ) deleteRS (obj interface {}) {
329
+ rs , ok := obj .(* apps.ReplicaSet )
330
+ if ! ok {
331
+ tombstone , ok := obj .(cache.DeletedFinalStateUnknown )
332
+ if ! ok {
333
+ utilruntime .HandleError (fmt .Errorf ("couldn't get object from tombstone %#v" , obj ))
334
+ return
335
+ }
336
+ rs , ok = tombstone .Obj .(* apps.ReplicaSet )
337
+ if ! ok {
338
+ utilruntime .HandleError (fmt .Errorf ("tombstone contained object that is not a ReplicaSet %#v" , obj ))
339
+ return
340
+ }
341
+ }
342
+
343
+ key , err := controller .KeyFunc (rs )
344
+ if err != nil {
345
+ utilruntime .HandleError (fmt .Errorf ("couldn't get key for object %#v: %v" , rs , err ))
346
+ return
347
+ }
348
+
349
+ klog .V (4 ).Infof ("Deleting %s %q" , rsc .Kind , key )
350
+
351
+ // Delete expectations for the ReplicaSet so if we create a new one with the same name it starts clean
352
+ rsc .expectations .DeleteExpectations (key )
353
+
354
+ rsc .queue .Add (key )
290
355
}
291
356
292
357
// When a pod is created, enqueue the replica set that manages it and update its expectations.
@@ -312,7 +377,7 @@ func (rsc *ReplicaSetController) addPod(obj interface{}) {
312
377
}
313
378
klog .V (4 ).Infof ("Pod %s created: %#v." , pod .Name , pod )
314
379
rsc .expectations .CreationObserved (rsKey )
315
- rsc .enqueueReplicaSet ( rs )
380
+ rsc .queue . Add ( rsKey )
316
381
return
317
382
}
318
383
@@ -326,7 +391,7 @@ func (rsc *ReplicaSetController) addPod(obj interface{}) {
326
391
}
327
392
klog .V (4 ).Infof ("Orphan Pod %s created: %#v." , pod .Name , pod )
328
393
for _ , rs := range rss {
329
- rsc .enqueueReplicaSet (rs )
394
+ rsc .enqueueRS (rs )
330
395
}
331
396
}
332
397
@@ -363,7 +428,7 @@ func (rsc *ReplicaSetController) updatePod(old, cur interface{}) {
363
428
if controllerRefChanged && oldControllerRef != nil {
364
429
// The ControllerRef was changed. Sync the old controller, if any.
365
430
if rs := rsc .resolveControllerRef (oldPod .Namespace , oldControllerRef ); rs != nil {
366
- rsc .enqueueReplicaSet (rs )
431
+ rsc .enqueueRS (rs )
367
432
}
368
433
}
369
434
@@ -374,7 +439,7 @@ func (rsc *ReplicaSetController) updatePod(old, cur interface{}) {
374
439
return
375
440
}
376
441
klog .V (4 ).Infof ("Pod %s updated, objectMeta %+v -> %+v." , curPod .Name , oldPod .ObjectMeta , curPod .ObjectMeta )
377
- rsc .enqueueReplicaSet (rs )
442
+ rsc .enqueueRS (rs )
378
443
// TODO: MinReadySeconds in the Pod will generate an Available condition to be added in
379
444
// the Pod status which in turn will trigger a requeue of the owning replica set thus
380
445
// having its status updated with the newly available replica. For now, we can fake the
@@ -386,7 +451,7 @@ func (rsc *ReplicaSetController) updatePod(old, cur interface{}) {
386
451
klog .V (2 ).Infof ("%v %q will be enqueued after %ds for availability check" , rsc .Kind , rs .Name , rs .Spec .MinReadySeconds )
387
452
// Add a second to avoid milliseconds skew in AddAfter.
388
453
// See https://github.com/kubernetes/kubernetes/issues/39785#issuecomment-279959133 for more info.
389
- rsc .enqueueReplicaSetAfter (rs , (time .Duration (rs .Spec .MinReadySeconds )* time .Second )+ time .Second )
454
+ rsc .enqueueRSAfter (rs , (time .Duration (rs .Spec .MinReadySeconds )* time .Second )+ time .Second )
390
455
}
391
456
return
392
457
}
@@ -400,7 +465,7 @@ func (rsc *ReplicaSetController) updatePod(old, cur interface{}) {
400
465
}
401
466
klog .V (4 ).Infof ("Orphan Pod %s updated, objectMeta %+v -> %+v." , curPod .Name , oldPod .ObjectMeta , curPod .ObjectMeta )
402
467
for _ , rs := range rss {
403
- rsc .enqueueReplicaSet (rs )
468
+ rsc .enqueueRS (rs )
404
469
}
405
470
}
406
471
}
@@ -438,31 +503,12 @@ func (rsc *ReplicaSetController) deletePod(obj interface{}) {
438
503
}
439
504
rsKey , err := controller .KeyFunc (rs )
440
505
if err != nil {
506
+ utilruntime .HandleError (fmt .Errorf ("couldn't get key for object %#v: %v" , rs , err ))
441
507
return
442
508
}
443
509
klog .V (4 ).Infof ("Pod %s/%s deleted through %v, timestamp %+v: %#v." , pod .Namespace , pod .Name , utilruntime .GetCaller (), pod .DeletionTimestamp , pod )
444
510
rsc .expectations .DeletionObserved (rsKey , controller .PodKey (pod ))
445
- rsc .enqueueReplicaSet (rs )
446
- }
447
-
448
- // obj could be an *apps.ReplicaSet, or a DeletionFinalStateUnknown marker item.
449
- func (rsc * ReplicaSetController ) enqueueReplicaSet (obj interface {}) {
450
- key , err := controller .KeyFunc (obj )
451
- if err != nil {
452
- utilruntime .HandleError (fmt .Errorf ("couldn't get key for object %+v: %v" , obj , err ))
453
- return
454
- }
455
- rsc .queue .Add (key )
456
- }
457
-
458
- // obj could be an *apps.ReplicaSet, or a DeletionFinalStateUnknown marker item.
459
- func (rsc * ReplicaSetController ) enqueueReplicaSetAfter (obj interface {}, after time.Duration ) {
460
- key , err := controller .KeyFunc (obj )
461
- if err != nil {
462
- utilruntime .HandleError (fmt .Errorf ("couldn't get key for object %+v: %v" , obj , err ))
463
- return
464
- }
465
- rsc .queue .AddAfter (key , after )
511
+ rsc .queue .Add (rsKey )
466
512
}
467
513
468
514
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
@@ -485,7 +531,7 @@ func (rsc *ReplicaSetController) processNextWorkItem() bool {
485
531
return true
486
532
}
487
533
488
- utilruntime .HandleError (fmt .Errorf ("Sync %q failed with %v" , key , err ))
534
+ utilruntime .HandleError (fmt .Errorf ("sync %q failed with %v" , key , err ))
489
535
rsc .queue .AddRateLimited (key )
490
536
491
537
return true
@@ -498,7 +544,7 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *apps
498
544
diff := len (filteredPods ) - int (* (rs .Spec .Replicas ))
499
545
rsKey , err := controller .KeyFunc (rs )
500
546
if err != nil {
501
- utilruntime .HandleError (fmt .Errorf ("Couldn 't get key for %v %#v: %v" , rsc .Kind , rs , err ))
547
+ utilruntime .HandleError (fmt .Errorf ("couldn 't get key for %v %#v: %v" , rsc .Kind , rs , err ))
502
548
return nil
503
549
}
504
550
if diff < 0 {
@@ -608,7 +654,6 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *apps
608
654
// meaning it did not expect to see any more of its pods created or deleted. This function is not meant to be
609
655
// invoked concurrently with the same key.
610
656
func (rsc * ReplicaSetController ) syncReplicaSet (key string ) error {
611
-
612
657
startTime := time .Now ()
613
658
defer func () {
614
659
klog .V (4 ).Infof ("Finished syncing %v %q (%v)" , rsc .Kind , key , time .Since (startTime ))
@@ -631,7 +676,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(key string) error {
631
676
rsNeedsSync := rsc .expectations .SatisfiedExpectations (key )
632
677
selector , err := metav1 .LabelSelectorAsSelector (rs .Spec .Selector )
633
678
if err != nil {
634
- utilruntime .HandleError (fmt .Errorf ("Error converting pod selector to selector: %v" , err ))
679
+ utilruntime .HandleError (fmt .Errorf ("error converting pod selector to selector: %v" , err ))
635
680
return nil
636
681
}
637
682
@@ -670,7 +715,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(key string) error {
670
715
if manageReplicasErr == nil && updatedRS .Spec .MinReadySeconds > 0 &&
671
716
updatedRS .Status .ReadyReplicas == * (updatedRS .Spec .Replicas ) &&
672
717
updatedRS .Status .AvailableReplicas != * (updatedRS .Spec .Replicas ) {
673
- rsc .enqueueReplicaSetAfter ( updatedRS , time .Duration (updatedRS .Spec .MinReadySeconds )* time .Second )
718
+ rsc .queue . AddAfter ( key , time .Duration (updatedRS .Spec .MinReadySeconds )* time .Second )
674
719
}
675
720
return manageReplicasErr
676
721
}
0 commit comments