Skip to content

Commit

Permalink
Fix the finalizer code (#1513)
Browse files Browse the repository at this point in the history
* Fix the finalizer code

* Fix the unit tests
  • Loading branch information
mkumatag authored Nov 24, 2023
1 parent a37a749 commit bd65766
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 9 deletions.
3 changes: 1 addition & 2 deletions controllers/ibmpowervscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func (r *IBMPowerVSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re
}

func (r *IBMPowerVSClusterReconciler) reconcile(clusterScope *scope.PowerVSClusterScope) ctrl.Result { //nolint:unparam
if !controllerutil.ContainsFinalizer(clusterScope.IBMPowerVSCluster, infrav1beta2.IBMPowerVSClusterFinalizer) {
controllerutil.AddFinalizer(clusterScope.IBMPowerVSCluster, infrav1beta2.IBMPowerVSClusterFinalizer)
if controllerutil.AddFinalizer(clusterScope.IBMPowerVSCluster, infrav1beta2.IBMPowerVSClusterFinalizer) {
return ctrl.Result{}
}

Expand Down
4 changes: 3 additions & 1 deletion controllers/ibmpowervsimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func (r *IBMPowerVSImageReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

func (r *IBMPowerVSImageReconciler) reconcile(cluster *infrav1beta2.IBMPowerVSCluster, imageScope *scope.PowerVSImageScope) (ctrl.Result, error) {
controllerutil.AddFinalizer(imageScope.IBMPowerVSImage, infrav1beta2.IBMPowerVSImageFinalizer)
if controllerutil.AddFinalizer(imageScope.IBMPowerVSImage, infrav1beta2.IBMPowerVSImageFinalizer) {
return ctrl.Result{}, nil
}

// Create new labels section for IBMPowerVSImage metadata if nil.
if imageScope.IBMPowerVSImage.Labels == nil {
Expand Down
1 change: 1 addition & 0 deletions controllers/ibmpowervsimage_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func TestIBMPowerVSImageReconciler_reconcile(t *testing.T) {
UID: "1",
},
},
Finalizers: []string{infrav1beta2.IBMPowerVSImageFinalizer},
},
Spec: infrav1beta2.IBMPowerVSImageSpec{
ClusterName: "capi-powervs-cluster",
Expand Down
4 changes: 3 additions & 1 deletion controllers/ibmpowervsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ func (r *IBMPowerVSMachineReconciler) reconcileNormal(machineScope *scope.PowerV
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}

controllerutil.AddFinalizer(machineScope.IBMPowerVSMachine, infrav1beta2.IBMPowerVSMachineFinalizer)
if controllerutil.AddFinalizer(machineScope.IBMPowerVSMachine, infrav1beta2.IBMPowerVSMachineFinalizer) {
return ctrl.Result{}, nil
}

ins, err := r.getOrCreate(machineScope)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions controllers/ibmpowervsmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ func TestIBMPowerVSMachineReconciler_ReconcileOperations(t *testing.T) {
},
IBMPowerVSMachine: &infrav1beta2.IBMPowerVSMachine{
ObjectMeta: metav1.ObjectMeta{
Name: *pointer.String("capi-test-machine"),
Name: *pointer.String("capi-test-machine"),
Finalizers: []string{infrav1beta2.IBMPowerVSMachineFinalizer},
},
},
IBMPowerVSImage: &infrav1beta2.IBMPowerVSImage{
Expand Down Expand Up @@ -484,7 +485,8 @@ func TestIBMPowerVSMachineReconciler_ReconcileOperations(t *testing.T) {

pvsmachine := &infrav1beta2.IBMPowerVSMachine{
ObjectMeta: metav1.ObjectMeta{
Name: *pointer.String("capi-test-machine"),
Name: *pointer.String("capi-test-machine"),
Finalizers: []string{infrav1beta2.IBMPowerVSMachineFinalizer},
},
Spec: infrav1beta2.IBMPowerVSMachineSpec{
MemoryGiB: 8,
Expand Down
4 changes: 2 additions & 2 deletions controllers/ibmvpccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func (r *IBMVPCClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

func (r *IBMVPCClusterReconciler) reconcile(clusterScope *scope.ClusterScope) (ctrl.Result, error) {
if !controllerutil.ContainsFinalizer(clusterScope.IBMVPCCluster, infrav1beta2.ClusterFinalizer) {
controllerutil.AddFinalizer(clusterScope.IBMVPCCluster, infrav1beta2.ClusterFinalizer)
// If the IBMVPCCluster doesn't have our finalizer, add it.
if controllerutil.AddFinalizer(clusterScope.IBMVPCCluster, infrav1beta2.ClusterFinalizer) {
return ctrl.Result{}, nil
}

Expand Down
4 changes: 3 additions & 1 deletion controllers/ibmvpcmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func (r *IBMVPCMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *IBMVPCMachineReconciler) reconcileNormal(machineScope *scope.MachineScope) (ctrl.Result, error) {
controllerutil.AddFinalizer(machineScope.IBMVPCMachine, infrav1beta2.MachineFinalizer)
if controllerutil.AddFinalizer(machineScope.IBMVPCMachine, infrav1beta2.MachineFinalizer) {
return ctrl.Result{}, nil
}

// Make sure bootstrap data is available and populated.
if machineScope.Machine.Spec.Bootstrap.DataSecretName == nil {
Expand Down
2 changes: 2 additions & 0 deletions controllers/ibmvpcmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func TestIBMVPCMachineReconciler_reconcile(t *testing.T) {
Labels: map[string]string{
capiv1beta1.MachineControlPlaneNameLabel: "capi-control-plane-machine",
},
Finalizers: []string{infrav1beta2.MachineFinalizer},
},
},
Machine: &capiv1beta1.Machine{
Expand Down Expand Up @@ -290,6 +291,7 @@ func TestIBMVPCMachineLBReconciler_reconcile(t *testing.T) {
Labels: map[string]string{
capiv1beta1.MachineControlPlaneNameLabel: "capi-control-plane-machine",
},
Finalizers: []string{infrav1beta2.MachineFinalizer},
},
},
Machine: &capiv1beta1.Machine{
Expand Down

0 comments on commit bd65766

Please sign in to comment.