diff --git a/pkg/controller/cluster/cluster_controller.go b/pkg/controller/cluster/cluster_controller.go index 3387e00cfa8c..e4430bd61554 100644 --- a/pkg/controller/cluster/cluster_controller.go +++ b/pkg/controller/cluster/cluster_controller.go @@ -19,7 +19,8 @@ package cluster import ( "context" - "k8s.io/apimachinery/pkg/api/errors" + "github.com/pkg/errors" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog" @@ -77,7 +78,7 @@ func (r *ReconcileCluster) Reconcile(request reconcile.Request) (reconcile.Resul cluster := &clusterv1alpha1.Cluster{} err := r.Get(context.Background(), request.NamespacedName, cluster) if err != nil { - if errors.IsNotFound(err) { + if apierrors.IsNotFound(err) { // Object not found, return. Created objects are automatically garbage collected. // For additional cleanup logic use finalizers. return reconcile.Result{}, nil @@ -137,11 +138,10 @@ func (r *ReconcileCluster) Reconcile(request reconcile.Request) (reconcile.Resul } klog.Infof("reconciling cluster object %v triggers idempotent reconcile.", name) - err = r.actuator.Reconcile(cluster) - if err != nil { - if requeueErr, ok := err.(*controllerError.RequeueAfterError); ok { - klog.Infof("Actuator returned requeue after error: %v", requeueErr) - return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.RequeueAfter}, nil + if err := r.actuator.Reconcile(cluster); err != nil { + if requeueErr, ok := errors.Cause(err).(controllerError.HasRequeueAfterError); ok { + klog.Infof("Actuator returned requeue-after error: %v", requeueErr) + return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil } klog.Errorf("Error reconciling cluster object %v; %v", name, err) return reconcile.Result{}, err diff --git a/pkg/controller/error/requeue_error.go b/pkg/controller/error/requeue_error.go index b1e424bf597d..a559529adbb4 100644 --- a/pkg/controller/error/requeue_error.go +++ b/pkg/controller/error/requeue_error.go @@ -21,6 +21,15 @@ import ( "time" ) +// HasRequeueAfterError represents that an actuator managed object should +// be requeued for further processing after the given RequeueAfter time has +// passed. +type HasRequeueAfterError interface { + // GetRequeueAfter gets the duration to wait until the managed object is + // requeued for further processing. + GetRequeueAfter() time.Duration +} + // RequeueAfterError represents that an actuator managed object should be // requeued for further processing after the given RequeueAfter time has // passed. @@ -32,3 +41,9 @@ type RequeueAfterError struct { func (e *RequeueAfterError) Error() string { return fmt.Sprintf("requeue in: %s", e.RequeueAfter) } + +// GetRequeueAfter gets the duration to wait until the managed object is +// requeued for further processing. +func (e *RequeueAfterError) GetRequeueAfter() time.Duration { + return e.RequeueAfter +} diff --git a/pkg/controller/machine/machine_controller.go b/pkg/controller/machine/machine_controller.go index bfd4c4f39047..3b29738ea4ac 100644 --- a/pkg/controller/machine/machine_controller.go +++ b/pkg/controller/machine/machine_controller.go @@ -20,6 +20,7 @@ import ( "context" "os" + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -169,9 +170,9 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul klog.Infof("Reconciling machine %q triggers delete", name) if err := r.actuator.Delete(ctx, cluster, m); err != nil { - if requeueErr, ok := err.(*controllerError.RequeueAfterError); ok { + if requeueErr, ok := errors.Cause(err).(controllerError.HasRequeueAfterError); ok { klog.Infof("Actuator returned requeue-after error: %v", requeueErr) - return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.RequeueAfter}, nil + return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil } klog.Errorf("Failed to delete machine %q: %v", name, err) @@ -206,9 +207,9 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul if exist { klog.Infof("Reconciling machine %q triggers idempotent update", name) if err := r.actuator.Update(ctx, cluster, m); err != nil { - if requeueErr, ok := err.(*controllerError.RequeueAfterError); ok { + if requeueErr, ok := errors.Cause(err).(controllerError.HasRequeueAfterError); ok { klog.Infof("Actuator returned requeue-after error: %v", requeueErr) - return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.RequeueAfter}, nil + return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil } klog.Errorf(`Error updating machine "%s/%s": %v`, m.Namespace, name, err) @@ -221,9 +222,9 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul // Machine resource created. Machine does not yet exist. klog.Infof("Reconciling machine object %v triggers idempotent create.", m.ObjectMeta.Name) if err := r.actuator.Create(ctx, cluster, m); err != nil { - if requeueErr, ok := err.(*controllerError.RequeueAfterError); ok { + if requeueErr, ok := errors.Cause(err).(controllerError.HasRequeueAfterError); ok { klog.Infof("Actuator returned requeue-after error: %v", requeueErr) - return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.RequeueAfter}, nil + return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil } klog.Warningf("Failed to create machine %q: %v", name, err)