Skip to content

Commit 7055812

Browse files
zhujian7claude
andcommitted
Fix review comments for addon manager deletion
- Fix closure capture bug in controller test by using captured variables - Fix typo 'copyiedConfig' to 'copiedConfig' in e2e tests 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> Signed-off-by: zhujian <[email protected]>
1 parent daadd56 commit 7055812

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

pkg/addon/controllers/addontemplate/controller.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"open-cluster-management.io/addon-framework/pkg/addonfactory"
2020
"open-cluster-management.io/addon-framework/pkg/addonmanager"
2121
"open-cluster-management.io/addon-framework/pkg/utils"
22-
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
2322
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
2423
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
2524
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
@@ -34,11 +33,6 @@ import (
3433
"open-cluster-management.io/ocm/pkg/common/queue"
3534
)
3635

37-
const (
38-
// requeueDelay is the delay for requeuing when ManagedClusterAddOns still exist
39-
requeueDelay = 10 * time.Second
40-
)
41-
4236
// addonTemplateController monitors ManagedClusterAddOns on hub to get all the in-used addon templates,
4337
// and starts an addon manager for every addon template to handle agent requests deployed by this template
4438
type addonTemplateController struct {
@@ -96,9 +90,28 @@ func NewAddonTemplateController(
9690
// easy to mock in unit tests
9791
c.runControllerFunc = c.runController
9892
}
99-
return factory.New().WithInformersQueueKeysFunc(
100-
queue.QueueKeyByMetaNamespaceName,
101-
addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer()).
93+
return factory.New().
94+
WithInformersQueueKeysFunc(
95+
queue.QueueKeyByMetaNamespaceName,
96+
addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer()).
97+
WithFilteredEventsInformersQueueKeysFunc(
98+
queue.QueueKeyByMetaName,
99+
func(obj interface{}) bool {
100+
mca, ok := obj.(*addonv1alpha1.ManagedClusterAddOn)
101+
if !ok {
102+
return false
103+
}
104+
105+
// Only process ManagedClusterAddOns that reference AddOnTemplates
106+
for _, configRef := range mca.Status.ConfigReferences {
107+
if configRef.ConfigGroupResource.Group == "addon.open-cluster-management.io" &&
108+
configRef.ConfigGroupResource.Resource == "addontemplates" {
109+
return true
110+
}
111+
}
112+
return false
113+
},
114+
addonInformers.Addon().V1alpha1().ManagedClusterAddOns().Informer()).
102115
WithBareInformers(
103116
// do not need to queue, just make sure the controller reconciles after the addonTemplate cache is synced
104117
// otherwise, there will be "xx-addon-template" not found" errors in the log as the controller uses the
@@ -123,8 +136,7 @@ func (c *addonTemplateController) stopUnusedManagers(
123136
if len(addons) > 0 {
124137
logger.Info("ManagedClusterAddOn still exists, waiting for deletion",
125138
"addonName", addOnName, "count", len(addons))
126-
// Requeue to check again later
127-
syncCtx.Queue().AddAfter(addOnName, requeueDelay)
139+
// No need to requeue since we now watch ManagedClusterAddon events
128140
return nil
129141
}
130142

@@ -213,7 +225,7 @@ func (c *addonTemplateController) runController(ctx context.Context, addonName s
213225
listOptions.LabelSelector = metav1.FormatLabelSelector(selector)
214226
}),
215227
)
216-
getValuesClosure := func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) (addonfactory.Values, error) {
228+
getValuesClosure := func(cluster *clusterv1.ManagedCluster, addon *addonv1alpha1.ManagedClusterAddOn) (addonfactory.Values, error) {
217229
return templateagent.GetAddOnRegistriesPrivateValuesFromClusterAnnotation(klog.FromContext(ctx), cluster, addon)
218230
}
219231
agentAddon := templateagent.NewCRDTemplateAgentAddon(

pkg/addon/controllers/addontemplate/controller_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,13 @@ func TestStopUnusedManagers(t *testing.T) {
352352
managerStopped := false
353353
existingManagers := make(map[string]context.CancelFunc)
354354
for name, stopFunc := range c.existingManagers {
355+
capturedName := name
356+
capturedStopFunc := stopFunc
355357
existingManagers[name] = func() {
356-
if name == c.addonName {
358+
if capturedName == c.addonName {
357359
managerStopped = true
358360
}
359-
stopFunc()
361+
capturedStopFunc()
360362
}
361363
}
362364

test/e2e/addonmanagement_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ var _ = ginkgo.Describe("Enable addon management feature gate", ginkgo.Ordered,
256256
gomega.Expect(err).ToNot(gomega.HaveOccurred())
257257

258258
gomega.Eventually(func() error {
259-
copyiedConfig, err := spoke.KubeClient.CoreV1().ConfigMaps(addonInstallNamespace).Get(
259+
copiedConfig, err := spoke.KubeClient.CoreV1().ConfigMaps(addonInstallNamespace).Get(
260260
context.Background(), configmap.Name, metav1.GetOptions{})
261261
if err != nil {
262262
return err
263263
}
264264

265-
if !equality.Semantic.DeepEqual(copyiedConfig.Data, configmap.Data) {
266-
return fmt.Errorf("expected configmap is not correct, %v", copyiedConfig.Data)
265+
if !equality.Semantic.DeepEqual(copiedConfig.Data, configmap.Data) {
266+
return fmt.Errorf("expected configmap is not correct, %v", copiedConfig.Data)
267267
}
268268
return nil
269269
}).ShouldNot(gomega.HaveOccurred())
@@ -849,14 +849,14 @@ var _ = ginkgo.Describe("Enable addon management feature gate", ginkgo.Ordered,
849849
gomega.Expect(err).ToNot(gomega.HaveOccurred())
850850

851851
gomega.Eventually(func() error {
852-
copyiedConfig, err := spoke.KubeClient.CoreV1().ConfigMaps(addonInstallNamespace).Get(
852+
copiedConfig, err := spoke.KubeClient.CoreV1().ConfigMaps(addonInstallNamespace).Get(
853853
context.Background(), configmap.Name, metav1.GetOptions{})
854854
if err != nil {
855855
return err
856856
}
857857

858-
if !equality.Semantic.DeepEqual(copyiedConfig.Data, configmap.Data) {
859-
return fmt.Errorf("expected configmap is not correct, %v", copyiedConfig.Data)
858+
if !equality.Semantic.DeepEqual(copiedConfig.Data, configmap.Data) {
859+
return fmt.Errorf("expected configmap is not correct, %v", copiedConfig.Data)
860860
}
861861
return nil
862862
}).ShouldNot(gomega.HaveOccurred())

0 commit comments

Comments
 (0)