Skip to content

Commit

Permalink
Merge pull request #2865 from zhanggbj/improve_patch_helper
Browse files Browse the repository at this point in the history
🌱 Improve patch helper error handling
  • Loading branch information
k8s-ci-robot authored Apr 3, 2024
2 parents 046cbe5 + 8b9dad1 commit dbedef4
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion controllers/serviceaccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, req reconcile.
// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereCluster, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}

// Create the cluster context for this request.
Expand Down
2 changes: 1 addition & 1 deletion controllers/servicediscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (r *serviceDiscoveryReconciler) Reconcile(ctx context.Context, req reconcil
// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereCluster, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}

// Create the cluster context for this request.
Expand Down
2 changes: 1 addition & 1 deletion controllers/vmware/vspherecluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
// Build the patch helper.
patchHelper, err := patch.NewHelper(vsphereCluster, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}

// Build the cluster context.
Expand Down
8 changes: 2 additions & 6 deletions controllers/vspherecluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (r *clusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereCluster, r.Client)
if err != nil {
return reconcile.Result{}, pkgerrors.Wrap(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}

// Create the cluster context for this request.
Expand Down Expand Up @@ -264,12 +264,8 @@ func (r *clusterReconciler) reconcileIdentitySecret(ctx context.Context, cluster
if !ctrlutil.ContainsFinalizer(secret, infrav1.SecretIdentitySetFinalizer) {
ctrlutil.AddFinalizer(secret, infrav1.SecretIdentitySetFinalizer)
}
err = helper.Patch(ctx, secret)
if err != nil {
return pkgerrors.Wrapf(err, "Failed to patch secret %s", klog.KObj(secret))
}

return nil
return helper.Patch(ctx, secret)
}

func (r *clusterReconciler) reconcileVCenterConnectivity(ctx context.Context, clusterCtx *capvcontext.ClusterContext) (*session.Session, error) {
Expand Down
2 changes: 1 addition & 1 deletion controllers/vsphereclusteridentity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
// Create the patch helper.
patchHelper, err := patch.NewHelper(identity, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}

defer func() {
Expand Down
11 changes: 4 additions & 7 deletions controllers/vspheredeploymentzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (r vsphereDeploymentZoneReconciler) Reconcile(ctx context.Context, request

patchHelper, err := patch.NewHelper(vsphereDeploymentZone, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}

vsphereDeploymentZoneContext := &capvcontext.VSphereDeploymentZoneContext{
Expand Down Expand Up @@ -287,17 +287,14 @@ func (r vsphereDeploymentZoneReconciler) reconcileDelete(ctx context.Context, de
func updateOwnerReferences(ctx context.Context, obj client.Object, client client.Client, ownerRefFunc func() []metav1.OwnerReference) error {
patchHelper, err := patch.NewHelper(obj, client)
if err != nil {
return errors.Wrapf(err, "failed to init patch helper for %s %s",
obj.GetObjectKind(),
obj.GetName())
return err
}

obj.SetOwnerReferences(ownerRefFunc())
if err := patchHelper.Patch(ctx, obj); err != nil {
return errors.Wrapf(err, "failed to patch object %s %s",
obj.GetObjectKind(),
obj.GetName())
return errors.Wrapf(err, "failed to update OwnerReferences")
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/vspheremachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (r *machineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
// Create the patch helper.
patchHelper, err := patch.NewHelper(machineContext.GetVSphereMachine(), r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}
machineContext.SetBaseMachineContext(&capvcontext.BaseMachineContext{
Cluster: cluster,
Expand Down
2 changes: 1 addition & 1 deletion controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (r vmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.R
// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereVM, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to initialize patch helper")
return reconcile.Result{}, err
}

authSession, err := r.retrieveVcenterSession(ctx, vsphereVM)
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/govmomi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (vms *VMService) DestroyVM(ctx context.Context, vmCtx *capvcontext.VMContex

virtualMachineCtx.VSphereVM.Status.TaskRef = task.Reference().Value
if err = virtualMachineCtx.Patch(ctx); err != nil {
return reconcile.Result{}, vm, errors.Wrapf(err, "failed to patch VSphereVM")
return reconcile.Result{}, vm, err
}

log.Info("Wait for VM to be powered off")
Expand Down Expand Up @@ -363,7 +363,7 @@ func (vms *VMService) reconcilePowerState(ctx context.Context, virtualMachineCtx
// Update the VSphereVM.Status.TaskRef to track the power-on task.
virtualMachineCtx.VSphereVM.Status.TaskRef = task.Reference().Value
if err = virtualMachineCtx.Patch(ctx); err != nil {
return false, errors.Wrapf(err, "failed to patch VSphereVM")
return false, err
}

// Once the VM is successfully powered on, a reconcile request should be
Expand Down

0 comments on commit dbedef4

Please sign in to comment.