Skip to content

Commit 07dc59a

Browse files
authored
Merge pull request #598 from rabbitmq/failed-set-plugins
Fail to set plugins (or any other necessary cli commands) should result in ReconcileSuccess set to false
2 parents c273d60 + cb758b9 commit 07dc59a

File tree

3 files changed

+25
-53
lines changed

3 files changed

+25
-53
lines changed

controllers/rabbitmqcluster_controller.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,28 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
186186
return ctrl.Result{RequeueAfter: requeueAfter}, err
187187
}
188188

189-
// Set ReconcileSuccess to true here because all CRUD operations to Kube API related
190-
// to child resources returned no error
191-
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionTrue, "Success", "Created or Updated all child resources")
192-
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
193-
logger.Error(writerErr, "Failed to Update Custom Resource status")
194-
}
195-
196189
if err := r.setDefaultUserStatus(ctx, rabbitmqCluster); err != nil {
197190
return ctrl.Result{}, err
198191
}
199192

200193
// By this point the StatefulSet may have finished deploying. Run any
201194
// post-deploy steps if so, or requeue until the deployment is finished.
202195
if requeueAfter, err := r.runRabbitmqCLICommandsIfAnnotated(ctx, rabbitmqCluster); err != nil || requeueAfter > 0 {
196+
if err != nil {
197+
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionFalse, "FailedCLICommand", err.Error())
198+
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
199+
logger.Error(writerErr, "Failed to update ReconcileSuccess condition state")
200+
}
201+
}
203202
return ctrl.Result{RequeueAfter: requeueAfter}, err
204203
}
205204

205+
// Set ReconcileSuccess to true after all reconciliation steps have finished with no error
206+
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionTrue, "Success", "Finish reconciling")
207+
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
208+
logger.Error(writerErr, "Failed to Update Custom Resource status")
209+
}
210+
206211
logger.Info("Finished reconciling")
207212

208213
return ctrl.Result{}, nil

controllers/rabbitmqcluster_controller_test.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -676,33 +676,6 @@ var _ = Describe("RabbitmqClusterController", func() {
676676
return "ReconcileSuccess status: condition not present"
677677
}, 5).Should(Equal("ReconcileSuccess status: False"))
678678
})
679-
680-
By("transitioning to True when a valid spec in updated", func() {
681-
// We have to Get() the CR again because Reconcile() changes the object
682-
// If we try to Update() without getting the latest version of the CR
683-
// We are very likely to hit a Conflict error
684-
Expect(client.Get(ctx, runtimeClient.ObjectKey{
685-
Name: crName,
686-
Namespace: defaultNamespace,
687-
}, cluster)).To(Succeed())
688-
cluster.Spec.Service.Annotations = map[string]string{"thisIs": "valid"}
689-
Expect(client.Update(ctx, cluster)).To(Succeed())
690-
691-
Eventually(func() string {
692-
someRabbit := &rabbitmqv1beta1.RabbitmqCluster{}
693-
Expect(client.Get(ctx, runtimeClient.ObjectKey{
694-
Name: crName,
695-
Namespace: defaultNamespace,
696-
}, someRabbit)).To(Succeed())
697-
698-
for i := range someRabbit.Status.Conditions {
699-
if someRabbit.Status.Conditions[i].Type == status.ReconcileSuccess {
700-
return fmt.Sprintf("ReconcileSuccess status: %s", someRabbit.Status.Conditions[i].Status)
701-
}
702-
}
703-
return "ReconcileSuccess status: condition not present"
704-
}, 5).Should(Equal("ReconcileSuccess status: True"))
705-
})
706679
})
707680
})
708681

