@@ -3,6 +3,7 @@ package queueinformer
33import (
44 "context"
55 "fmt"
6+ "sigs.k8s.io/controller-runtime/pkg/client"
67 "sync"
78 "time"
89
@@ -270,41 +271,53 @@ func (o *operator) processNextWorkItem(ctx context.Context, loop *QueueInformer)
270271 }
271272 defer queue .Done (item )
272273
274+ // As per the 'validateQueueInformer' function in config.go in this package
275+ // a valid queue informer must have either an indexer or an informer defined.
276+ // As per the 'complete' function in the same file, if an indexer is not defined
277+ // the informer indexer will be used.
278+ // NewQueueInformer calls complete after applying the options. Therefore,
279+ // an indexer should always be present.
280+ if loop .indexer == nil {
281+ panic ("invalid queueinformer: no indexer defined" )
282+ }
283+
273284 logger := o .logger .WithField ("item" , item )
274285 logger .WithField ("queue-length" , queue .Len ()).Trace ("popped queue" )
275286
276287 var event = item
288+
277289 if item .Type () != kubestate .ResourceDeleted {
278- // Get the key
279- key , keyable := loop .key (item )
280- if ! keyable {
281- logger .WithField ("item" , item ).Warn ("could not form key" )
282- queue .Forget (item )
283- return true
290+ // Get the key - add/update resource operations must always contain a string resource
291+ key , ok := item .Resource ().(string )
292+ if ! ok {
293+ panic (fmt .Sprintf ("unexpected item resource type: %T for add/update operation" , item .Resource ()))
284294 }
285295
286296 logger = logger .WithField ("cache-key" , key )
287297
288298 var resource interface {}
289- if loop .indexer == nil {
290- resource = item .Resource ()
291- } else {
292- // Get the current cached version of the resource
293- var exists bool
294- var err error
295- resource , exists , err = loop .indexer .GetByKey (key )
296- if err != nil {
297- logger .WithError (err ).Error ("cache get failed" )
298- queue .Forget (item )
299- return true
300- }
301- if ! exists {
302- logger .WithField ("existing-cache-keys" , loop .indexer .ListKeys ()).Debug ("cache get failed, key not in cache" )
303- queue .Forget (item )
304- return true
305- }
299+
300+ // Get the current cached version of the resource
301+ resource , exists , err := loop .indexer .GetByKey (key )
302+ if err != nil {
303+ logger .WithError (err ).Error ("cache get failed" )
304+ queue .Forget (item )
305+ return true
306+ }
307+ if ! exists {
308+ logger .WithField ("existing-cache-keys" , loop .indexer .ListKeys ()).Debug ("cache get failed, key not in cache" )
309+ queue .Forget (item )
310+ return true
306311 }
312+
307313 event = kubestate .NewResourceEvent (item .Type (), resource )
314+ } else {
315+ switch item .Resource ().(type ) {
316+ case client.Object :
317+ case cache.DeletedFinalStateUnknown :
318+ default :
319+ panic (fmt .Sprintf ("unexpected item resource type: %T for delete operation" , item .Resource ()))
320+ }
308321 }
309322
310323 // Sync and requeue on error (throw out failed deletion syncs)
0 commit comments