熟悉Kubernetes的小夥伴們想必會有一窺源碼的動機,領略大師的設計和實作,從官方開放出來的go-sdk來切入Kubernetes應該是個不錯的選擇。在client-go的工具包裡面,informer是一個主要的工具,下文是小編對inform元件的主要功能和部分代碼的簡單介紹,若有錯誤之處,歡迎指出,小編果然獻出鐵頭,供大家輕拍。
主要功能:
- List和Watch方法,從APIServer同步/解碼對象到本地緩存,另外提供本地緩存的通路入口,減少對APIServer的通路壓力。
- AddEventHandler方法,handler注冊的入口,使用者變更對象之後觸發handler對應的函數,将對象狀态資訊寫到workqueue,供worker消費。
邏輯關系圖(引用徐超大神的分享):
代碼結構淺析:
sharedInformerFactory
- infomers成員:維護Type接口到SharedIndexInformer的map,此處映射到sharedIndexInformer struct指針變量,工廠設計模式。
- startedInformers成員:維護Type接口到BOOL值的映射,啟動informer之前在此判斷,避免重複啟動infromer。
- Start方法:疊代informers成員,調用informer.run方法啟動informer。
- InformerFor方法:為informers成員持續增加entry。
- ...
deploymentInformer
- factory成員:實作SharedInformerFactory接口的結構變量,此處指派為sharedInformerFactory struct指針變量。
- Informer方法:調用factory的InformerFor方法,傳回sharedIndexInformer指針對象。
- Lister方法:傳回informer的indexer成員,用以讀取本地緩存。
- ...
sharedIndexInformer
- indexer成員:實作Indexer接口,維護本地緩存(map[string]interface{}),此處指派為cache struct指針變量。
- controller成員:實作Controller接口,維護APIServer到本地緩存的同步機制,同時保持與processor的通訊,此處指派為controller struct指針對象。
- processor成員:保持與controller的通訊,觸發handler對應的方法,此處指派為sharedProcessor struct 指針對象。
- AddEventHandler方法:建立processorListener struct值對象,append到processor成員的listeners和syncinglisteners。
- HandleDelta方法:疊代controller.config.Queue.items[key](Delta切片),調用indexer.Add/Delete/Update更新本地緩存,同時調用processor.distribute方法,按需疊代processor的listeners和syncinglisteners,下發消息到listener的消息隊列。
- ...
indexer
- cacheStorage成員:實作ThreadSafeStore接口,維護本地線程安全的緩存map[string]interface{},此處指派為threadSafeMap struct指針變量。
- keyFunc成員:維護本地緩存key的函數。
- Add/Delete/Update/List方法:支撐對本地緩存的常用操作。
- ...
controller
- reflector成員:利用resourceVersion機制銜接調用listerWatcher.List/Watch方法,從APIServer同步/解碼對象到config.Queue.items(map[string]Deltas)中,此處指派為Reflect struct指針對象。
- config成員:維護鍊隊列和對象操作映射(map[string]Deltas),此處指派為Config struct值對象。
- processloop方法:傳入config.Process成員,循環調用config.Queue.Pop方法。
- ...
processor
- listeners成員:processorListener struct指針變量的切片。
- run方法:疊代listeners,調用processorListener.run/pop方法,觸發processorListener.handler.OnAdd/OnUpdate/OnDelete方法。
- ...
reflector
- store成員:實作Store接口,此處指派為DeltaFIFO struct指針對象。
- listerWatcher成員:實作ListerWatcher的List/Watch方法,從APIServer同步/解碼對象,此處指派為ListWatch struct指針變量。
- ListAndWatch方法:調用listerWatcher成員的List/Watch方法,完成對象從APIServer的同步/解碼,然後調用syncWith和watchHandler方法,把事件和對象同步到store的存儲成員中。
- syncWith方法:調用Store接口的Replace方法,同步事件和對象資訊。
- watchHandler方法:調用Store接口的Add/Update/Delete方法,同步事件和對象資訊。
- ...
config
- Queue成員:實作Queue接口,此處指派為DeltaFIFO struct指針對象。
- Process成員:值為sharedIndexInformer.HandleDelta方法。
- ...
DeltaFIFO
- items成員:map[string]Deltas,存儲對象和操作資訊。
- queue成員:[]string,簡單的鍊隊列。
- knownObjects成員:實作KeyListerGetter接口,此處指派為cache struct指針對象。
- Pop方法:從queue成員pop,調用config.Process方法。
- Add/Delete/Update/Replace方法:調用queueActionLocked方法。
- queueActionLocked方法:維護items和queue成員。
- ...
processorListener
- nextCh成員:從本地pendingNotifications或者addCh生産消息,run方法消費消息。
- addCh成員:從調用add方法生産消息,寫入本地pendingNotifications或者傳遞給nextCh。
- pendingNotifications成員:維護本地緩沖消息,此處指派為RingGrowing struct值對象。
- pop方法:維護nextCh和addCh的消息傳遞。
- run方法:消費nextCh消息,按需調用processorListener.OnAdd/OnUpdate/OnDelete方法。
- ...
總結: