Skip to content

Commit

Permalink
Fix Kube patch aggregate error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Kovacs committed Jan 30, 2024
1 parent bbd27bb commit f0b7c2b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions controller/linodevpc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit f0b7c2b

Please sign in to comment.