Skip to content

Commit

Permalink
Set Warning if NATS module is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
muralov committed Dec 6, 2023
1 parent 1da00f9 commit 17ad780
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
13 changes: 10 additions & 3 deletions internal/controller/operator/eventing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,10 @@ func (r *Reconciler) handleEventingReconcile(ctx context.Context,
// set state processing if not set yet
r.InitStateProcessing(eventing)
if eventing.Spec.Backend == nil {
return ctrl.Result{Requeue: true}, r.syncStatusForEmptyBackend(ctx, eventing, log)
return kctrl.Result{Requeue: true}, r.syncStatusForEmptyBackend(ctx,
operatorv1alpha1.ConditionReasonBackendNotSpecified,
operatorv1alpha1.ConditionBackendNotSpecifiedMessage,
eventing, log)
}

// sync webhooks CABundle.
Expand Down Expand Up @@ -523,9 +526,13 @@ func (r *Reconciler) reconcileNATSBackend(ctx context.Context, eventing *operato
// into CrashLoopBackOff.
log.Infof("NATS module not enabled, deleting publisher proxy resources")
delErr := r.eventingManager.DeletePublisherProxyResources(ctx, eventing)
if delErr != nil {
return kctrl.Result{}, delErr
}
// update the Eventing CR status.
notFoundErr := fmt.Errorf("NATS module has to be installed: %v", err)
return kctrl.Result{}, errors.Join(r.syncStatusWithNATSErr(ctx, eventing, notFoundErr, log), delErr)
return kctrl.Result{}, r.syncStatusWithNATSState(ctx, operatorv1alpha1.StateWarning, eventing,
notFoundErr, log)
}
return kctrl.Result{}, err
}
Expand All @@ -537,7 +544,7 @@ func (r *Reconciler) reconcileNATSBackend(ctx context.Context, eventing *operato
// check nats CR if it exists and is in natsAvailable state
err = r.checkNATSAvailability(ctx, eventing)
if err != nil {
return kctrl.Result{}, r.syncStatusWithNATSErr(ctx, eventing, err, log)
return kctrl.Result{}, r.syncStatusWithNATSState(ctx, operatorv1alpha1.StateWarning, eventing, err, log)
}

// set NATSAvailable condition to true and update status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Test_CreateEventingCR_NATS(t *testing.T) {
),
givenNATSReady: false,
wantMatches: gomega.And(
matchers.HaveStatusError(),
matchers.HaveStatusWarning(),
matchers.HaveNATSNotAvailableCondition(),
matchers.HaveFinalizer(),
),
Expand Down Expand Up @@ -146,7 +146,7 @@ func Test_CreateEventingCR_NATS(t *testing.T) {
),
givenNATSCRDMissing: true,
wantMatches: gomega.And(
matchers.HaveStatusError(),
matchers.HaveStatusWarning(),
matchers.HaveNATSNotAvailableConditionWith("NATS module has to be installed: "+
"customresourcedefinitions.apiextensions.k8s.io \"nats.operator.kyma-project.io\" not found"),
matchers.HaveFinalizer(),
Expand Down
19 changes: 12 additions & 7 deletions internal/controller/operator/eventing/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,29 @@ func (es *Reconciler) InitStateProcessing(eventing *operatorv1alpha1.Eventing) {
// syncStatusWithNATSErr syncs Eventing status and sets an error state.
// Returns the relevant error.
func (r *Reconciler) syncStatusWithNATSErr(ctx context.Context,
eventing *operatorv1alpha1.Eventing, err error, log *zap.SugaredLogger,
) error {
eventing *operatorv1alpha1.Eventing, err error, log *zap.SugaredLogger) error {
return r.syncStatusWithNATSState(ctx, operatorv1alpha1.StateError, eventing, err, log)
}

func (r *Reconciler) syncStatusWithNATSState(ctx context.Context, state string,
eventing *operatorv1alpha1.Eventing, err error, log *zap.SugaredLogger) error {
// Set error state in status
eventing.Status.SetStateError()
eventing.Status.UpdateConditionBackendAvailable(kmetav1.ConditionFalse, operatorv1alpha1.ConditionReasonNATSNotAvailable,
eventing.Status.State = state
eventing.Status.UpdateConditionBackendAvailable(kmetav1.ConditionFalse,
operatorv1alpha1.ConditionReasonNATSNotAvailable,
err.Error())

return errors.Join(err, r.syncEventingStatus(ctx, eventing, log))
}

func (r *Reconciler) syncStatusForEmptyBackend(ctx context.Context,
eventing *operatorv1alpha1.Eventing, log *zap.SugaredLogger) error {
func (r *Reconciler) syncStatusForEmptyBackend(ctx context.Context, reason operatorv1alpha1.ConditionReason,
message string, eventing *operatorv1alpha1.Eventing, log *zap.SugaredLogger) error {
// Set error state in status
eventing.Status.SetStateWarning()
eventing.Status.UpdateConditionBackendAvailable(
kmetav1.ConditionFalse,
operatorv1alpha1.ConditionReasonBackendNotSpecified,
operatorv1alpha1.ConditionBackendNotSpecifiedMessage)
message)
return r.syncEventingStatus(ctx, eventing, log)
}

Expand Down

0 comments on commit 17ad780

Please sign in to comment.