Skip to content

Commit

Permalink
Merge pull request #244 from dvonthenen/feature/sharedinformer
Browse files Browse the repository at this point in the history
Refactor using single SharedInformer
  • Loading branch information
k8s-ci-robot authored Sep 4, 2019
2 parents f11e58a + 33cfc0f commit b67f516
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/cloudprovider/vsphere/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (vs *VSphere) Initialize(clientBuilder cloudprovider.ControllerClientBuilde
if err == nil {
klog.V(1).Info("Kubernetes Client Init Succeeded")

vs.informMgr = k8s.NewInformer(client)
vs.informMgr = k8s.NewInformer(client, true)

connMgr := cm.NewConnectionManager(vs.cfg, vs.informMgr, client)
vs.connectionManager = connMgr
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/connectionmanager/connectionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (connMgr *ConnectionManager) createManagersPerTenant(secretName string, sec
var informMgr *k8s.InformerManager
var lister listerv1.SecretLister
if client != nil && secretsDirectory == "" {
informMgr = k8s.NewInformer(client)
informMgr = k8s.NewInformer(client, true)
lister = informMgr.GetSecretLister()
}

Expand Down
22 changes: 18 additions & 4 deletions pkg/common/kubernetes/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,30 @@ func noResyncPeriodFunc() time.Duration {
return 0
}

var signalHandler <-chan struct{}
var (
signalHandler <-chan struct{}
informerFactory informers.SharedInformerFactory
)

// NewInformer creates a newk8s client based on a service account
func NewInformer(client clientset.Interface) *InformerManager {
func NewInformer(client clientset.Interface, singleWatcher bool) *InformerManager {
if signalHandler == nil {
signalHandler = signals.SetupSignalHandler()
}

var myInformerFactory informers.SharedInformerFactory
if singleWatcher {
if informerFactory == nil {
informerFactory = informers.NewSharedInformerFactory(client, noResyncPeriodFunc())
}
myInformerFactory = informerFactory
} else {
myInformerFactory = informers.NewSharedInformerFactory(client, noResyncPeriodFunc())
}
return &InformerManager{
client: client,
stopCh: signalHandler,
informerFactory: informers.NewSharedInformerFactory(client, noResyncPeriodFunc()),
informerFactory: myInformerFactory,
}
}

Expand All @@ -66,7 +79,8 @@ func (im *InformerManager) AddNodeListener(add, remove func(obj interface{}), up
})
}

// Listen starts the Informers
// Listen starts the Informers. Based on client-go informer package, if the Lister has
// already been initialized, it will not re-init them. Only new non-init Listers will be initialized.
func (im *InformerManager) Listen() {
go im.informerFactory.Start(im.stopCh)
}

0 comments on commit b67f516

Please sign in to comment.