🚀 优质资源分享 🚀
学习路线指引(点击解锁) | 知识定位 | 人群定位 |
---|---|---|
🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
💛Python量化交易实战💛 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
client-go之DeltaFIFO源码分析
1.DeltaFIFO概述
先从名字上来看,DeltaFIFO,首先它是一个FIFO,也就是一个先进先出的队列,而Delta代表变化的资源对象,其包含资源对象数据本身及其变化类型。
Delta的组成:
type Delta struct {
Type DeltaType
Object interface{}
}
DeltaFIFO的组成:
type DeltaFIFO struct {
...
items map[string]Deltas
queue []string
...
}
type Deltas []Delta
具体来说,DeltaFIFO存储着map[object key]Deltas以及object key的queue,Delta装有对象数据及对象的变化类型。输入输出方面,Reflector负责DeltaFIFO的输入,Controller负责处理DeltaFIFO的输出。
一个对象能算出一个唯一的object key,其对应着一个Deltas,所以一个对象对应着一个Deltas。
而目前Delta有4种Type,分别是: Added、Updated、Deleted、Sync。针对同一个对象,可能有多个不同Type的Delta元素在Deltas中,表示对该对象做了不同的操作,另外,也可能有多个相同Type的Delta元素在Deltas中(除Deleted外,Delted类型会被去重),比如短时间内,多次对某一个对象进行了更新操作,那么就会有多个Updated类型的Delta放入Deltas中。
2.DeltaFIFO的定义与初始化分析
2.1 DeltaFIFO struct
DeltaFIFO struct定义了DeltaFIFO的一些属性,下面挑几个重要的分析一下。
(1)lock:读写锁,操作DeltaFIFO中的items与queue之前都要先加锁;
(2)items:是个map,key根据对象算出,value为Deltas类型;
(3)queue:存储对象key的队列;
(4)keyFunc:计算对象key的函数;
// staging/src/k8s.io/client-go/tools/cache/delta\_fifo.go
type DeltaFIFO struct {
// lock/cond protects access to 'items' and 'queue'.
lock sync.RWMutex
cond sync.Cond
// We depend on the property that items in the set are in
// the queue and vice versa, and that all Deltas in this
// map have at least one Delta.
items map[string]Deltas
queue []string
// populated is true if the first batch of items inserted by Replace() has been populated
// or Delete/Add/Update was called first.
populated bool
// initialPopulationCount is the number of items inserted by the first call of Replace()
initialPopulationCount int
// keyFunc is used to make the key used for queued item
// insertion and retrieval, and should be deterministic.
keyFunc KeyFunc
// knownObjects list keys that are "known", for the
// purpose of figuring out which items have been deleted
// when Replace() or Delete() is called.
knownObjects KeyListerGetter
// Indication the queue is closed.
// Used to indicate a queue is closed so a control loop can exit when a queue is empty.
// Currently, not used to gate any of CRED operations.
closed bool
closedLock sync.Mutex
type Deltas
再来看一下Deltas类型,是Delta的切片类型。
type Deltas []Delta
type Delta
继续看到Delta类型,其包含两个属性:
(1)Type:代表的是Delta的类型,有Added、Updated、Deleted、Sync四个类型;
(2)Object:存储的资源对象,如pod等资源对象;
type Delta struct {
Type DeltaType
Object