前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >K8s源码分析(24)-ThreadSafeStore组件

K8s源码分析(24)-ThreadSafeStore组件

作者头像
TA码字
发布2022-10-30 13:25:17
2360
发布2022-10-30 13:25:17
举报
文章被收录于专栏:TA码字TA码字

上一篇文章里,我们主要介绍了和资源索引相关的一系列对象,其中包括了 indexer 对象,index 对象,以及 indices 对象等等。在本篇文章里我们主要来介绍和对象存储相关的组件 ThreadSafeStore 接口以及其实现。

ThreadSafeStore 接口

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

代码语言:javascript
复制
//k8s.io/client-go/tools/cache/thread_safe_store.go
type ThreadSafeStore interface {
  Add(key string, obj interface{})
  Update(key string, obj interface{})
  Delete(key string)
  Get(key string) (item interface{}, exists bool)
  List() []interface{}
  ListKeys() []string
  Replace(map[string]interface{}, string)
  Index(indexName string, obj interface{}) ([]interface{}, error)
  IndexKeys(indexName, indexKey string) ([]string, error)
  ListIndexFuncValues(name string) []string
  ByIndex(indexName, indexKey string) ([]interface{}, error)
  GetIndexers() Indexers

  // AddIndexers adds more indexers to this store.  If you call this after you already have data
  // in the store, the results are undefined.
  AddIndexers(newIndexers Indexers) error
  // Resync is a no-op and is deprecated
  Resync() error
}

  • 该接口定义了对于资源增删改查的方法,例如 Add/Update/Delete/Get/List 等操作。
  • 该接口定义了对于资源进行索引相关的方法,例如 Index/IndexKeys/ByIndex 等系列操作。

threadSafeMap 结构体

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

代码语言:javascript
复制
//k8s.io/client-go/tools/cache/thread_safe_store.go
type threadSafeMap struct {
  lock  sync.RWMutex
  items map[string]interface{}

  // indexers maps a name to an IndexFunc
  indexers Indexers
  // indices maps a name to an Index
  indices Indices
}


func (c *threadSafeMap) Add(key string, obj interface{}) {
  c.Update(key, obj)
}

func (c *threadSafeMap) Update(key string, obj interface{}){...}
func (c *threadSafeMap) Delete(key string) {...}
func (c *threadSafeMap) Get(key string) (item interface{}, exists bool){...}
func (c *threadSafeMap) List() []interface{...}
func (c *threadSafeMap) ListKeys() []string  {...}
func (c *threadSafeMap) Replace(items map[string]interface{}, resourceVersion string) {...}
func (c *threadSafeMap) Index(indexName string, obj interface{}) ([]interface{}, error) {...} {...}
func (c *threadSafeMap) ByIndex(indexName, indexedValue string) ([]interface{}, error) {...}
func (c *threadSafeMap) IndexKeys(indexName, indexedValue string) ([]string, error) {...}
func (c *threadSafeMap) ListIndexFuncValues(indexName string) []string {...}
.......
  • 该结构体中有名为 items 的 map 属性,其中存储了资源对象的 key 和资源对象本身。
  • 该结构体中上一篇文章中介绍的 Indexer 对象,存储了相应的命名索引函数。
  • 该结构体中上一篇文章中介绍的 Iidices 对象,存储了相应的命名索引。
  • 该结构体实现了 ThreadSafeStore 接口中对于资源进行增删改查相关的方法,例如 Add/Update/Delete/Get/List 等等。
  • 该结构体实现了 ThreadSafeStore 接口中对于资源进行索引操作相关的方法,例如 Index/IndexKeys/ByIndex 等等。
  • 对于该结构体实现的方法都有 Mutex 锁操作,所以是并发安全的。

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 TA码字 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档