controller数据结构分析,从上往下看
type BeeController struct { queue *controller.QueueWorker // Handles messages controller *BeeControllerImpl Name string BeforeReconcile func(key string) AfterReconcile func(key string, err error) Informers *sharedinformers.SharedInformers }
controller中的Informers就是SharedInformers,而controller就是BeeControllerImpl结构。
type SharedInformers struct {
controller.SharedInformersDefaults
Factory externalversions.SharedInformerFactory
}
type BeeControllerImpl struct { builders.DefaultControllerFns // lister indexes properties about Bee lister listers.BeeLister }
SharedInformers中的Factory中就是sharedInformerFactory
type sharedInformerFactory struct { client clientset.Interface namespace string tweakListOptions internalinterfaces.TweakListOptionsFunc lock sync.Mutex defaultResync time.Duration informers map[reflect.Type]cache.SharedIndexInformer // startedInformers is used for tracking which informers have been started. // This allows Start() to be called multiple times safely. startedInformers map[reflect.Type]bool }
func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer { f.lock.Lock() defer f.lock.Unlock() informerType := reflect.TypeOf(obj) informer, exists := f.informers[informerType] if exists { return informer } informer = newFunc(f.client, f.defaultResync) f.informers[informerType] = informer return informer }
其存在InformerFor成员函数,用于informers中添加shareInformerIndexerFactory(),通过informer()函数获取。shareInformerIndexerFactory拥有addeventhandler来添加具体的回调函数。
举例:Informer()生成对应的shareInformerIndexerFactory() 从下往上看
func NewFilteredBeeInformer(client clientset.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer { return cache.NewSharedIndexInformer( &cache.ListWatch{ ListFunc: func(options v1.ListOptions) (runtime.Object, error) { if tweakListOptions != nil { tweakListOptions(&options) } return client.AlphaV1alpha1().Bees(namespace).List(options) }, WatchFunc: func(options v1.ListOptions) (watch.Interface, error) { if tweakListOptions != nil { tweakListOptions(&options) } return client.AlphaV1alpha1().Bees(namespace).Watch(options) }, }, &alpha_v1alpha1.Bee{}, resyncPeriod, indexers, ) } func (f *beeInformer) defaultInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { return NewFilteredBeeInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) } func (f *beeInformer) Informer() cache.SharedIndexInformer { return f.factory.InformerFor(&alpha_v1alpha1.Bee{}, f.defaultInformer) }