informer机制之cache.indexer机制

本文详细介绍了Kubernetes Informer中的Cache.Indexer机制,包括Indexer接口、Store结构、Cache结构及其在实际应用中的工作原理,特别是如何通过索引加速对象查找。内容涵盖IndexFunc、Indexers、Indices的关系以及ThreadSafeStore的实现。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值