Skip to content

Commit

Permalink
chore: add log about ignoring unrecoverable error
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Oct 24, 2024
1 parent 62eb3f5 commit a801008
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
36 changes: 17 additions & 19 deletions controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -167,14 +168,9 @@ func Create[
}

case errors.As(err, &errSDK):
switch {
case ErrorIsSDKErrorTypeField(err):
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToCreateReason, errSDK.Error())
default:
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToCreateReason, err.Error())
}

SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToCreateReason, errSDK.Error())
case errors.As(err, &errRelationsFailed):
e.SetKonnectID(errRelationsFailed.KonnectID)
SetKonnectEntityProgrammedConditionFalse(e, errRelationsFailed.Reason, errRelationsFailed.Err.Error())
case err != nil:
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToCreateReason, err.Error())
Expand All @@ -184,7 +180,7 @@ func Create[

logOpComplete(ctx, start, CreateOp, e, err)

return e, IgnoreUnrecoverableAPIErr(err)
return e, IgnoreUnrecoverableAPIErr(err, loggerForEntity(ctx, e, CreateOp))
}

// Delete deletes a Konnect entity.
Expand Down Expand Up @@ -379,12 +375,7 @@ func Update[
)
switch {
case errors.As(err, &errSDK):
switch {
case ErrorIsSDKErrorTypeField(err):
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToUpdateReason, errSDK.Body)
default:
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToUpdateReason, err.Error())
}
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToUpdateReason, errSDK.Body)
case errors.As(err, &errRelationsFailed):
e.SetKonnectID(errRelationsFailed.KonnectID)
SetKonnectEntityProgrammedConditionFalse(e, errRelationsFailed.Reason, err.Error())
Expand All @@ -396,24 +387,31 @@ func Update[

logOpComplete(ctx, now, UpdateOp, e, err)

return ctrl.Result{}, IgnoreUnrecoverableAPIErr(err)
return ctrl.Result{}, IgnoreUnrecoverableAPIErr(err, loggerForEntity(ctx, e, UpdateOp))
}

func logOpComplete[
func loggerForEntity[
T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T],
](ctx context.Context, start time.Time, op Op, e TEnt, err error) {
](ctx context.Context, e TEnt, op Op) logr.Logger {
keysAndValues := []interface{}{
"op", op,
"duration", time.Since(start).String(),
}

// Only add the Konnect ID if it exists and it's a create operation.
// Otherwise the Konnect ID is already set in the logger.
if id := e.GetKonnectStatus().GetKonnectID(); id != "" && op == CreateOp {
keysAndValues = append(keysAndValues, "konnect_id", id)
}
logger := ctrllog.FromContext(ctx).WithValues(keysAndValues...)
return ctrllog.FromContext(ctx).WithValues(keysAndValues...)
}

func logOpComplete[
T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T],
](ctx context.Context, start time.Time, op Op, e TEnt, err error) {
logger := loggerForEntity(ctx, e, op).
WithValues("duration", time.Since(start).String())

if err != nil {
// NOTE: We don't want to print stack trace information here so skip 99 frames
Expand Down
5 changes: 4 additions & 1 deletion controller/konnect/ops/ops_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"slices"

sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kong/gateway-operator/controller/konnect/constraints"
"github.com/kong/gateway-operator/controller/pkg/log"
)

// ErrNilResponse is an error indicating that a Konnect operation returned an empty response.
Expand Down Expand Up @@ -247,12 +249,13 @@ func handleDeleteError[

// IgnoreUnrecoverableAPIErr ignores unrecoverable errors that would cause the
// reconciler to endlessly requeue.
func IgnoreUnrecoverableAPIErr(err error) error {
func IgnoreUnrecoverableAPIErr(err error, logger logr.Logger) error {
// If the error is a type field error or bad request error, then don't propagate
// it to the caller.
// We cannot recover from this error as this requires user to change object's
// manifest. The entity's status is already updated with the error.
if ErrorIsSDKErrorTypeField(err) || ErrorIsSDKBadRequestError(err) {
log.Debug(logger, "ignoring unrecoverable API error, consult object's status for details", "err", err)
return nil
}

Expand Down

0 comments on commit a801008

Please sign in to comment.