1
- # SharedInformer
1
+ # Informer
2
2
3
3
<!-- TOC -->
4
4
5
- - [ SharedInformer ] ( #sharedinformer )
5
+ - [ Informer ] ( #informer )
6
6
- [ processorListener] ( #processorlistener )
7
7
- [ add() 方法] ( #add-方法 )
8
8
- [ pop() 方法] ( #pop-方法 )
21
21
- [ HandleDeltas() 方法] ( #handledeltas-方法 )
22
22
- [ WaitForCacheSync() 函数] ( #waitforcachesync-函数 )
23
23
- [ codegen 为特定资源类型创建的 SharedIndexInformer] ( #codegen-为特定资源类型创建的-sharedindexinformer )
24
- - [ SharedInformer 的使用场景] ( #sharedinformer-的使用场景 )
25
- - [ GenericInformer 接口] ( #genericinformer-接口 )
26
24
27
25
<!-- /TOC -->
28
26
27
+ Inforer 的主要使用场景是自定义 Controller,它需要从 apiserver List/Watch 特定类型的资源对象的所有事件,缓存到内存,供后续快速查找,根据事件类型,调用用户注册的回调函数。
28
+
29
29
[ 前面分析了] (3.listwatch-reflector-controller.md) NewInformer()、NewIndexInformer() 函数使用 controller 的 Reflector List/Watch 特定资源类型的对象,缓存到本地,并调用用户设置的 OnAdd/OnUpdate/OnDelete 回调函数(保存在 ResourceEventHandler 中)。
30
30
31
31
这两个函数返回的 Store 和 Index 都缓存了从 apiserver List/Watch 更新的资源类型对象,并保持与 etcd 同步。另外,可以使用 Index 创建 Lister,进而更方便的从本地缓存中 List 和 Get 符合条件的资源对象。
@@ -38,6 +38,8 @@ SharedInformer 提供一个共享的对象缓存,并且可以将缓存中对
38
38
39
39
SharedInformer 和 SharedIndexInformer 一般和 workqueue 同时使用,具体参考:[ 8.customize-controller.md] ( 8.customize-controller.md )
40
40
41
+ 在分析 SharedInformer 和 SharedIndexInformer 之前,先分析它使用的 processorListener 和 sharedProcessor 结构类型。
42
+
41
43
## processorListener
42
44
43
45
processorListener 封装了监听器处理函数 ResourceEventHandler 以及 RingGrowing 类型的循环队列。
@@ -307,9 +309,9 @@ type SharedInformer interface {
307
309
// GetStore returns the Store.
308
310
GetStore () Store
309
311
// 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{})
313
315
// HasSynced returns true if the shared informer's store has synced.
314
316
HasSynced () bool
315
317
// LastSyncResourceVersion is the resource version observed when last synced with the underlying
@@ -355,6 +357,8 @@ func NewSharedIndexInformer(lw ListerWatcher, objType runtime.Object, defaultEve
355
357
356
358
+ 传给 NewSharedIndexInformer () 的 indexers 一般是 ` cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc} ` ,然后用 ` DeletionHandlingMetaNamespaceKeyFunc ` 作为对象的 KeyFunc 创建 Indexer 缓存;
357
359
360
+ 后文会介绍,一般情况下,我们不需要使用 NewSharedInformer() 和 NewSharedIndexInformer() 函数为特定资源类型创建 SharedInformer,而是使用 codegen 为特定资源类型创建的 NewXXXInformer() 和 NewFilteredXXXInformer() 函数来创建。
361
+
358
362
### 实现 SharedIndexInformer 接口的 sharedIndexInformer 类型
359
363
360
364
这两个 Informer 包含:
@@ -773,6 +777,7 @@ func (f *deploymentInformer) Lister() v1.DeploymentLister {
773
777
return v1.NewDeploymentLister (f.Informer ().GetIndexer ())
774
778
}
775
779
```
780
+
776
781
+ client 一般是 client-go 的 kubernets 或 CRD 的 clientset,如:
777
782
778
783
``` go
@@ -797,34 +802,3 @@ func (f *deploymentInformer) Lister() v1.DeploymentLister {
797
802
798
803
一般情况下,我们不直接使用上面的 NewXXX 函数创建各资源类型的 SharedInformer,而是使用 codegen 生成的 sharedInformerFactory 来创建它们,具体参考:[ 6.sharedInformerFactory.md] ( 6.sharedInformerFactory.md )
799
804
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