Skip to content

Commit

Permalink
Extract common error msgs from controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Mar 12, 2021
1 parent 6143683 commit 3e68f61
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
8 changes: 4 additions & 4 deletions controllers/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (r *BindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

rabbitClient, err := rabbitholeClient(ctx, r.Client, binding.Spec.RabbitmqClusterReference)
if err != nil {
logger.Error(err, "Failed to generate http rabbitClient")
logger.Error(err, failedGenerateRabbitClient)
return reconcile.Result{}, err
}

spec, err := json.Marshal(binding.Spec)
if err != nil {
logger.Error(err, "Failed to marshal binding spec")
logger.Error(err, failedMarshalSpec)
}

logger.Info("Start reconciling",
Expand All @@ -66,7 +66,7 @@ func (r *BindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, binding)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
return ctrl.Result{}, err
}
Expand All @@ -75,7 +75,7 @@ func (r *BindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, binding)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
logger.Info("Finished reconciling")

Expand Down
7 changes: 6 additions & 1 deletion controllers/common.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package controllers

const failedConditionsUpdateMsg = "Failed to update status conditions"
// common error messages shared across controllers
const (
failedConditionsUpdate = "Failed to update status conditions"
failedMarshalSpec = "Failed to marshal spec"
failedGenerateRabbitClient = "Failed to generate http rabbitClient"
)
8 changes: 4 additions & 4 deletions controllers/exchange_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (r *ExchangeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c

rabbitClient, err := rabbitholeClient(ctx, r.Client, exchange.Spec.RabbitmqClusterReference)
if err != nil {
logger.Error(err, "Failed to generate http rabbitClient")
logger.Error(err, failedGenerateRabbitClient)
return reconcile.Result{}, err
}

Expand All @@ -67,7 +67,7 @@ func (r *ExchangeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c

spec, err := json.Marshal(exchange.Spec)
if err != nil {
logger.Error(err, "Failed to marshal exchange spec")
logger.Error(err, failedMarshalSpec)
}

logger.Info("Start reconciling",
Expand All @@ -79,7 +79,7 @@ func (r *ExchangeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, exchange)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
return ctrl.Result{}, err
}
Expand All @@ -88,7 +88,7 @@ func (r *ExchangeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, exchange)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
logger.Info("Finished reconciling")

Expand Down
8 changes: 4 additions & 4 deletions controllers/policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

rabbitClient, err := rabbitholeClient(ctx, r.Client, policy.Spec.RabbitmqClusterReference)
if err != nil {
logger.Error(err, "Failed to generate http rabbit client")
logger.Error(err, failedGenerateRabbitClient)
}

if !policy.ObjectMeta.DeletionTimestamp.IsZero() {
Expand All @@ -67,7 +67,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

spec, err := json.Marshal(policy.Spec)
if err != nil {
logger.Error(err, "Failed to marshal policy spec")
logger.Error(err, failedMarshalSpec)
}

logger.Info("Start reconciling",
Expand All @@ -79,7 +79,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, policy)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
return ctrl.Result{}, err
}
Expand All @@ -88,7 +88,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, policy)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
logger.Info("Finished reconciling")

Expand Down
8 changes: 4 additions & 4 deletions controllers/queue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *QueueReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// create rabbitmq http rabbitClient
rabbitClient, err := rabbitholeClient(ctx, r.Client, q.Spec.RabbitmqClusterReference)
if err != nil {
logger.Error(err, "Failed to generate http rabbitClient")
logger.Error(err, failedGenerateRabbitClient)
return reconcile.Result{}, err
}

Expand All @@ -73,7 +73,7 @@ func (r *QueueReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl

queueSpec, err := json.Marshal(q.Spec)
if err != nil {
logger.Error(err, "Failed to marshal q spec")
logger.Error(err, failedMarshalSpec)
}

logger.Info("Start reconciling",
Expand All @@ -85,7 +85,7 @@ func (r *QueueReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, q)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
return ctrl.Result{}, err
}
Expand All @@ -94,7 +94,7 @@ func (r *QueueReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, q)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
logger.Info("Finished reconciling")

Expand Down
8 changes: 4 additions & 4 deletions controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.

rabbitClient, err := rabbitholeClient(ctx, r.Client, user.Spec.RabbitmqClusterReference)
if err != nil {
logger.Error(err, "Failed to generate http rabbitClient")
logger.Error(err, failedGenerateRabbitClient)
return reconcile.Result{}, err
}

Expand All @@ -77,7 +77,7 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.

spec, err := json.Marshal(user.Spec)
if err != nil {
logger.Error(err, "Failed to marshal binding spec")
logger.Error(err, failedMarshalSpec)
}

logger.Info("Start reconciling",
Expand All @@ -100,7 +100,7 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, user)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
return ctrl.Result{}, err
}
Expand All @@ -109,7 +109,7 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, user)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}

logger.Info("Finished reconciling")
Expand Down
8 changes: 4 additions & 4 deletions controllers/vhost_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *VhostReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl

rabbitClient, err := rabbitholeClient(ctx, r.Client, vhost.Spec.RabbitmqClusterReference)
if err != nil {
logger.Error(err, "Failed to generate http rabbitClient")
logger.Error(err, failedGenerateRabbitClient)
return reconcile.Result{}, err
}

Expand All @@ -59,7 +59,7 @@ func (r *VhostReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl

spec, err := json.Marshal(vhost.Spec)
if err != nil {
logger.Error(err, "Failed to marshal vhost spec")
logger.Error(err, failedMarshalSpec)
}

logger.Info("Start reconciling",
Expand All @@ -71,7 +71,7 @@ func (r *VhostReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, vhost)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
return ctrl.Result{}, err
}
Expand All @@ -80,7 +80,7 @@ func (r *VhostReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if writerErr := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
return r.Status().Update(ctx, vhost)
}); writerErr != nil {
logger.Error(writerErr, failedConditionsUpdateMsg)
logger.Error(writerErr, failedConditionsUpdate)
}
logger.Info("Finished reconciling")

Expand Down

0 comments on commit 3e68f61

Please sign in to comment.