Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor to initialize shared informer only once #245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions pkg/cloudprovider/vsphere/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (

cloudprovider "k8s.io/cloud-provider"

"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
"k8s.io/cloud-provider-vsphere/pkg/cloudprovider/vsphere/server"
vcfg "k8s.io/cloud-provider-vsphere/pkg/common/config"
cm "k8s.io/cloud-provider-vsphere/pkg/common/connectionmanager"
k8s "k8s.io/cloud-provider-vsphere/pkg/common/kubernetes"
)

const (
Expand Down Expand Up @@ -66,18 +67,25 @@ func (vs *VSphere) Initialize(clientBuilder cloudprovider.ControllerClientBuilde
if err == nil {
klog.V(1).Info("Kubernetes Client Init Succeeded")

vs.informMgr = k8s.NewInformer(client, true)
informerFactory := informers.NewSharedInformerFactory(client, 0)
secretLister := informerFactory.Core().V1().Secrets().Lister()
nodeInformer := informerFactory.Core().V1().Nodes().Informer()

connMgr := cm.NewConnectionManager(vs.cfg, vs.informMgr, client)
connMgr := cm.NewConnectionManager(vs.cfg, secretLister, client)
vs.connectionManager = connMgr
vs.nodeManager.connectionManager = connMgr

vs.informMgr.AddNodeListener(vs.nodeAdded, vs.nodeDeleted, nil)
nodeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: vs.nodeAdded,
UpdateFunc: nil,
DeleteFunc: vs.nodeDeleted,
})

vs.informMgr.Listen()
informerFactory.WaitForCacheSync(stop)
informerFactory.Start(stop)

//if running secrets, init them
connMgr.InitializeSecretLister()
//if running muiltiple VCs, init the credential managers for each of them
connMgr.InitializeCredentialManagers(secretLister)

if !vs.cfg.Global.APIDisable {
klog.V(1).Info("Starting the API Server")
Expand Down
2 changes: 0 additions & 2 deletions pkg/cloudprovider/vsphere/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

vcfg "k8s.io/cloud-provider-vsphere/pkg/common/config"
cm "k8s.io/cloud-provider-vsphere/pkg/common/connectionmanager"
k8s "k8s.io/cloud-provider-vsphere/pkg/common/kubernetes"
"k8s.io/cloud-provider-vsphere/pkg/common/vclib"
)

Expand All @@ -39,7 +38,6 @@ type VSphere struct {
cfg *vcfg.Config
connectionManager *cm.ConnectionManager
nodeManager *NodeManager
informMgr *k8s.InformerManager
instances cloudprovider.Instances
zones cloudprovider.Zones
server GRPCServer
Expand Down
38 changes: 7 additions & 31 deletions pkg/common/connectionmanager/connectionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,31 @@ import (

vcfg "k8s.io/cloud-provider-vsphere/pkg/common/config"
cm "k8s.io/cloud-provider-vsphere/pkg/common/credentialmanager"
k8s "k8s.io/cloud-provider-vsphere/pkg/common/kubernetes"
vclib "k8s.io/cloud-provider-vsphere/pkg/common/vclib"
)

// NewConnectionManager returns a new ConnectionManager object
// This function also initializes the Default/Global lister for secrets. In other words,
// If a single global secret is used for all VCs, the informMgr param will be used to
// obtain those secrets
func NewConnectionManager(cfg *vcfg.Config, informMgr *k8s.InformerManager, client clientset.Interface) *ConnectionManager {
func NewConnectionManager(cfg *vcfg.Config, secretLister listerv1.SecretLister, client clientset.Interface) *ConnectionManager {
connMgr := &ConnectionManager{
client: client,
VsphereInstanceMap: generateInstanceMap(cfg),
credentialManagers: make(map[string]*cm.CredentialManager),
informerManagers: make(map[string]*k8s.InformerManager),
}

if informMgr != nil {
if secretLister != nil {
klog.V(2).Info("Initializing with K8s SecretLister")
credMgr := cm.NewCredentialManager(cfg.Global.SecretName, cfg.Global.SecretNamespace, "", informMgr.GetSecretLister())
credMgr := cm.NewCredentialManager(cfg.Global.SecretName, cfg.Global.SecretNamespace, "", secretLister)
connMgr.credentialManagers[vcfg.DefaultCredentialManager] = credMgr
connMgr.informerManagers[vcfg.DefaultCredentialManager] = informMgr

return connMgr
}

if cfg.Global.SecretsDirectory != "" {
klog.V(2).Info("Initializing for generic CO with secrets")
credMgr, _ := connMgr.createManagersPerTenant("", "", cfg.Global.SecretsDirectory, nil)
credMgr := cm.NewCredentialManager("", "", cfg.Global.SecretsDirectory, secretLister)
connMgr.credentialManagers[vcfg.DefaultCredentialManager] = credMgr

return connMgr
Expand Down Expand Up @@ -92,9 +89,9 @@ func generateInstanceMap(cfg *vcfg.Config) map[string]*VSphereInstance {
return vsphereInstanceMap
}

// InitializeSecretLister initializes the individual secret listers that are NOT
// InitializeCredentialManagers initializes the individual secret listers that are NOT
// handled through the Default/Global lister tied to the default service account.
func (connMgr *ConnectionManager) InitializeSecretLister() {
func (connMgr *ConnectionManager) InitializeCredentialManagers(secretLister listerv1.SecretLister) {
// For each vsi that has a Secret set createManagersPerTenant
for _, vInstance := range connMgr.VsphereInstanceMap {
klog.V(3).Infof("Checking vcServer=%s SecretRef=%s", vInstance.Cfg.VCenterIP, vInstance.Cfg.SecretRef)
Expand All @@ -104,32 +101,11 @@ func (connMgr *ConnectionManager) InitializeSecretLister() {
}

klog.V(3).Infof("Adding credMgr/informMgr for vcServer=%s", vInstance.Cfg.VCenterIP)
credsMgr, informMgr := connMgr.createManagersPerTenant(vInstance.Cfg.SecretName,
vInstance.Cfg.SecretNamespace, "", connMgr.client)
credsMgr := cm.NewCredentialManager(vInstance.Cfg.SecretName, vInstance.Cfg.SecretNamespace, "", secretLister)
connMgr.credentialManagers[vInstance.Cfg.SecretRef] = credsMgr
connMgr.informerManagers[vInstance.Cfg.SecretRef] = informMgr
}
}

func (connMgr *ConnectionManager) createManagersPerTenant(secretName string, secretNamespace string,
secretsDirectory string, client clientset.Interface) (*cm.CredentialManager, *k8s.InformerManager) {

var informMgr *k8s.InformerManager
var lister listerv1.SecretLister
if client != nil && secretsDirectory == "" {
informMgr = k8s.NewInformer(client, true)
lister = informMgr.GetSecretLister()
}

credMgr := cm.NewCredentialManager(secretName, secretNamespace, secretsDirectory, lister)

if lister != nil {
informMgr.Listen()
}

return credMgr, informMgr
}

// Connect connects to vCenter with existing credentials
// If credentials are invalid:
// 1. It will fetch credentials from credentialManager
Expand Down
4 changes: 0 additions & 4 deletions pkg/common/connectionmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
clientset "k8s.io/client-go/kubernetes"
vcfg "k8s.io/cloud-provider-vsphere/pkg/common/config"
cm "k8s.io/cloud-provider-vsphere/pkg/common/credentialmanager"
k8s "k8s.io/cloud-provider-vsphere/pkg/common/kubernetes"
vclib "k8s.io/cloud-provider-vsphere/pkg/common/vclib"
)

Expand All @@ -38,9 +37,6 @@ type ConnectionManager struct {
// CredentialManager per VC
// The global CredentialManager will have an entry in this map with the key of "Global"
credentialManagers map[string]*cm.CredentialManager
// InformerManagers per VC
// The global InformerManager will have an entry in this map with the key of "Global"
informerManagers map[string]*k8s.InformerManager
}

// VSphereInstance represents a vSphere instance where one or more kubernetes nodes are running.
Expand Down
86 changes: 0 additions & 86 deletions pkg/common/kubernetes/informers.go

This file was deleted.

41 changes: 0 additions & 41 deletions pkg/common/kubernetes/types.go

This file was deleted.