Skip to content

Commit

Permalink
Add logic to reconcile multiple RolloutMangers
Browse files Browse the repository at this point in the history
Signed-off-by: Rizwana777 <[email protected]>
  • Loading branch information
Rizwana777 committed Jun 12, 2024
1 parent b3e573f commit 3d1d32b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 20 deletions.
51 changes: 31 additions & 20 deletions controllers/argorollouts_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,41 @@ func (r *RolloutManagerReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
}

// Next, fetch and reconcile the RolloutManager instance
rolloutManager := &rolloutsmanagerv1alpha1.RolloutManager{}
if err := r.Client.Get(ctx, req.NamespacedName, rolloutManager); err != nil {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
// Fetch all RolloutManager instances in the namespace
rolloutsmanagerList := rolloutsmanagerv1alpha1.RolloutManagerList{}
if err := r.List(ctx, &rolloutsmanagerList, &client.ListOptions{Namespace: req.Namespace}); err != nil {
reqLogger.Error(err, "Failed to list RolloutManager instances")
return ctrl.Result{}, err
}

res, reconcileErr := r.reconcileRolloutsManager(ctx, *rolloutManager)
// Iterate through the list and reconcile each RolloutManager instance
for _, rolloutManager := range rolloutsmanagerList.Items {

// Fetch the RolloutManager instance
if err := r.Client.Get(ctx, client.ObjectKeyFromObject(&rolloutManager), &rolloutManager); err != nil {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}

// Set the condition/phase on the RolloutManager status (before we check the error from reconcileRolloutManager, below)
if err := updateStatusConditionOfRolloutManager(ctx, res, rolloutManager, r.Client, log); err != nil {
log.Error(err, "unable to update status of RolloutManager")
return reconcile.Result{}, err
}
// Reconcile the RolloutManager instance
res, reconcileErr := r.reconcileRolloutsManager(ctx, rolloutManager)

// Next return the reconcileErr if applicable
if reconcileErr != nil {
return reconcile.Result{}, reconcileErr
// Set the condition/phase on the RolloutManager status
if err := updateStatusConditionOfRolloutManager(ctx, res, &rolloutManager, r.Client, log); err != nil {
log.Error(err, "Unable to update status of RolloutManager")
return reconcile.Result{}, err
}

// Check the reconcileErr and handle it
if reconcileErr != nil {
return reconcile.Result{}, reconcileErr
}
}

return reconcile.Result{}, nil
Expand Down
16 changes: 16 additions & 0 deletions controllers/argorollouts_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ var _ = Describe("RolloutManagerReconciler tests", func() {
rm.Status.Conditions[0].Reason == rolloutsmanagerv1alpha1.RolloutManagerReasonMultipleClusterScopedRolloutManager &&
rm.Status.Conditions[0].Message == UnsupportedRolloutManagerConfiguration &&
rm.Status.Conditions[0].Status == metav1.ConditionFalse).To(BeTrue())

By("1st RM: Delete 1st RolloutManager")
Expect(r.Client.Delete(ctx, rm)).To(Succeed())
Expect(r.Client.Get(ctx, types.NamespacedName{Name: rm.Name, Namespace: rm.Namespace}, rm)).ToNot(Succeed())

By("2nd RM: Reconcile 2nd RolloutManager's once again and check whether it has removed failed condition.")
res2, err = r.Reconcile(ctx, req2)
Expect(err).ToNot(HaveOccurred())
Expect(res2.Requeue).Should(BeFalse(), "reconcile should not requeue request")

By("2nd RM: Check if RolloutManager's Status.Conditions are set.")
Expect(r.Client.Get(ctx, types.NamespacedName{Name: rm2.Name, Namespace: rm2.Namespace}, rm2)).To(Succeed())
Expect(rm2.Status.Conditions[0].Type == rolloutsmanagerv1alpha1.RolloutManagerConditionType &&
rm2.Status.Conditions[0].Reason == rolloutsmanagerv1alpha1.RolloutManagerReasonSuccess &&
rm2.Status.Conditions[0].Message == "" &&
rm2.Status.Conditions[0].Status == metav1.ConditionTrue).To(BeTrue())
})
})

Expand Down
43 changes: 43 additions & 0 deletions tests/e2e/cluster-scoped/cluster_scoped_rollouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,48 @@ var _ = Describe("Cluster-scoped RolloutManager tests", func() {
By("1st RM: Create and validate Rollout in 3rd namespace.")
utils.ValidateArgoRolloutsResources(ctx, k8sClient, nsName2, 31002, 32002)
})

It("After creating 2 cluster-scoped RolloutManager in a namespace, delete 1st RolloutManager and verify it removes the Failed status of 2nd RolloutManager", func() {
By("1st RM: Create cluster-scoped RolloutManager in a namespace.")
rolloutsManagerCl, err := utils.CreateRolloutManager(ctx, k8sClient, "test-rollouts-manager-1", fixture.TestE2ENamespace, false)
Expect(err).ToNot(HaveOccurred())

By("1st RM: Verify that RolloutManager is successfully created.")
Eventually(rolloutsManagerCl, "1m", "1s").Should(rmFixture.HavePhase(rmv1alpha1.PhaseAvailable))

By("1st RM: Verify that Status.Condition is having success condition.")
Eventually(rolloutsManagerCl, "1m", "1s").Should(rmFixture.HaveSuccessCondition())

By("2nd RM: Create cluster-scoped RolloutManager in a namespace.")
rolloutsManagerCl2, err := utils.CreateRolloutManager(ctx, k8sClient, "test-rollouts-manager-2", fixture.TestE2ENamespace, false)
Expect(err).ToNot(HaveOccurred())

By("2nd RM: Verify that RolloutManager is not working.")
Eventually(rolloutsManagerCl2, "1m", "1s").Should(rmFixture.HavePhase(rmv1alpha1.PhaseFailure))

By("1st RM: Verify that Status.Condition is now having error message.")
Eventually(rolloutsManagerCl, "3m", "1s").Should(rmFixture.HaveCondition(
metav1.Condition{
Type: rmv1alpha1.RolloutManagerConditionType,
Status: metav1.ConditionFalse,
Reason: rmv1alpha1.RolloutManagerReasonMultipleClusterScopedRolloutManager,
Message: controllers.UnsupportedRolloutManagerConfiguration,
}))

By("2nd RM: Verify that Status.Condition is now having error message.")
Eventually(rolloutsManagerCl2, "3m", "1s").Should(rmFixture.HaveCondition(
metav1.Condition{
Type: rmv1alpha1.RolloutManagerConditionType,
Status: metav1.ConditionFalse,
Reason: rmv1alpha1.RolloutManagerReasonMultipleClusterScopedRolloutManager,
Message: controllers.UnsupportedRolloutManagerConfiguration,
}))

By("1st RM: Delete first RolloutManager.")
Expect(k8sClient.Delete(ctx, &rolloutsManagerCl)).To(Succeed())

By("2nd RM: Verify that Status.Condition is having success condition.")
Eventually(rolloutsManagerCl2, "1m", "1s").Should(rmFixture.HaveSuccessCondition())
})
})
})

0 comments on commit 3d1d32b

Please sign in to comment.