// ListerWatcher 組合了 Lister 跟 Watcher 接下去看 這兩個interface負責什麼
typeListerWatcherinterface{ListerWatcher}// Lister is any object that knows how to perform an initial list.
typeListerinterface{// 根據 ListOptions 來決定要列出哪些物件
List(optionsmetav1.ListOptions)(runtime.Object,error)}// Watcher is any object that knows how to start a watch on a resource.
typeWatcherinterface{// 根據 ListOptions 來決定要跟蹤哪些物件
Watch(optionsmetav1.ListOptions)(watch.Interface,error)}
先打預防針不是每一個 ListerWatcher 都是透過以下方式實作的,本章節只討論 client go 範例的調用鏈,我猜其他實作的方式也差不多吧?(有時間再來研究其他的 listwatch 怎麼做) source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 定義實作 Lister 的 struct
typeListFuncfunc(optionsmetav1.ListOptions)(runtime.Object,error)// 定義實作 Watcher 的 struct
typeWatchFuncfunc(optionsmetav1.ListOptions)(watch.Interface,error)// ListWatch knows how to list and watch a set of apiserver resources. It satisfies the ListerWatcher interface.
// It is a convenience function for users of NewReflector, etc.
// ListFunc and WatchFunc must not be nil
typeListWatchstruct{ListFuncListFuncWatchFuncWatchFunc// DisableChunking requests no chunking for this list watcher.
DisableChunkingbool}
New Function
我們來看一下 cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", v1.NamespaceDefault, fields.Everything())這個 function 做了什麼 source code
// 委託給剛剛的 object 列出物件的情況
func(lw*ListWatch)List(optionsmetav1.ListOptions)(runtime.Object,error){// ListWatch is used in Reflector, which already supports pagination.
// Don't paginate here to avoid duplication.
returnlw.ListFunc(options)}