深入分析K8S SharedInformer源碼

深入分析K8S SharedInformer源碼

云原生應(yīng)用(基于k8s的應(yīng)用)一定繞不開SharedInformer,所以分析一下該類诸尽。

sharedInformer

SharedInformer has a shared data cache and is capable of distributing notifications for changes to the cache to multiple listeners who registered via AddEventHandler.

If you use this, there is one behavior change compared to a standard Informer. When you receive a notification, the cache will be AT LEAST as fresh as the notification, but it MAY be more fresh. You should NOT depend on the contents of the cache exactly matching the notification you've received in handler functions. If there was a create, followed by a delete, the cache may NOT have your item. This has advantages over the broadcaster since it allows us to share a common cache across many controllers. Extending the broadcaster would have required us keep duplicate caches for each watch.

sharedInformer有兩個功能,cache和注冊事件監(jiān)聽绸硕。cache功能主要作用是減少對apiserver的直接訪問(ps. 畢竟 client-go里有限流=恶座。=)鲤拿。事件監(jiān)聽可以幫助構(gòu)建自己的云原生應(yīng)用胶台。

type SharedInformer interface {
   // AddEventHandler adds an event handler to the shared informer using the shared informer's resync
   // period.  Events to a single handler are delivered sequentially, but there is no coordination
   // between different handlers.
   AddEventHandler(handler ResourceEventHandler)
   // AddEventHandlerWithResyncPeriod adds an event handler to the shared informer using the
   // specified resync period.  Events to a single handler are delivered sequentially, but there is
   // no coordination between different handlers.
   AddEventHandlerWithResyncPeriod(handler ResourceEventHandler, resyncPeriod time.Duration)
   // GetStore returns the Store.
   GetStore() Store
   // GetController gives back a synthetic interface that "votes" to start the informer
   GetController() Controller
   // Run starts the shared informer, which will be stopped when stopCh is closed.
   Run(stopCh <-chan struct{})
   // HasSynced returns true if the shared informer's store has synced.
   HasSynced() bool
   // LastSyncResourceVersion is the resource version observed when last synced with the underlying
   // store. The value returned is not synchronized with access to the underlying store and is not
   // thread-safe.
   LastSyncResourceVersion() string
}

最重要的函數(shù)應(yīng)該是:Run, AddEventHandler, GetIndexer, HasSynced

sharedInformer.Run

func (s *sharedIndexInformer) Run(stopCh <-chan struct{}) {
   defer utilruntime.HandleCrash()

   fifo := NewDeltaFIFO(MetaNamespaceKeyFunc, s.indexer)
    
   cfg := &Config{
        Queue:            fifo,
        ListerWatcher:    s.listerWatcher,
        ObjectType:       s.objectType,
        FullResyncPeriod: s.resyncCheckPeriod,
        RetryOnError:     false,
        ShouldResync:     s.processor.shouldResync,

        Process: s.HandleDeltas,
   }

   func() {
      s.startedLock.Lock()
      defer s.startedLock.Unlock()

      s.controller = New(cfg)
      s.controller.(*controller).clock = s.clock
      s.started = true
   }()

   // Separate stop channel because Processor should be stopped strictly after controller
   processorStopCh := make(chan struct{})
   var wg wait.Group
   defer wg.Wait()              // Wait for Processor to stop
   defer close(processorStopCh) // Tell Processor to stop
   wg.StartWithChannel(processorStopCh, s.cacheMutationDetector.Run)
   wg.StartWithChannel(processorStopCh, s.processor.run)

   defer func() {
      s.startedLock.Lock()
      defer s.startedLock.Unlock()
      s.stopped = true // Don't want any new listeners
   }()
   s.controller.Run(stopCh)
}

processor.run處理ev handler歼疮,后面再分析

controller.Run

type Controller interface {
    Run(stopCh <-chan struct{})
    HasSynced() bool
    LastSyncResourceVersion() string
}

// Run begins processing items, and will continue until a value is sent down stopCh.
// It's an error to call Run more than once.
// Run blocks; call via go.
func (c *controller) Run(stopCh <-chan struct{}) {
   defer utilruntime.HandleCrash()
   go func() {
      <-stopCh
      c.config.Queue.Close()
   }()
   r := NewReflector(
      c.config.ListerWatcher,
      c.config.ObjectType,
      c.config.Queue, // store
      c.config.FullResyncPeriod,
   )
   r.ShouldResync = c.config.ShouldResync
   r.clock = c.clock

   c.reflectorMutex.Lock()
   c.reflector = r
   c.reflectorMutex.Unlock()

   var wg wait.Group
   defer wg.Wait()

   wg.StartWithChannel(stopCh, r.Run)

   wait.Until(c.processLoop, time.Second, stopCh)
}

