From f0b7c2b1dd5ffb93752c8fcb980f490e0b13fefe Mon Sep 17 00:00:00 2001 From: Richard Kovacs Date: Tue, 30 Jan 2024 15:11:11 +0100 Subject: [PATCH] Fix Kube patch aggregate error handling --- controller/linodevpc_controller.go | 8 +++++--- util/helpers.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/controller/linodevpc_controller.go b/controller/linodevpc_controller.go index 6c1ca2edb..2c99e0047 100644 --- a/controller/linodevpc_controller.go +++ b/controller/linodevpc_controller.go @@ -74,9 +74,11 @@ func (r *LinodeVPCReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( linodeVPC := &infrav1.LinodeVPC{} if err := r.Client.Get(ctx, req.NamespacedName, linodeVPC); err != nil { - log.Error(err, "Failed to fetch LinodeVPC") + if err = client.IgnoreNotFound(err); err != nil { + log.Error(err, "Failed to fetch LinodeVPC") + } - return ctrl.Result{}, client.IgnoreNotFound(err) + return ctrl.Result{}, err } vpcScope, err := scope.NewVPCScope( @@ -118,7 +120,7 @@ func (r *LinodeVPCReconciler) reconcile( r.Recorder.Event(vpcScope.LinodeVPC, corev1.EventTypeWarning, string(failureReason), err.Error()) } - if patchErr := vpcScope.PatchHelper.Patch(ctx, vpcScope.LinodeVPC); patchErr != nil && client.IgnoreNotFound(patchErr) != nil { + if patchErr := vpcScope.PatchHelper.Patch(ctx, vpcScope.LinodeVPC); patchErr != nil && util.IgnoreKubeNotFound(patchErr) != nil { logger.Error(patchErr, "failed to patch LinodeVPC") err = errors.Join(err, patchErr) diff --git a/util/helpers.go b/util/helpers.go index 3607960d3..1f27b2007 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -7,6 +7,8 @@ import ( "github.com/linode/linodego" "k8s.io/apimachinery/pkg/types" + kerrors "k8s.io/apimachinery/pkg/util/errors" + "sigs.k8s.io/controller-runtime/pkg/client" ) // Pointer returns the pointer of any type @@ -49,3 +51,20 @@ func IgnoreLinodeAPIError(err error, code int) error { return err } + +// IgnoreKubeNotFound returns the error even if aggregated except not found +func IgnoreKubeNotFound(err error) error { + //nolint:errorlint // This is specific non wrapped error. + errs, ok := err.(kerrors.Aggregate) + if !ok { + return client.IgnoreNotFound(err) + } + + for _, e := range errs.Errors() { + if client.IgnoreNotFound(e) != nil { + return err + } + } + + return nil +}