• K8s源码分析(25)-Store组件和Indexer组件


    上一篇文章里,我们主要介绍了和对象存储相关的组件 ThreadSafeStore 接口以及它的实现结构体 threadSafeMap,本质上来说该接口是并发安全的资源对象存储数据结构。在本篇文章里我们主要来介绍 Store 和 Indexer ,它们同样也是资源对象存储组件。

    Store 接口

    Store 是接口,图解和源码如下:

    423bfe84948a68d851f86cb16856aacd.png

    1. //k8s.io/client-go/tools/cache/store.go
    2. type Store interface {
    3. Add(obj interface{}) error
    4. Update(obj interface{}) error
    5. Delete(obj interface{}) error
    6. List() []interface{}
    7. ListKeys() []string
    8. Get(obj interface{}) (item interface{}, exists bool, err error)
    9. GetByKey(key string) (item interface{}, exists bool, err error)
    10. Replace([]interface{}, string) error
    11. Resync() error
    12. }
    • 该接口定义了对于资源增删改查的方法,例如 Add/Update/Delete/Get/List 等等。

    • 上一篇中介绍的 ThreadSafeStore 接口有所不同,这个接口的操作只是针对象本身,而 ThreadSafeStore 的操作则是针对对象的 key 和对象本身。就此来说 Store 是对 ThreadSafeStore 的更高一层的抽象,或者说 Store 的实现里一定有能力获取对象的 key,进而用 ThreadSafeStore 的能力来操作对象。

    Indexer 接口

    Indexer 是接口,图解和源码如下:

    4eb2b652fe0598c5f21b60962340bf26.png

    1. //k8s.io/client-go/tools/cache/store.go
    2. type Indexer interface {
    3. Store
    4. Index(indexName string, obj interface{}) ([]interface{}, error)
    5. IndexKeys(indexName, indexedValue string) ([]string, error)
    6. ListIndexFuncValues(indexName string) []string
    7. ByIndex(indexName, indexedValue string) ([]interface{}, error)
    8. GetIndexers() Indexers
    9. AddIndexers(newIndexers Indexers) error
    10. }
    • 该接口内部有上部分介绍的 Store 类型属性,所以是对于 Store 更高层的抽象定义。

    • 该接口还定义了和索引相关的方法, 例如 Index/IndexKeys 等等,所以对于该接口来说是在 Store 的基础上拥有索引的能力。

    cache 结构体

    cache 是一个结构体,该结构体实现了上面介绍的 Indexer 接口,其相关图解和源码如下:

    d32fb94be6b2e45c8eca1daeda9c5e80.png

    1. //k8s.io/client-go/tools/cache/store.go
    2. type KeyFunc func(obj interface{}) (string, error)
    3. type cache struct {
    4. cacheStorage ThreadSafeStore
    5. keyFunc KeyFunc
    6. }
    7. func (c *cache) Add(obj interface{}) error {
    8. key, err := c.keyFunc(obj)
    9. if err != nil {
    10. return KeyError{obj, err}
    11. }
    12. c.cacheStorage.Add(key, obj)
    13. return nil
    14. }
    15. func (c *cache) Update(obj interface{}) error
    16. func (c *cache) Delete(obj interface{}) error
    17. func (c *cache) List() []interface{}
    18. func (c *cache) ListKeys() []string
    19. func (c *cache) GetIndexers() Indexers
    20. func (c *cache) Index(indexName string, obj interface{}) ([]interface{}, error)
    21. func (c *cache) IndexKeys(indexName, indexKey string) ([]string, error)
    22. func (c *cache) ListIndexFuncValues(indexName string) []string
    23. func (c *cache) ByIndex(indexName, indexKey string) ([]interface{}, error)
    24. func (c *cache) AddIndexers(newIndexers Indexers) error
    25. .......
    • 结构体中有 cacheStore 属性,其类型为上一篇文章中介绍的 ThreadSafeStore 组件,说明该结构体也是并发安全的,并且借助 ThreadSafeStore 来实现资源的并发安全操作。

    • 结构体中有 KeyFunc 属性,主要用来计算资源对象的 key 值,然后利用上一篇文章中介绍的 ThreadSafeStore 组件来对资源操作。这样间接表明了 Store 接口和实现本质上是对 ThreadSafeStore 接口和实现的更高级抽象。

    • 结构体实现了 Indexer 接口中对于资源对象进行增删改查的一系列相关方法,例如 Add/Update/Delete/Get/List 等等。

    cache 结构体的创建

    cache 结构体创建的源码如下:

    1. //k8s.io/client-go/tools/cache/store.go
    2. func NewStore(keyFunc KeyFunc) Store {
    3. return &cache{
    4. cacheStorage: NewThreadSafeStore(Indexers{}, Indices{}),
    5. keyFunc: keyFunc,
    6. }
    7. }
    8. // NewIndexer returns an Indexer implemented simply with a map and a lock.
    9. func NewIndexer(keyFunc KeyFunc, indexers Indexers) Indexer {
    10. return &cache{
    11. cacheStorage: NewThreadSafeStore(indexers, Indices{}),
    12. keyFunc: keyFunc,
    13. }
    14. }
    • 结构体 cache 的创建必须有 KeyFunc 作为参数,其作用是用来生成资源对象的 key,从而内部操作资源对象。

    • 结构体 cache 的创建也可以接受以前文章中介绍的 Indexers 类型参数,本质上是用来创建内部的 ThreadSafeStore 组件。

    • NewStore 方法和 NewIndexer 方法虽然实现上都是去创建上面介绍的 cache 类型对象,但本质上 NewStore 是返回 Store 类型来提供存储能力,而 NewIndexer 是返回 Indexer 类型来提供索引能力。

    目前我们先写到这里,在下一篇文章中我们继续介绍 Queue 组件以及 DeltaFIFO 组件。

  • 相关阅读:
    C/C++蓝桥杯之杨辉三角
    【服务器搭建】教程二:快速搭建我们服务器 进来看
    机器故障预测:未来24小时的决胜时刻!!!
    概率论与数据统计学习:随机变量(一)——知识总结与C语言案例实现
    Git-分布式版本控制工具教程
    微前端的初探索
    &4_单机优化(确定性算法,优化框架)
    STM32_GPIO
    现代 PHP 新特性 —— 内置的 HTTP 服务器
    近期的感想与2024年的计划
  • 原文地址:https://blog.csdn.net/weixin_46073333/article/details/126552779