NewReflector & reflector.run 生產(chǎn)者

c.processLoop 消費者

首先分析生產(chǎn)者

Reflector.Run

// Reflector watches a specified resource and causes all changes to be reflected in the given store.
type Reflector struct {
   // name identifies this reflector. By default it will be a file:line if possible.
   name string
   // metrics tracks basic metric information about the reflector
   metrics *reflectorMetrics

   // The type of object we expect to place in the store.
   expectedType reflect.Type
   // The destination to sync up with the watch source
   store Store
   // listerWatcher is used to perform lists and watches.
   listerWatcher ListerWatcher
   // period controls timing between one watch ending and
   // the beginning of the next one.
   period       time.Duration
   resyncPeriod time.Duration
   ShouldResync func() bool
   // clock allows tests to manipulate time
   clock clock.Clock
   // lastSyncResourceVersion is the resource version token last
   // observed when doing a sync with the underlying store
   // it is thread safe, but not synchronized with the underlying store
   lastSyncResourceVersion string
   // lastSyncResourceVersionMutex guards read/write access to lastSyncResourceVersion
   lastSyncResourceVersionMutex sync.RWMutex
}

ListAndWatch核心類

// Run starts a watch and handles watch events. Will restart the watch if it is closed.
// Run will exit when stopCh is closed.
func (r *Reflector) Run(stopCh <-chan struct{}) {
   wait.Until(func() {
      if err := r.ListAndWatch(stopCh); err != nil {
         utilruntime.HandleError(err)
      }
   }, r.period, stopCh)
}

ListAndWatch

// ListAndWatch first lists all items and get the resource version at the moment of call,
// and then use the resource version to watch.
// It returns error if ListAndWatch didn't even try to initialize watch.
func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
   var resourceVersion string

   // Explicitly set "0" as resource version - it's fine for the List()
   // to be served from cache and potentially be delayed relative to
   // etcd contents. Reflector framework will catch up via Watch() eventually.
   options := metav1.ListOptions{ResourceVersion: "0"}

   if err := func() error {
      var list runtime.Object
      var err error
      listCh := make(chan struct{}, 1)
      panicCh := make(chan interface{}, 1)
      go func() {
         defer func() {
            if r := recover(); r != nil {
               panicCh <- r
            }
         }()
         list, err = r.listerWatcher.List(options)
         close(listCh)
      }()
      select {
      case <-stopCh:
         return nil
      case r := <-panicCh:
         panic(r)
      case <-listCh:
      }
      listMetaInterface, err := meta.ListAccessor(list)
      resourceVersion = listMetaInterface.GetResourceVersion()
      items, err := meta.ExtractList(list)
      
      // 同步緩存 
      if err := r.syncWith(items, resourceVersion); err != nil {
         return fmt.Errorf("%s: Unable to sync list result: %v", r.name, err)
      }
      
      r.setLastSyncResourceVersion(resourceVersion)
      return nil
   }(); err != nil {
      return err
   }

    
// syncWith replaces the store's items with the given list.
func (r *Reflector) syncWith(items []runtime.Object, resourceVersion string) error {
    found := make([]interface{}, 0, len(items))
    for _, item := range items {
        found = append(found, item)
    }
    return r.store.Replace(found, resourceVersion)
}

list一份完整的資源;更新緩存;Replace

   resyncerrc := make(chan error, 1)
   cancelCh := make(chan struct{})
   defer close(cancelCh)
   go func() {
      resyncCh, cleanup := r.resyncChan()
      defer func() {
         cleanup() // Call the last one written into cleanup
      }()
      for {
         select {
         case <-resyncCh:
         case <-stopCh:
            return
         case <-cancelCh:
            return
         }
         if r.ShouldResync == nil || r.ShouldResync() {
            if err := r.store.Resync(); err != nil {
               resyncerrc <- err
               return
            }
         }
         cleanup()
         resyncCh, cleanup = r.resyncChan()
      }
   }()

func (r *Reflector) resyncChan() (<-chan time.Time, func() bool) {
    t := r.clock.NewTimer(r.resyncPeriod)
    return t.C(), t.Stop
}

