Skip to content

Commit

Permalink
Merge pull request #87 from banzaicloud/native-reconciler-retryonerror
Browse files Browse the repository at this point in the history
support retry on resource reconcile error in native reconciler
  • Loading branch information
waynz0r authored Nov 19, 2021
2 parents c8de0b1 + 5859139 commit 04dbff6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
25 changes: 24 additions & 1 deletion pkg/reconciler/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -155,6 +156,8 @@ type NativeReconciler struct {
setControllerRef bool
reconciledObjectStates map[reconciledObjectState][]runtime.Object
waitBackoff *wait.Backoff
retryBackoff wait.Backoff
retriableErrorFunc func(error) bool
objectModifiers []resources.ObjectModifierWithParentFunc
}

Expand Down Expand Up @@ -190,6 +193,18 @@ func NativeReconcilerWithModifier(modifierFunc resources.ObjectModifierWithParen
}
}

func NativeReconcilerWithRetryBackoff(backoff wait.Backoff) NativeReconcilerOpt {
return func(r *NativeReconciler) {
r.retryBackoff = backoff
}
}

func NativeReconcilerWithRetriableErrorFunc(retriableErrorFunc func(error) bool) NativeReconcilerOpt {
return func(r *NativeReconciler) {
r.retriableErrorFunc = retriableErrorFunc
}
}

func NewNativeReconcilerWithDefaults(
component string,
client client.Client,
Expand Down Expand Up @@ -236,6 +251,10 @@ func NewNativeReconciler(
reconciledComponent: reconciledComponent,
configTranslate: resourceTranslate,
componentName: componentName,

// do not retry on errors by default
retriableErrorFunc: func(error) bool { return false },
retryBackoff: retry.DefaultRetry,
}

reconciler.initReconciledObjectStates()
Expand Down Expand Up @@ -319,7 +338,11 @@ LOOP:
}

var result *reconcile.Result
result, err = rec.ReconcileResource(o, state)
err = retry.OnError(rec.retryBackoff, rec.retriableErrorFunc, func() error {
var err error
result, err = rec.ReconcileResource(o, state)
return err
})
if err == nil {
resourceID, err := rec.generateResourceID(o)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

type Backoff wait.Backoff
type Backoff = wait.Backoff

type ResourceConditionChecks struct {
client client.Client
Expand Down Expand Up @@ -69,7 +69,6 @@ func (c *ResourceConditionChecks) WaitForCustomConditionChecks(id string, checkF

return true, nil
})

if err != nil {
return err
}
Expand Down

0 comments on commit 04dbff6

Please sign in to comment.