Skip to content

Commit

Permalink
fix: panic in case of Coralogix API returns an error (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasv authored Sep 5, 2023
1 parent f0392db commit c6f7e00
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
45 changes: 26 additions & 19 deletions controllers/alphacontrollers/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,33 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return ctrl.Result{}, nil
}

var notFount bool
var err error
var actualState *coralogixv1alpha1.AlertStatus
var flattenErr error

var (
notFound bool
err error
actualState *coralogixv1alpha1.AlertStatus
)
if id := alertCRD.Status.ID; id == nil {
log.V(1).Info("alert wasn't created")
notFount = true
} else if getAlertResp, err := alertsClient.GetAlert(ctx, &alerts.GetAlertByUniqueIdRequest{Id: wrapperspb.String(*id)}); status.Code(err) == codes.NotFound {
log.V(1).Info("alert doesn't exist in Coralogix backend", "ID", id)
notFount = true
} else if err == nil {
actualState, flattenErr = flattenAlert(ctx, getAlertResp.GetAlert(), alertCRD.Spec)
if flattenErr != nil {
log.Error(flattenErr, "Received an error while flattened Alert")
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, flattenErr
notFound = true
} else {
getAlertResp, err := alertsClient.GetAlert(ctx, &alerts.GetAlertByUniqueIdRequest{Id: wrapperspb.String(*id)})
switch {
case status.Code(err) == codes.NotFound:
log.V(1).Info("alert doesn't exist in Coralogix backend", "ID", id)
notFound = true
case err != nil:
log.Error(err, "Received an error while getting Alert")
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, err
case err == nil:
actualState, err = flattenAlert(ctx, getAlertResp.GetAlert(), alertCRD.Spec)
if err != nil {
log.Error(err, "Received an error while flattened Alert")
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, err
}
}
}

if notFount {
if notFound {
if alertCRD.Spec.Labels == nil {
alertCRD.Spec.Labels = make(map[string]string)
}
Expand Down Expand Up @@ -196,10 +203,10 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, err
}

actualState, flattenErr = flattenAlert(ctx, createAlertResp.GetAlert(), alertCRD.Spec)
if flattenErr != nil {
log.Error(flattenErr, "Received an error while flattened Alert")
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, flattenErr
actualState, err = flattenAlert(ctx, createAlertResp.GetAlert(), alertCRD.Spec)
if err != nil {
log.Error(err, "Received an error while flattened Alert")
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, err
}
alertCRD.Status = *actualState
if err := r.Status().Update(ctx, alertCRD); err != nil {
Expand Down
23 changes: 14 additions & 9 deletions controllers/alphacontrollers/rulegroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,21 @@ func (r *RuleGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if id := ruleGroupCRD.Status.ID; id == nil {
log.V(1).Info("ruleGroup wasn't created")
notFound = true
} else if getRuleGroupResp, err := rulesGroupsClient.GetRuleGroup(ctx, &rulesgroups.GetRuleGroupRequest{GroupId: *id}); status.Code(err) == codes.NotFound {
// TODO: this needs more error handling, what if it returns internal error?

log.V(1).Info("ruleGroup doesn't exist in Coralogix backend")
notFound = true
} else if err == nil {
actualState, err = flattenRuleGroup(getRuleGroupResp.GetRuleGroup())
if err != nil {
log.Error(err, "Error mapping coralogix API response", "Name", ruleGroupCRD.Name, "Namespace", ruleGroupCRD.Namespace)
} else {
getRuleGroupResp, err := rulesGroupsClient.GetRuleGroup(ctx, &rulesgroups.GetRuleGroupRequest{GroupId: *id})
switch {
case status.Code(err) == codes.NotFound:
log.V(1).Info("ruleGroup doesn't exist in Coralogix backend")
notFound = true
case err != nil:
log.Error(err, "Received an error while getting RuleGroup")
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, err
case err == nil:
actualState, err = flattenRuleGroup(getRuleGroupResp.GetRuleGroup())
if err != nil {
log.Error(err, "Error mapping coralogix API response", "Name", ruleGroupCRD.Name, "Namespace", ruleGroupCRD.Namespace)
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, err
}
}
}

Expand Down

0 comments on commit c6f7e00

Please sign in to comment.