1. 程式人生 > >k8s controller分析

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)
}