Skip to content

Commit fba3c54

Browse files
authored
Merge pull request kubernetes#70803 from Adirio/controller-cleanup
Remove duplicate code in client-go/tools/cache/controller.go
2 parents 780192a + b09c1ce commit fba3c54

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

staging/src/k8s.io/client-go/tools/cache/controller.go

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -285,45 +285,7 @@ func NewInformer(
285285
// This will hold the client state, as we know it.
286286
clientState := NewStore(DeletionHandlingMetaNamespaceKeyFunc)
287287

288-
// This will hold incoming changes. Note how we pass clientState in as a
289-
// KeyLister, that way resync operations will result in the correct set
290-
// of update/delete deltas.
291-
fifo := NewDeltaFIFO(MetaNamespaceKeyFunc, clientState)
292-
293-
cfg := &Config{
294-
Queue: fifo,
295-
ListerWatcher: lw,
296-
ObjectType: objType,
297-
FullResyncPeriod: resyncPeriod,
298-
RetryOnError: false,
299-
300-
Process: func(obj interface{}) error {
301-
// from oldest to newest
302-
for _, d := range obj.(Deltas) {
303-
switch d.Type {
304-
case Sync, Added, Updated:
305-
if old, exists, err := clientState.Get(d.Object); err == nil && exists {
306-
if err := clientState.Update(d.Object); err != nil {
307-
return err
308-
}
309-
h.OnUpdate(old, d.Object)
310-
} else {
311-
if err := clientState.Add(d.Object); err != nil {
312-
return err
313-
}
314-
h.OnAdd(d.Object)
315-
}
316-
case Deleted:
317-
if err := clientState.Delete(d.Object); err != nil {
318-
return err
319-
}
320-
h.OnDelete(d.Object)
321-
}
322-
}
323-
return nil
324-
},
325-
}
326-
return clientState, New(cfg)
288+
return clientState, newInformer(lw, objType, resyncPeriod, h, clientState)
327289
}
328290

329291
// NewIndexerInformer returns a Indexer and a controller for populating the index
@@ -352,6 +314,30 @@ func NewIndexerInformer(
352314
// This will hold the client state, as we know it.
353315
clientState := NewIndexer(DeletionHandlingMetaNamespaceKeyFunc, indexers)
354316

317+
return clientState, newInformer(lw, objType, resyncPeriod, h, clientState)
318+
}
319+
320+
// newInformer returns a controller for populating the store while also
321+
// providing event notifications.
322+
//
323+
// Parameters
324+
// * lw is list and watch functions for the source of the resource you want to
325+
// be informed of.
326+
// * objType is an object of the type that you expect to receive.
327+
// * resyncPeriod: if non-zero, will re-list this often (you will get OnUpdate
328+
// calls, even if nothing changed). Otherwise, re-list will be delayed as
329+
// long as possible (until the upstream source closes the watch or times out,
330+
// or you stop the controller).
331+
// * h is the object you want notifications sent to.
332+
// * clientState is the store you want to populate
333+
//
334+
func newInformer(
335+
lw ListerWatcher,
336+
objType runtime.Object,
337+
resyncPeriod time.Duration,
338+
h ResourceEventHandler,
339+
clientState Store,
340+
) Controller {
355341
// This will hold incoming changes. Note how we pass clientState in as a
356342
// KeyLister, that way resync operations will result in the correct set
357343
// of update/delete deltas.
@@ -390,5 +376,5 @@ func NewIndexerInformer(
390376
return nil
391377
},
392378
}
393-
return clientState, New(cfg)
379+
return New(cfg)
394380
}

0 commit comments

Comments
 (0)