From 0c7c40c49f3dcc0b8d7b115ab12c89c9dcde6729 Mon Sep 17 00:00:00 2001 From: zhujian Date: Fri, 29 Nov 2024 14:54:37 +0800 Subject: [PATCH] Wait for cache sync for lister resources Signed-off-by: zhujian --- .../controllers/addonconfig/controller.go | 2 ++ .../controllers/agentdeploy/controller.go | 12 ++++++------ .../controllers/certificate/csrapprove.go | 2 ++ .../controllers/registration/controller.go | 2 ++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/addonmanager/controllers/addonconfig/controller.go b/pkg/addonmanager/controllers/addonconfig/controller.go index 68718580..2a39dff4 100644 --- a/pkg/addonmanager/controllers/addonconfig/controller.go +++ b/pkg/addonmanager/controllers/addonconfig/controller.go @@ -71,6 +71,8 @@ func NewAddonConfigController( return []string{key} }, addonInformers.Informer()). WithBareInformers(configInformers...). + // clusterManagementAddonLister is used, so wait for cache sync + WithBareInformers(clusterManagementAddonInformers.Informer()). WithSync(c.sync).ToController(controllerName) } diff --git a/pkg/addonmanager/controllers/agentdeploy/controller.go b/pkg/addonmanager/controllers/agentdeploy/controller.go index 23018b0c..550d80a5 100644 --- a/pkg/addonmanager/controllers/agentdeploy/controller.go +++ b/pkg/addonmanager/controllers/agentdeploy/controller.go @@ -76,6 +76,8 @@ func NewAddonDeployController( agentAddons: agentAddons, } + c.setClusterInformerHandler(clusterInformers) + f := factory.New().WithSyncContext(syncCtx). WithFilteredEventsInformersQueueKeysFunc( func(obj runtime.Object) []string { @@ -126,15 +128,13 @@ func NewAddonDeployController( }, workInformers.Informer(), ). + WithBareInformers(clusterInformers.Informer()). WithSync(c.sync) - if c.watchManagedCluster(clusterInformers) { - f.WithBareInformers(clusterInformers.Informer()) - } return f.ToController(controllerName) } -func (c addonDeployController) watchManagedCluster(clusterInformers clusterinformers.ManagedClusterInformer) bool { +func (c addonDeployController) setClusterInformerHandler(clusterInformers clusterinformers.ManagedClusterInformer) { var filters []func(old, new *clusterv1.ManagedCluster) bool for _, addon := range c.agentAddons { if addon.GetAgentAddonOptions().AgentDeployTriggerClusterFilter != nil { @@ -142,7 +142,7 @@ func (c addonDeployController) watchManagedCluster(clusterInformers clusterinfor } } if len(filters) == 0 { - return false + return } _, err := clusterInformers.Informer().AddEventHandler( @@ -169,8 +169,8 @@ func (c addonDeployController) watchManagedCluster(clusterInformers clusterinfor if err != nil { utilruntime.HandleError(err) } - return true } + func (c *addonDeployController) enqueueAddOnsByCluster() func(obj interface{}) { return func(obj interface{}) { accessor, _ := meta.Accessor(obj) diff --git a/pkg/addonmanager/controllers/certificate/csrapprove.go b/pkg/addonmanager/controllers/certificate/csrapprove.go index 91d52dcf..29372d04 100644 --- a/pkg/addonmanager/controllers/certificate/csrapprove.go +++ b/pkg/addonmanager/controllers/certificate/csrapprove.go @@ -109,6 +109,8 @@ func NewCSRApprovingController( return true }, csrInformer). + // clusterLister and addonLister are used, so wait for cache sync + WithBareInformers(clusterInformers.Informer(), addonInformers.Informer()). WithSync(c.sync). ToController("CSRApprovingController") } diff --git a/pkg/addonmanager/controllers/registration/controller.go b/pkg/addonmanager/controllers/registration/controller.go index a3833256..1f98be33 100644 --- a/pkg/addonmanager/controllers/registration/controller.go +++ b/pkg/addonmanager/controllers/registration/controller.go @@ -58,6 +58,8 @@ func NewAddonRegistrationController( return true }, addonInformers.Informer()). + // clusterLister is used, so wait for cache sync + WithBareInformers(clusterInformers.Informer()). WithSync(c.sync).ToController("addon-registration-controller") }