Skip to content

Commit

Permalink
Improve patch helper error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Gong Zhang <[email protected]>
  • Loading branch information
zhanggbj committed Apr 1, 2024
1 parent 80d2f2a commit 23c54aa
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 24 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

Check warning on line 168 in controllers/serviceaccount_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/serviceaccount_controller.go#L168

Added line #L168 was not covered by tests
}

// 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

Check warning on line 176 in controllers/servicediscovery_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/servicediscovery_controller.go#L176

Added line #L176 was not covered by tests
}

// 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

Check warning on line 105 in controllers/vmware/vspherecluster_reconciler.go

View check run for this annotation

Codecov / codecov/patch

controllers/vmware/vspherecluster_reconciler.go#L105

Added line #L105 was not covered by tests
}

// 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

Check warning on line 94 in controllers/vspherecluster_reconciler.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherecluster_reconciler.go#L94

Added line #L94 was not covered by tests
}

// 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

Check warning on line 93 in controllers/vsphereclusteridentity_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vsphereclusteridentity_controller.go#L93

Added line #L93 was not covered by tests
}

defer func() {
Expand Down
13 changes: 3 additions & 10 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

Check warning on line 108 in controllers/vspheredeploymentzone_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspheredeploymentzone_controller.go#L108

Added line #L108 was not covered by tests
}

vsphereDeploymentZoneContext := &capvcontext.VSphereDeploymentZoneContext{
Expand Down Expand Up @@ -287,18 +287,11 @@ 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

Check warning on line 290 in controllers/vspheredeploymentzone_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspheredeploymentzone_controller.go#L290

Added line #L290 was not covered by tests
}

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 nil
return patchHelper.Patch(ctx, obj)
}

func (r vsphereDeploymentZoneReconciler) failureDomainsToDeploymentZones(ctx context.Context, a client.Object) []reconcile.Request {
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

Check warning on line 234 in controllers/vspheremachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspheremachine_controller.go#L234

Added line #L234 was not covered by tests
}
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

Check warning on line 175 in controllers/vspherevm_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherevm_controller.go#L175

Added line #L175 was not covered by tests
}

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

Check warning on line 254 in pkg/services/govmomi/service.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/govmomi/service.go#L254

Added line #L254 was not covered by tests
}

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

Check warning on line 366 in pkg/services/govmomi/service.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/govmomi/service.go#L366

Added line #L366 was not covered by tests
}

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

0 comments on commit 23c54aa

Please sign in to comment.