当前位置: 首页 > news >正文

k8s ThreadSafeStore原理

k8s ThreadSafeStore原理

image

定义

cache.Indexers
索引列表,指定从单个资源对象中获取索引值的方法IndexFunc
// IndexFunc knows how to compute the set of indexed values for an object.
// 个人觉得这里返回值用string就可以了,不需要[]string,因为索引值一般是单个
type IndexFunc func(obj interface{}) ([]string, error)
// Indexers maps a name to an IndexFunc
// 个人觉得Indexers和Indices可以合并,因为两者的key是一样的,例如map[string]struct{IndexFunc, index}
type Indexers map[string]IndexFunc
cache.NewIndexer
创建包含锁、指定从单个资源对象中获取key的方法keyFunc、索引列表的缓存对象
// Set是相同类型且不重复的元素列表,保存map中key
// Set is a set of the same type elements, implemented via map[comparable]struct{} for minimal memory consumption.
type Set[T comparable] map[T]Empty
// key是IndexFunc返回的索引值
// Index maps the indexed value to a set of keys in the store that match on that value
type index map[string]sets.Set[string]
// key是Indexers的key
// Indices maps a name to an Index
type Indices map[string]index
// storeIndex implements the indexing functionality for Store interface
type storeIndex struct {// indexers maps a name to an IndexFuncindexers Indexers// indices maps a name to an Indexindices Indices
}
// threadSafeMap implements ThreadSafeStore
type threadSafeMap struct {lock  sync.RWMutexitems map[string]interface{}// index implements the indexing functionalityindex *storeIndexrv    string// metrics is used to expose metrics about the store// and must be non-nil. If not provided, a noop implementation will be used.metrics *storeMetrics
}