From d880f8c1ca3077496ceafadbb8807618b3f6e684 Mon Sep 17 00:00:00 2001 From: wojtekt Date: Wed, 23 Jan 2019 09:45:37 +0100 Subject: [PATCH] Couple fixes to DeltaFIFO machinery --- staging/src/k8s.io/client-go/tools/cache/controller.go | 6 +++--- staging/src/k8s.io/client-go/tools/cache/delta_fifo.go | 6 +++++- staging/src/k8s.io/client-go/tools/cache/delta_fifo_test.go | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/staging/src/k8s.io/client-go/tools/cache/controller.go b/staging/src/k8s.io/client-go/tools/cache/controller.go index 028c75e8e1..83f70f4d77 100644 --- a/staging/src/k8s.io/client-go/tools/cache/controller.go +++ b/staging/src/k8s.io/client-go/tools/cache/controller.go @@ -28,9 +28,9 @@ import ( // Config contains all the settings for a Controller. type Config struct { - // The queue for your objects; either a FIFO or - // a DeltaFIFO. Your Process() function should accept - // the output of this Queue's Pop() method. + // The queue for your objects - has to be a DeltaFIFO due to + // assumptions in the implementation. Your Process() function + // should accept the output of this Queue's Pop() method. Queue // Something that can list and watch your objects. diff --git a/staging/src/k8s.io/client-go/tools/cache/delta_fifo.go b/staging/src/k8s.io/client-go/tools/cache/delta_fifo.go index f818a293a6..f24eec2541 100644 --- a/staging/src/k8s.io/client-go/tools/cache/delta_fifo.go +++ b/staging/src/k8s.io/client-go/tools/cache/delta_fifo.go @@ -466,6 +466,7 @@ func (f *DeltaFIFO) Replace(list []interface{}, resourceVersion string) error { if f.knownObjects == nil { // Do deletion detection against our own list. + queuedDeletions := 0 for k, oldItem := range f.items { if keys.Has(k) { continue @@ -474,6 +475,7 @@ func (f *DeltaFIFO) Replace(list []interface{}, resourceVersion string) error { if n := oldItem.Newest(); n != nil { deletedObj = n.Object } + queuedDeletions++ if err := f.queueActionLocked(Deleted, DeletedFinalStateUnknown{k, deletedObj}); err != nil { return err } @@ -481,7 +483,9 @@ func (f *DeltaFIFO) Replace(list []interface{}, resourceVersion string) error { if !f.populated { f.populated = true - f.initialPopulationCount = len(list) + // While there shouldn't be any queued deletions in the initial + // population of the queue, it's better to be on the safe side. + f.initialPopulationCount = len(list) + queuedDeletions } return nil diff --git a/staging/src/k8s.io/client-go/tools/cache/delta_fifo_test.go b/staging/src/k8s.io/client-go/tools/cache/delta_fifo_test.go index 9f8e575804..afe0a5a48f 100644 --- a/staging/src/k8s.io/client-go/tools/cache/delta_fifo_test.go +++ b/staging/src/k8s.io/client-go/tools/cache/delta_fifo_test.go @@ -338,6 +338,7 @@ func TestDeltaFIFO_HasSyncedCorrectOnDeletion(t *testing.T) { // Since "bar" didn't have a delete event and wasn't in the Replace list // it should get a tombstone key with the right Obj. {{Deleted, DeletedFinalStateUnknown{Key: "bar", Obj: mkFifoObj("bar", 6)}}}, + {{Deleted, DeletedFinalStateUnknown{Key: "baz", Obj: mkFifoObj("baz", 7)}}}, } for _, expected := range expectedList { @@ -349,7 +350,7 @@ func TestDeltaFIFO_HasSyncedCorrectOnDeletion(t *testing.T) { t.Errorf("Expected %#v, got %#v", e, a) } } - if f.HasSynced() { + if !f.HasSynced() { t.Errorf("Expected HasSynced to be true") } }