Skip to content

Commit af5a756

Browse files
authored
Reduce ClusterPermission controller reconcile rate (#77)
Signed-off-by: fxiang1 <[email protected]>
1 parent 0178191 commit af5a756

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

controllers/clusterpermission_controller.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,17 @@ func (r *ClusterPermissionReconciler) Reconcile(ctx context.Context, req ctrl.Re
287287
return ctrl.Result{}, err
288288
}
289289
} else if err == nil {
290-
log.Info("updating ManifestWork")
291-
mw.Spec = manifestWork.Spec
292-
err = r.Client.Update(ctx, &mw)
293-
if err != nil {
294-
log.Error(err, "unable to update ManifestWork")
295-
return ctrl.Result{}, err
290+
// Only update if the spec has actually changed
291+
if !equality.Semantic.DeepEqual(mw.Spec, manifestWork.Spec) {
292+
log.Info("updating ManifestWork - spec has changed")
293+
mw.Spec = manifestWork.Spec
294+
err = r.Client.Update(ctx, &mw)
295+
if err != nil {
296+
log.Error(err, "unable to update ManifestWork")
297+
return ctrl.Result{}, err
298+
}
299+
} else {
300+
log.Info("ManifestWork spec unchanged, skipping update")
296301
}
297302
} else {
298303
log.Error(err, "unable to fetch ManifestWork")

controllers/managed_cluster_addon_informer.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewManagedClusterAddOnInformer(config *rest.Config, eventHandler func(obj *
8787
informer := cache.NewSharedIndexInformer(
8888
listWatcher,
8989
&addonv1alpha1.ManagedClusterAddOn{},
90-
time.Minute*10, // resync period
90+
time.Hour*10, // resync period - ManagedClusterAddOn namespaces change infrequently
9191
cache.Indexers{},
9292
)
9393

@@ -117,10 +117,31 @@ func (i *ManagedClusterAddOnInformer) Start() error {
117117
// no-op
118118
},
119119
UpdateFunc: func(oldObj, newObj any) {
120-
if addon, ok := newObj.(*addonv1alpha1.ManagedClusterAddOn); ok {
121-
i.enqueueAddon(addon)
122-
} else {
120+
oldAddon, oldOk := oldObj.(*addonv1alpha1.ManagedClusterAddOn)
121+
newAddon, newOk := newObj.(*addonv1alpha1.ManagedClusterAddOn)
122+
123+
if !newOk {
123124
i.logger.Error(nil, "newObj is not a ManagedClusterAddOn", "object", newObj)
125+
return
126+
}
127+
128+
if !oldOk {
129+
i.logger.Error(nil, "oldObj is not a ManagedClusterAddOn", "object", oldObj)
130+
return
131+
}
132+
133+
// Only enqueue if the addon's Status.Namespace or Spec.InstallNamespace has changed
134+
// These are the fields the controller cares about for generating subjects
135+
if oldAddon.Status.Namespace != newAddon.Status.Namespace ||
136+
oldAddon.Spec.InstallNamespace != newAddon.Spec.InstallNamespace {
137+
i.logger.Info("ManagedClusterAddOn namespace changed, enqueuing for reconciliation",
138+
"namespace", newAddon.Namespace,
139+
"name", newAddon.Name,
140+
"oldStatusNamespace", oldAddon.Status.Namespace,
141+
"newStatusNamespace", newAddon.Status.Namespace,
142+
"oldInstallNamespace", oldAddon.Spec.InstallNamespace,
143+
"newInstallNamespace", newAddon.Spec.InstallNamespace)
144+
i.enqueueAddon(newAddon)
124145
}
125146
},
126147
DeleteFunc: func(obj any) {

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ func main() {
137137
RetryPeriod: &options.LeaderElectionRetryPeriod,
138138
// Memory optimization: Configure cache options
139139
Cache: cache.Options{
140-
// Reduce default resync period to save memory
141-
SyncPeriod: &[]time.Duration{10 * time.Minute}[0],
140+
// Use a longer resync period for resources that change infrequently
141+
// Controller-runtime default is 10 hours, which is appropriate for this use case
142+
SyncPeriod: &[]time.Duration{10 * time.Hour}[0],
142143
// Configure cache size limits
143144
DefaultNamespaces: map[string]cache.Config{
144145
// Only cache specific namespaces if needed

0 commit comments

Comments
 (0)