Skip to content

Instantly share code, notes, and snippets.

@sub-mod
Forked from songbinliu/SharedInformer.md
Created December 29, 2019 23:46
Show Gist options
  • Save sub-mod/f61e0f63eb6d332430421f8318452221 to your computer and use it in GitHub Desktop.
Save sub-mod/f61e0f63eb6d332430421f8318452221 to your computer and use it in GitHub Desktop.
Notes about Kubernetes SharedInformer

Kubernetes SharedInformer

Overview

SharedInformer has a shared data cache, and is able to distribute notifications for changes to the cache to multiple listeners who have registered to it. SharedInformer is also a "Producer-Consumer" framework, using Controller as Producer to produce the Events (, or changes to the cache).

Definition of the interface

definiton.of.interface

The interface SharedInformer enables users to provide customer ResourceEventHandler via the AddEventHandler function. The concrete implementation of this interface sharedIndexInformer, provides a way to better understand this interface.

Implementation Details

Definition of sharedIndexInformer

defintion2
This struct has two main components:
  • Controller : which is the producer. Indexer, ListerWatcher, and objectType are all for it.
  • sharedProcessor :
    • manage the customer Listeners (or EventHandlers);
    • distribute Events to the Listeners.

CacheMutationDetector is not necessary from logical, because it just checked whether the cached content has beed modified illegally. If it detects some illegal modifition, it will just panic.

Logical of Running

informer.run()

When sharedIndexInformer starts to run, it will first creates a Controller, and start the Controller. It will also start the sharedProcessor. It is should be noted that HandleDeltas function, which is used by the Controller, will call sharedProcessor.distribute().

distribute

sharedProcessor and Listeners

sharedProcessor and Listener

The struct of sharedProcessor is simple, its main content is an array of Listeners(processorListeners). The struct of processorListener is more complicated than the sharedProcessor. The field pendingNotifications are the Events received from sharedProcessor.distribute(); and the field ResourceEventHandler is provided by users, and is to consume the Events.

addlistenerevent

sharedProcessor manages the Listeners via function addListener(), and distribute the Events via function distribute().

Add Listener

sharedInformer.AddEventHandler()

User can register their ResourceEventHandler at any time, before the sharedInformer is running, or while it is running. sharedInformer will then generate a Listener with that ResourceEventHandler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment