Table of Contents
1. 背景
tool/cache.indexer是informer中提供本地缓存,并且带有丰富索引的机制。
index是索引的实现。类似于数据库的索引一样,index可以加快查找速度。
本节就是弄清楚cache中indexer是如何实现的
本节研究的内容位置整个informer机制的红色圈起来区域如何存储+如何索引
2. Indexer结构说明
Indexer是一个接口,包含两个部分:
(1)Store。从Store定义来看,Store是真正保存数据的结构体。Store本身也是一个接口,具体的存储需要实现这些接口。
(2)Index,IndexKeys,ListIndexFuncValues,ByIndex,GetIndexers,AddIndexers 等和操作索引有关的函数
// IndexFunc knows how to provide an indexed value for an object. type IndexFunc func(obj interface{}) ([]string, error) // Index maps the indexed value to a set of keys in the store that match on that value type Index map[string]sets.String // Indexers maps a name to a IndexFunc type Indexers map[string]IndexFunc // Indices maps a name to an Index type Indices map[string]Index // Indexer接口是为了添加或者查询索引用的。当前可能一下子看注释很迷惑,先看看后面的例子就清楚了 type Indexer interface { Store // 通过indexName获得索引函数,然后obj(pod)对象作为函数输入,输出所有检索值。然后找出来所有包含检索值的对象(pod) // 举例pod1 通过byuser这个函数,检索出来有ernie,bert两个检索值 // 然后Index("byuser",pod1) 会输出pod1, pod2(包含bert),pod3(包含ernie) // Retrieve list of objects that match on the named indexing function Index(indexName string, obj interface{}) ([]interface{}, error) // 通过索引函数的名字(byUser)+具体的值(bert),获得pod的名字(ns/podName) // IndexKeys returns the set of keys that match on the named indexing function. IndexKeys(indexName, indexKey string) ([]string, error) // 通过索引函数的名字(byUser), 获得所有的索引值。这里输入byuser, 输出:ernie, bert, elmo, oscar // ListIndexFuncValues returns the list of generated values of an Index func ListIndexFuncValues(indexName string) []string // 通过索引函数的名字(byUser)+具体的值(bert),获得pod对象 // ByIndex lists object that match on the named indexing function with the exact key ByIndex(indexName, indexKey string) ([]interface{}, error) // 返回所有的索引函数 // GetIndexer return the indexers GetIndexers() Indexers // AddIndexers adds more indexers to this store. If you call this after you already have data // in the sto