定時重新同步存儲


   for {
      case <-stopCh:
         return nil
      default:
      }

      timeoutSeconds := int64(minWatchTimeout.Seconds() * (rand.Float64() + 1.0))
      options = metav1.ListOptions{
         ResourceVersion: resourceVersion,
         TimeoutSeconds: &timeoutSeconds,
      }

      w, err := r.listerWatcher.Watch(options)

      if err := r.watchHandler(w, &resourceVersion, resyncerrc, stopCh); err != nil {
      }
   }
}

// watchHandler watches w and keeps *resourceVersion up to date.
func (r *Reflector) watchHandler(w watch.Interface, resourceVersion *string, errc chan error, stopCh <-chan struct{}) error {
    start := r.clock.Now()
    eventCount := 0

    defer w.Stop()

loop:
    for {
        select {
        case <-stopCh:
            return errorStopRequested
        case err := <-errc:
            return err
        case event, ok := <-w.ResultChan():
            if !ok {
                break loop
            }

            newResourceVersion := meta.GetResourceVersion()
            switch event.Type {
            case watch.Added:
                err := r.store.Add(event.Object)
            case watch.Modified:
                err := r.store.Update(event.Object)
            case watch.Deleted:
                err := r.store.Delete(event.Object)
                }
            default:
            }
            *resourceVersion = newResourceVersion
            r.setLastSyncResourceVersion(newResourceVersion)
            eventCount++
        }
    }

    return nil
}

watch list之后的變更嗤锉,更新store仲闽。

DeltaFIFO重要數(shù)據(jù)結(jié)構(gòu)后續(xù)會分析

生產(chǎn)者分析完了,繼續(xù)分析消費者

controller.processLoop

需要注意Reflector.Store就是controller.config.Queue

// processLoop drains the work queue.
func (c *controller) processLoop() {
   for {
      obj, err := c.config.Queue.Pop(PopProcessFunc(c.config.Process))
   }
}

從queue中pop刁赦,返回之前調(diào)用process func

func (s *sharedIndexInformer) HandleDeltas(obj interface{}) error {
   s.blockDeltas.Lock()
   defer s.blockDeltas.Unlock()

   // from oldest to newest
   for _, d := range obj.(Deltas) {
      switch d.Type {
      case Sync, Added, Updated:
         isSync := d.Type == Sync
         s.cacheMutationDetector.AddObject(d.Object)
         if old, exists, err := s.indexer.Get(d.Object); err == nil && exists {
            s.indexer.Update(d.Object)
             
            s.processor.distribute(updateNotification{oldObj: old, newObj: d.Object}, isSync)
         } else {
            err := s.indexer.Add(d.Object)
             
            s.processor.distribute(addNotification{newObj: d.Object}, isSync)
         }
      case Deleted:
         s.indexer.Delete(d.Object)
          
         s.processor.distribute(deleteNotification{oldObj: d.Object}, false)
      }
   }
   return nil
}

處理delta增量的過程,更新完緩存之后會分發(fā)給listeners處理

func (p *sharedProcessor) distribute(obj interface{}, sync bool) {
   p.listenersLock.RLock()
   defer p.listenersLock.RUnlock()

   if sync {
      for _, listener := range p.syncingListeners {
         listener.add(obj)
      }
   } else {
      for _, listener := range p.listeners {
         listener.add(obj)
      }
   }
}

processorListener

func newProcessListener(handler ResourceEventHandler, requestedResyncPeriod, resyncPeriod time.Duration, now time.Time, bufferSize int) *processorListener {
    ret := &processorListener{
        nextCh:                make(chan interface{}),
        addCh:                 make(chan interface{}),
        handler:               handler,
        pendingNotifications:  *buffer.NewRingGrowing(bufferSize),
        requestedResyncPeriod: requestedResyncPeriod,
        resyncPeriod:          resyncPeriod,
    }

    ret.determineNextResync(now)

    return ret
}

func (p *processorListener) add(notification interface{}) {
   p.addCh <- notification
}

注意addCh是一個block channel,根據(jù)addCh找到pop方法

func (p *processorListener) pop() {
   defer utilruntime.HandleCrash()
   defer close(p.nextCh) // Tell .run() to stop

   var nextCh chan<- interface{}
   var notification interface{}
   for {
      select {
      case nextCh <- notification:
         // Notification dispatched
         var ok bool
         notification, ok = p.pendingNotifications.ReadOne()
         if !ok { // Nothing to pop
            nextCh = nil // Disable this select case
         }
      case notificationToAdd, ok := <-p.addCh:
         if notification == nil { 
            notification = notificationToAdd
            nextCh = p.nextCh
         } else { // There is already a notification waiting to be dispatched
            p.pendingNotifications.WriteOne(notificationToAdd)
         }
      }
   }
}

