Skip to content

Commit 35fc6d1

Browse files
authored
Merge pull request #92 from aws/fix/csmachine-missing-finalizer
Fix CloudStackMachine Missing Finalizer
2 parents 9156702 + 814ce39 commit 35fc6d1

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

controllers/cloudstackmachine_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes
193193

194194
if err == nil && !controllerutil.ContainsFinalizer(r.ReconciliationSubject, infrav1.MachineFinalizer) { // Fetched or Created?
195195
r.Log.Info("CloudStack instance Created", "instanceStatus", r.ReconciliationSubject.Status)
196-
controllerutil.AddFinalizer(r.ReconciliationSubject, infrav1.MachineFinalizer)
197196
}
197+
// Always add the finalizer regardless. It can't be added twice anyway.
198+
controllerutil.AddFinalizer(r.ReconciliationSubject, infrav1.MachineFinalizer)
198199
return ctrl.Result{}, err
199200
}
200201

@@ -204,7 +205,7 @@ func (r *CloudStackMachineReconciliationRunner) RequeueIfInstanceNotRunning() (r
204205
r.Log.Info("Machine instance is Running...")
205206
r.ReconciliationSubject.Status.Ready = true
206207
} else if r.ReconciliationSubject.Status.InstanceState == "Error" {
207-
r.Log.Info("CloudStackMachine VM in error state. Deleting associated Machine.", "csMachine", r.ReconciliationSubject)
208+
r.Log.Info("CloudStackMachine VM in error state. Deleting associated Machine.", "csMachine", r.ReconciliationSubject.GetName())
208209
if err := r.K8sClient.Delete(r.RequestCtx, r.CAPIMachine); err != nil {
209210
return ctrl.Result{}, err
210211
}
@@ -259,6 +260,7 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
259260
}
260261
return ctrl.Result{}, err
261262
}
263+
r.Log.Info("VM Deleted.")
262264
controllerutil.RemoveFinalizer(r.ReconciliationSubject, infrav1.MachineFinalizer)
263265
return ctrl.Result{}, nil
264266
}

controllers/utils/utils.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,20 @@ func GetOwnerOfKind(ctx context.Context, c clientPkg.Client, owned clientPkg.Obj
143143
return errors.Errorf(
144144
"found more than one GVK for owner when finding owner kind for %s/%s", owned.GetName(), owned.GetNamespace())
145145
}
146-
kind := gvks[0].Kind
146+
gvk := gvks[0]
147+
147148
for _, ref := range owned.GetOwnerReferences() {
148-
if ref.Kind != kind {
149+
if ref.Kind != gvk.Kind {
149150
continue
150151
}
151152
key := clientPkg.ObjectKey{Name: ref.Name, Namespace: owned.GetNamespace()}
152153
if err := c.Get(ctx, key, owner); err != nil {
153-
return errors.Wrapf(err, "finding owner of kind %s %s/%s",
154-
owner.GetObjectKind().GroupVersionKind().Kind, owner.GetNamespace(), owner.GetName())
154+
return errors.Wrapf(err, "finding owner of kind %s in namespace %s", gvk.Kind, owned.GetNamespace())
155155
}
156156
return nil
157157
}
158-
return errors.Errorf("couldn't find owner of kind %s %s/%s",
159-
owner.GetObjectKind().GroupVersionKind().Kind, owner.GetNamespace(), owner.GetName())
158+
159+
return errors.Errorf("couldn't find owner of kind %s in namespace %s", gvk.Kind, owned.GetNamespace())
160160
}
161161

162162
func ContainsNoMatchSubstring(err error) bool {

0 commit comments

Comments
 (0)