diff --git a/manager/pkg/processes/hubmanagement/hub_addon_controller.go b/manager/pkg/processes/hubmanagement/hub_addon_controller.go index 326000b4f..1331b78cf 100644 --- a/manager/pkg/processes/hubmanagement/hub_addon_controller.go +++ b/manager/pkg/processes/hubmanagement/hub_addon_controller.go @@ -51,18 +51,28 @@ func AddManagedClusterAddonController(mgr ctrl.Manager) error { } func (c *managerClusterAddonController) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) { + if hubStatusManager == nil { + log.Warn("The hub management process should be started") + return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, nil + } + addon := &addonv1alpha1.ManagedClusterAddOn{} if err := c.client.Get(ctx, request.NamespacedName, addon); apierrors.IsNotFound(err) { - return ctrl.Result{}, nil + if request.Namespace == "" { + return ctrl.Result{}, nil + } + + log.Infof("inactive the agent when the global hub addon(%s) is deleted", request.Namespace) + err := hubStatusManager.inactive(ctx, []models.LeafHubHeartbeat{{ + Name: request.Namespace, + }}) + return ctrl.Result{}, err } else if err != nil { return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, fmt.Errorf("failed to get addon: %w", err) } if !addon.DeletionTimestamp.IsZero() { - if hubStatusManager == nil { - log.Warn("The hub management process should be started") - return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, nil - } + log.Infof("inactive the agent when the global hub addon(%s) is deleting", addon.Namespace) err := hubStatusManager.inactive(ctx, []models.LeafHubHeartbeat{{ Name: request.Namespace, }}) diff --git a/manager/pkg/processes/hubmanagement/hub_addon_controller_test.go b/manager/pkg/processes/hubmanagement/hub_addon_controller_test.go index 5429d1563..1b7ead672 100644 --- a/manager/pkg/processes/hubmanagement/hub_addon_controller_test.go +++ b/manager/pkg/processes/hubmanagement/hub_addon_controller_test.go @@ -28,7 +28,7 @@ func (m *mockHubManagement) reactive(ctx context.Context, hubs []models.LeafHubH return nil } -func TestManagerClusterAddonController_Reconcile(t *testing.T) { +func TestManagerClusterAddonReconcile(t *testing.T) { scheme := runtime.NewScheme() err := addonv1alpha1.AddToScheme(scheme) assert.NoError(t, err) @@ -40,7 +40,7 @@ func TestManagerClusterAddonController_Reconcile(t *testing.T) { client: fakeClient, } - t.Run("addon not found", func(t *testing.T) { + t.Run("hub manager is not ready", func(t *testing.T) { request := ctrl.Request{ NamespacedName: types.NamespacedName{ Namespace: "test-namespace", @@ -50,7 +50,7 @@ func TestManagerClusterAddonController_Reconcile(t *testing.T) { res, err := controller.Reconcile(ctx, request) assert.NoError(t, err) - assert.Equal(t, ctrl.Result{}, res) + assert.Equal(t, ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, res) }) t.Run("addon with deletion timestamp", func(t *testing.T) {