k8s controller分析

controller数据结构分析,从上往下看

type BeeController struct {
   queue *controller.QueueWorker

   // Handles messages
   controller *BeeControllerImpl

   Name string

   BeforeReconcile func(key string)
   AfterReconcile  func(key string, err error)

   Informers *sharedinformers.SharedInformers
}

controller中的Informers就是SharedInformers,而controller就是BeeControllerImpl结构。

type SharedInformers struct {
   controller.SharedInformersDefaults
   Factory externalversions.SharedInformerFactory
}
type BeeControllerImpl struct {
   builders.DefaultControllerFns

   // lister indexes properties about Bee
   lister listers.BeeLister
}

SharedInformers中的Factory中就是sharedInformerFactory

type sharedInformerFactory struct {
   client           clientset.Interface
   namespace        string
   tweakListOptions internalinterfaces.TweakListOptionsFunc
   lock             sync.Mutex
   defaultResync    time.Duration

   informers map[reflect.Type]cache.SharedIndexInformer
   // startedInformers is used for tracking which informers have been started.
   // This allows Start() to be called multiple times safely.
   startedInformers map[reflect.Type]bool
}
func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
   f.lock.Lock()
   defer f.lock.Unlock()

   informerType := reflect.TypeOf(obj)
   informer, exists := f.informers[informerType]
   if exists {
      return informer
   }
   informer = newFunc(f.client, f.defaultResync)
   f.informers[informerType] = informer

   return informer
}

其存在InformerFor成员函数,用于informers中添加shareInformerIndexerFactory(),通过informer()函数获取。shareInformerIndexerFactory拥有addeventhandler来添加具体的回调函数。

举例:Informer()生成对应的shareInformerIndexerFactory()     从下往上看

func NewFilteredBeeInformer(client clientset.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
   return cache.NewSharedIndexInformer(
      &cache.ListWatch{
         ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
            if tweakListOptions != nil {
               tweakListOptions(&options)
            }
            return client.AlphaV1alpha1().Bees(namespace).List(options)
         },
         WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
            if tweakListOptions != nil {
               tweakListOptions(&options)
            }
            return client.AlphaV1alpha1().Bees(namespace).Watch(options)
         },
      },
      &alpha_v1alpha1.Bee{},
      resyncPeriod,
      indexers,
   )
}

func (f *beeInformer) defaultInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
   return NewFilteredBeeInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
}

func (f *beeInformer) Informer() cache.SharedIndexInformer {
   return f.factory.InformerFor(&alpha_v1alpha1.Bee{}, f.defaultInformer)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值