這段代碼比較有意思阅仔,nextCh也是一個block ch。p有一個pendingNotifications緩存未處理的notification弧械,真正處理用nextCh八酒。不用buffer channel做,可能是因為不好設(shè)置channel buffer size

type processorListener struct {
    nextCh chan interface{}
    addCh  chan interface{}

    handler ResourceEventHandler

    // pendingNotifications is an unbounded ring buffer that holds all notifications not yet distributed.
    // There is one per listener, but a failing/stalled listener will have infinite pendingNotifications
    // added until we OOM.
    // TODO: This is no worse than before, since reflectors were backed by unbounded DeltaFIFOs, but
    // we should try to do something better.
    pendingNotifications buffer.RingGrowing

    resyncPeriod time.Duration
    nextResync time.Time
    resyncLock sync.Mutex
}

根據(jù)nextCh找到run方法

func (p *processorListener) run() {
   stopCh := make(chan struct{})
   wait.Until(func() {
      err := wait.ExponentialBackoff(retry.DefaultRetry, func() (bool, error) {
         for next := range p.nextCh {
            switch notification := next.(type) {
            case updateNotification:
               p.handler.OnUpdate(notification.oldObj, notification.newObj)
            case addNotification:
               p.handler.OnAdd(notification.newObj)
            case deleteNotification:
               p.handler.OnDelete(notification.oldObj)
            default:
               utilruntime.HandleError(fmt.Errorf("unrecognized notification: %#v", next))
            }
         }
      })

   }, 1*time.Minute, stopCh)
}

根據(jù)notification的類型刃唐,回調(diào)注冊的handler處理函數(shù)羞迷,使用方法如下

podInformer.AddEventHandler(cache.FilteringResourceEventHandler{
   FilterFunc: pc.filter,
   Handler: cache.ResourceEventHandlerFuncs{
      AddFunc:    pc.add,
      UpdateFunc: pc.update,
      DeleteFunc: pc.delete,
   },
})

Cache

緩存的使用方法如下:

func initNodeCache() {
    informerFactory := informers.NewSharedInformerFactory(K8sClient, 0)

    nodeInformer := informerFactory.Core().V1().Nodes()
    NodeStore = nodeInformer.Lister()

    forever := make(chan struct{})
    informerFactory.Start(forever)

    if !k8scache.WaitForCacheSync(forever, nodeInformer.Informer().HasSynced) {
        return
    }
}

func (f *nodeInformer) Lister() v1.NodeLister {
    return v1.NewNodeLister(f.Informer().GetIndexer())
}

至此sharedInformer已經(jīng)分析完畢,還遺留幾個重要數(shù)據(jù)結(jié)構(gòu)后面分析画饥。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末衔瓮,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子抖甘,更是在濱河造成了極大的恐慌热鞍,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異薇宠,居然都是意外死亡偷办,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門澄港,熙熙樓的掌柜王于貴愁眉苦臉地迎上來爽篷,“玉大人,你說我怎么就攤上這事慢睡≈鸸ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵漂辐,是天一觀的道長泪喊。 經(jīng)常有香客問我,道長髓涯,這世上最難降的妖魔是什么袒啼? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮纬纪,結(jié)果婚禮上蚓再,老公的妹妹穿的比我還像新娘。我一直安慰自己包各,他們只是感情好摘仅,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著问畅,像睡著了一般娃属。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上护姆,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天矾端,我揣著相機與錄音,去河邊找鬼卵皂。 笑死秩铆,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的灯变。 我是一名探鬼主播殴玛,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼柒凉!你這毒婦竟也來了族阅?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤膝捞,失蹤者是張志新(化名)和其女友劉穎坦刀,沒想到半個月后愧沟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡鲤遥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年沐寺,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盖奈。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡混坞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出钢坦,到底是詐尸還是另有隱情究孕,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布爹凹,位于F島的核電站厨诸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏禾酱。R本人自食惡果不足惜微酬,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望颤陶。 院中可真熱鬧颗管,春花似錦、人聲如沸滓走。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽闲坎。三九已至疫粥,卻和暖如春茬斧,著一層夾襖步出監(jiān)牢的瞬間腰懂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工项秉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留绣溜,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓娄蔼,卻偏偏與公主長得像怖喻,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子岁诉,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359