From f0bcda0dcef06b0745f4e16c29ee81e7a3584977 Mon Sep 17 00:00:00 2001 From: kitianFresh <777tiangoole@gmail.com> Date: Thu, 9 Sep 2021 13:37:25 +0800 Subject: [PATCH] avoid sharedIndexInformer run more than once, avoid more cache and memory consume Kubernetes-commit: 305c13faca48d1a6ea4d263e262ebc8df0387c84 --- tools/cache/shared_informer.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/cache/shared_informer.go b/tools/cache/shared_informer.go index 1956f265..1a95dcfe 100644 --- a/tools/cache/shared_informer.go +++ b/tools/cache/shared_informer.go @@ -368,6 +368,10 @@ func (s *sharedIndexInformer) SetWatchErrorHandler(handler WatchErrorHandler) er func (s *sharedIndexInformer) Run(stopCh <-chan struct{}) { defer utilruntime.HandleCrash() + if s.HasStarted() { + klog.Warningf("The sharedIndexInformer has started, run more than once is not allowed") + return + } fifo := NewDeltaFIFOWithOptions(DeltaFIFOOptions{ KnownObjects: s.indexer, EmitDeltaTypeReplaced: true, @@ -410,6 +414,12 @@ func (s *sharedIndexInformer) Run(stopCh <-chan struct{}) { s.controller.Run(stopCh) } +func (s *sharedIndexInformer) HasStarted() bool { + s.startedLock.Lock() + defer s.startedLock.Unlock() + return s.started +} + func (s *sharedIndexInformer) HasSynced() bool { s.startedLock.Lock() defer s.startedLock.Unlock()