client-go源码分析--Indexer, cache, threadSafeMap

前言

本文主要聚焦client-go controller框架的Indexer对象,分析Indexer的实现。

1 cache - indexer对象关系

golang的struct和interface之间是弱耦合关系,即struct只需要实现了某个interface的所有方法,就认为该struct实现了该interface。cache和Store对象关系如下:

从类的实现、组合关系上可见,
cache实现了Store interface和Index interface的所有方法,因此cache可以赋值给Store和Index接口。理解threadSafeMap的实现能帮助我们理解cache。

client-go中的很多实现封装都非常规范,index.go中给出了索引相关的操作(接口);store.go中给出了与操作存储相关的接口,并提供了一个cache实现,当然也可以实现自行实现Store接口;thread_safe_store.go为cache的私有实现。client-go的indexer实际操作的还是threadSafeMap中的方法和数据,调用关系如下:

2 理解threadSafeMap的实现

几个内部索引对象及其关系

type IndexFunc func(obj interface) ([]string, error)

type Indexers map[string]indexFunc

type Index map[string]sets.String

type indices map[string]Index

从上述几个对象的命名和关系看,很难理解几个对象的依赖关系。我们首先梳理几个对象之间的关系,其次分析一个pod对象的增、删、改时,几个索引对象的状态迁移如何发生。

2.1 threadSafeMap内部对象关系

可以通过下图理解threadSafeMap中各种索引之间的关系

具体举例:

对象键:objkey
索引键:indexkey

  • items:
    Items类型map[string] interface{},用来存储runtime.object,map的key(对象键)用函数MetaNamespaceKeyFunc生成,value是runtime.object本身。
  • Indexers:
    indexers类型是map[string]indexFunc,用来存储indexFunc(索引键计算函数),map的key是indexFuncName,value是indexFunc。注意两个要点:1. 区别对象键和索引键;2. {indexName: indexFunc}是外部调用者传入,调用关系参考k8s源码: kubernetes-master\pkg\client\informers\informers_generated\internalversion\extensions\internalversion\deployment.go:
func (f *customResourceDefinitionInformer) defaultInformer(client internalclientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
        return NewFilteredCustomResourceDefinitionInformer(client, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
}
  • Indices:
    Indices类型map[string]Index。indices是对象快速索引表,map的key是indexName,value是Index对象,即map[string]sets.String。

  • Index:
    Index类型map[string]sets.String。Index map的key是indexFunc计算的值,如MetaNamespaceIndexFunc返回的是namespace,如果使用indexByPodNodeName(源码:kubernetes-master\pkg\controller\daemon\daemon_controller.go)那么返回nodename。这里的namespace和nodename是指具体实例,比如default,kube-system,node1, node2 。。。

  • keySets:
    keySets 是笔者自己取的名字。keySets 是set.String类型,值是对象键objkey。到此为止,相信细心的读者可能会发现,threadSafeMap做了两件事:1)存储:保存k8s runtime.object到items map。2)索引:为items map的每个对象建立三层索引:第一层是indices map的类别索引,如按'namespace', 'nodeName'索引,这里是抽象类别,indices的key就是字符串“namespace”或 “nodeName”;第二层是index map的详细类别索引,就是具体地namespace,如'namespace1','namespace2'… 或者具体的节点名,如'nodeName1','nodeName2'……;第三层是indexSet的对象键。是一个集合,第三层索引才真正索引到runtime.object。

2.2 threadSafeMap关键实现分析

以两个典型的方法 func (c *threadSafeMap) updateIndices(oldObj interface{}, newObj interface{}, key string)和func (c *threadSafeMap) deleteFromIndices(obj interface{}, key string) 为例分析threadSafeMap内部对象的状态迁移过程。

// deleteFromIndices removes the object from each of the managed indexes// it is intended to be called from a function that already has a lock on the cachefunc (c *threadSafeMap) deleteFromIndices(obj interface{}, key string) {
        for name, indexFunc := range c.indexers {
                indexValues, err := indexFunc(obj)
                if err != nil {
                        panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
                }

                index := c.indices[name]
                if index == nil {
                        continue
                }
                for _, indexValue := range indexValues {
                        set := index[indexValue]
                        if set != nil {
                                set.Delete(key)
                        }
                }
        }
}

1)遍历indexFunc map c.indexers获取第一层索引indices的类别name(抽象大类,如namesapce,nodeName。。。),并计算第二层索引index的详细索引类别indexValue(详细索引类,如default, kube-system等namesapce,node1,node2,master等nodeName);
2)遍历第二层索引表index,依次删除第三层索引表indexSet的对象键。

updateIndices函数被Add,Update,Replace等函数调用。

// updateIndices modifies the objects location in the managed indexes, if this is an update, you must provide an oldObj// updateIndices must be called from a function that already has a lock on the cachefunc (c *threadSafeMap) updateIndices(oldObj interface{}, newObj interface{}, key string) {
        // if we got an old object, we need to remove it before we add it again
        if oldObj != nil {
                c.deleteFromIndices(oldObj, key)
        }
        for name, indexFunc := range c.indexers {
                indexValues, err := indexFunc(newObj)
                if err != nil {
                        panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
                }
                index := c.indices[name]
                if index == nil {
                        index = Index{}
                        c.indices[name] = index
                }

                for _, indexValue := range indexValues {
                        set := index[indexValue]
                        if set == nil {
                                set = sets.String{}
                                index[indexValue] = set
                        }
                        set.Insert(key)
                }
        }
}

updateIndices执行步骤如下:1)updateIndices函数首先删除oldObj的索引键;2)遍历indexFunc map c.indexers,获取第一层索引indices的索引类别name,并计算第二层索引Index的详细索引类别indexValues(数组);3)遍历第二层索引Index,依次执行:若第三层索引IndexSet为空则添加一个空set;若非空则添加对象键到第三层索引IndexSet。

参考文档

https://www.jianshu.com/p/d17f70369c35
https://blog.csdn.net/weixin_42663840/article/details/81530606

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值