1- # SharedInformer  
1+ # Informer  
22
33<!--  TOC --> 
44
5- -  [ SharedInformer ] ( #sharedinformer  ) 
5+ -  [ Informer ] ( #informer  ) 
66 -  [ processorListener] ( #processorlistener ) 
77 - [ add() 方法] ( #add-方法 ) 
88 - [ pop() 方法] ( #pop-方法 ) 
2121 -  [ HandleDeltas() 方法] ( #handledeltas-方法 ) 
2222 -  [ WaitForCacheSync() 函数] ( #waitforcachesync-函数 ) 
2323 -  [ codegen 为特定资源类型创建的 SharedIndexInformer] ( #codegen-为特定资源类型创建的-sharedindexinformer ) 
24-  -  [ SharedInformer 的使用场景] ( #sharedinformer-的使用场景 ) 
25-  -  [ GenericInformer 接口] ( #genericinformer-接口 ) 
2624
2725<!--  /TOC --> 
2826
27+ Inforer 的主要使用场景是自定义 Controller,它需要从 apiserver List/Watch 特定类型的资源对象的所有事件,缓存到内存,供后续快速查找,根据事件类型,调用用户注册的回调函数。
28+ 2929[ 前面分析了]  (3.listwatch-reflector-controller.md) NewInformer()、NewIndexInformer() 函数使用 controller 的 Reflector List/Watch 特定资源类型的对象,缓存到本地,并调用用户设置的 OnAdd/OnUpdate/OnDelete 回调函数(保存在 ResourceEventHandler 中)。 
3030
3131这两个函数返回的 Store 和 Index 都缓存了从 apiserver List/Watch 更新的资源类型对象,并保持与 etcd 同步。另外,可以使用 Index 创建 Lister,进而更方便的从本地缓存中 List 和 Get 符合条件的资源对象。
@@ -38,6 +38,8 @@ SharedInformer 提供一个共享的对象缓存,并且可以将缓存中对
3838
3939SharedInformer 和 SharedIndexInformer 一般和 workqueue 同时使用,具体参考:[ 8.customize-controller.md] ( 8.customize-controller.md ) 
4040
41+ 在分析 SharedInformer 和 SharedIndexInformer 之前,先分析它使用的 processorListener 和 sharedProcessor 结构类型。
42+ 4143## processorListener  
4244
4345processorListener 封装了监听器处理函数 ResourceEventHandler 以及 RingGrowing 类型的循环队列。
@@ -307,9 +309,9 @@ type SharedInformer interface {
307309	//  GetStore returns the Store.
308310	GetStore () Store 
309311	//  GetController gives back a synthetic interface that "votes" to start the informer
310- 	GetController ()  Controller 
311- 	// Run starts the shared informer, which will be stopped when stopCh is closed. 
312- 	Run (stopCh  <- chan  struct {})
312+ 	GetContrtarts  the shared informer, which will be stopped when stopCh is closed. 
313+ 	Run ( stopoller ()  Controller 
314+ 	// Run sCh  <-chan struct{})
313315	//  HasSynced returns true if the shared informer's store has synced.
314316	HasSynced () bool 
315317	//  LastSyncResourceVersion is the resource version observed when last synced with the underlying
@@ -355,6 +357,8 @@ func NewSharedIndexInformer(lw ListerWatcher, objType runtime.Object, defaultEve
355357
356358+  传给 NewSharedIndexInformer () 的 indexers 一般是 ` cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc} ` ,然后用 ` DeletionHandlingMetaNamespaceKeyFunc `  作为对象的 KeyFunc 创建 Indexer 缓存;
357359
360+ 后文会介绍,一般情况下,我们不需要使用 NewSharedInformer() 和 NewSharedIndexInformer() 函数为特定资源类型创建 SharedInformer,而是使用 codegen 为特定资源类型创建的 NewXXXInformer() 和 NewFilteredXXXInformer() 函数来创建。
361+ 358362### 实现 SharedIndexInformer 接口的 sharedIndexInformer 类型  
359363
360364这两个 Informer 包含:
@@ -773,6 +777,7 @@ func (f *deploymentInformer) Lister() v1.DeploymentLister {
773777	return  v1.NewDeploymentLister (f.Informer ().GetIndexer ())
774778}
775779``` 
780+ 776781+  client 一般是 client-go 的 kubernets 或 CRD 的 clientset,如:
777782
778783	``` go 
@@ -797,34 +802,3 @@ func (f *deploymentInformer) Lister() v1.DeploymentLister {
797802
798803一般情况下,我们不直接使用上面的 NewXXX 函数创建各资源类型的 SharedInformer,而是使用 codegen 生成的 sharedInformerFactory 来创建它们,具体参考:[ 6.sharedInformerFactory.md] ( 6.sharedInformerFactory.md ) 
799804
800- ### SharedInformer 的使用场景  
801- 802- SharedInforer 的主要使用场景是自定义 Controller,它需要从 apiserver List/Watch 特定类型的资源对象的所有事件,缓存到内存,供后续快速查找,根据事件类型,调用用户注册的回调函数。
803- 804- ## GenericInformer 接口  
805- 806- ```  go 
807- //  来源于 k8s.io/client-go/informers/generic.go
808- type  GenericInformer  interface  {
809- 	Informer () cache.SharedIndexInformer 
810- 	Lister () cache.GenericLister 
811- }
812- ``` 
813- 814- 内置类型 genericInformer 实现了该接口:
815- 816- ```  go 
817- type  genericInformer struct  {
818- 	informer cache.SharedIndexInformer 
819- 	resource schema.GroupResource 
820- }
821- //  Informer returns the SharedIndexInformer.
822- func  (f  *genericInformer ) Informer  () cache .SharedIndexInformer  {
823- 	return  f.informer 
824- }
825- 826- //  Lister returns the GenericLister.
827- func  (f  *genericInformer ) Lister  () cache .GenericLister  {
828- 	return  cache.NewGenericLister (f.Informer ().GetIndexer (), f.resource )
829- }
830- ``` 
0 commit comments