Controller

Controller作为核心中枢,集成了Reflector、DeltaFIFO、Indexer等组件,成为连接下游消费者的桥梁。Informer组件的启动,以及数据在几个组件之间的传递都由Controller完成。

Controller的定义如下。

type controller struct {
    config         Config
    reflector      *Reflector
    reflectorMutex sync.RWMutex
    clock          clock.Clock
}

Controller继承了核心组件,定义示例如下。

type Config struct {
    Queue
    ListerWatcher
    Process ProcessFunc
    ObjectType runtime.Object
    FullResyncPeriod time.Duration
    ShouldResync ShouldResyncFunc
    RetryOnError bool
    WatchErrorHandler WatchErrorHandler
    WatchListPageSize int64
}

Config结构体的字段介绍如下。

·Queue:实际由DeltaFIFO实现。

·ListerWatcher:用于构造Reflector。

·Process ProcessFunc:Pop操作得到的对象处理函数。

·ObjectType runtime.Object:目标对象类型。

·FullResyncPeriod time.Duration:全量重新同步周期。

·ShouldResync ShouldResyncFunc:是否进行重新同步的判断函数。

·RetryOnError bool:如果为true,Process()函数返回err,并再次入队。

·WatchErrorHandler WatchErrorHandler:Watch方法返回err的回调函数。

·WatchListPageSize int64:Watch方法的分页大小。

Controller中以goroutine协程方式启动Run方法,会启动Reflector的List/Watch方法,用于从API Server中拉取全量资源并监听增量资源,将资源存储到DeltaFIFO中。接着,启动processLoop不断从DeltaFIFOPop中消费资源。在sharedIndexInformer中取出数据,之后使用HandleDeltas方法进行处理,一方面维护Indexer中的数据,另一方面调用下游sharedProcessor方法进行处理。

posted @ 2023-02-26 14:29  muzinan110  阅读(86)  评论(0编辑  收藏  举报