controllers/reconcile_cli.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func (r *RabbitmqClusterReconciler) runRabbitmqCLICommandsIfAnnotated(ctx contex
2525
logger.Info("not all replicas ready yet; requeuing request to run RabbitMQ CLI commands")
2626
return 15 * time.Second, nil
2727
}
28-
2928
// Retrieve the plugins config map, if it exists.
3029
pluginsConfig, err := r.configMap(ctx, rmq, rmq.ChildResourceName(resource.PluginsConfigName))
3130
if client.IgnoreNotFound(err) != nil {
@@ -72,12 +71,10 @@ func (r *RabbitmqClusterReconciler) runEnableFeatureFlagsCommand(ctx context.Con
7271
cmd := "set -eo pipefail; rabbitmqctl -s list_feature_flags name state stability | (grep 'disabled\\sstable$' || true) | cut -f 1 | xargs -r -n1 rabbitmqctl enable_feature_flag"
7372
stdout, stderr, err := r.exec(rmq.Namespace, podName, "rabbitmq", "bash", "-c", cmd)
7473
if err != nil {
75-
logger.Error(err, "failed to enable all feature flags",
76-
"pod", podName,
77-
"command", cmd,
78-
"stdout", stdout,
79-
"stderr", stderr)
80-
return err
74+
msg := "failed to enable all feature flags on pod"
75+
logger.Error(err, msg, "pod", podName, "command", cmd, "stdout", stdout, "stderr", stderr)
76+
r.Recorder.Event(rmq, corev1.EventTypeWarning, "FailedReconcile", fmt.Sprintf("%s %s", msg, podName))
77+
return fmt.Errorf("%s %s: %v", msg, podName, err)
8178
}
8279
logger.Info("successfully enabled all feature flags")
8380
return r.deleteAnnotation(ctx, sts, stsCreateAnnotation)
@@ -95,29 +92,26 @@ func (r *RabbitmqClusterReconciler) runSetPluginsCommand(ctx context.Context, rm
9592
cmd := fmt.Sprintf("rabbitmq-plugins set %s", plugins.AsString(" "))
9693
stdout, stderr, err := r.exec(rmq.Namespace, podName, "rabbitmq", "sh", "-c", cmd)
9794
if err != nil {
98-
logger.Error(err, "failed to set plugins",
99-
"pod", podName,
100-
"command", cmd,
101-
"stdout", stdout,
102-
"stderr", stderr)
103-
return err
95+
msg := "failed to set plugins on pod"
96+
logger.Error(err, msg, "pod", podName, "command", cmd, "stdout", stdout, "stderr", stderr)
97+
r.Recorder.Event(rmq, corev1.EventTypeWarning, "FailedReconcile", fmt.Sprintf("%s %s", msg, podName))
98+
return fmt.Errorf("%s %s: %v", msg, podName, err)
10499
}
105100
}
106101
logger.Info("successfully set plugins")
107102
return r.deleteAnnotation(ctx, configMap, pluginsUpdateAnnotation)
108103
}
109104

110105
func (r *RabbitmqClusterReconciler) runQueueRebalanceCommand(ctx context.Context, rmq *rabbitmqv1beta1.RabbitmqCluster) error {
106+
logger := ctrl.LoggerFrom(ctx)
111107
podName := fmt.Sprintf("%s-0", rmq.ChildResourceName("server"))
112108
cmd := "rabbitmq-queues rebalance all"
113109
stdout, stderr, err := r.exec(rmq.Namespace, podName, "rabbitmq", "sh", "-c", cmd)
114110
if err != nil {
115-
ctrl.LoggerFrom(ctx).Error(err, "failed to run queue rebalance",
116-
"pod", podName,
117-
"command", cmd,
118-
"stdout", stdout,
119-
"stderr", stderr)
120-
return err
111+
msg := "failed to run queue rebalance on pod"
112+
logger.Error(err, msg, "pod", podName, "command", cmd, "stdout", stdout, "stderr", stderr)
113+
r.Recorder.Event(rmq, corev1.EventTypeWarning, "FailedReconcile", fmt.Sprintf("%s %s", msg, podName))
114+
return fmt.Errorf("%s %s: %v", msg, podName, err)
121115
}
122116
return r.deleteAnnotation(ctx, rmq, queueRebalanceAnnotation)
123117
}

0 commit comments

Comments
 (0)