Skip to content

Commit cbfa8ca

Browse files
Merge pull request #37 from aws/feature/destroy_vm_async
Destroy VM asynchronously
2 parents 1a56b3b + c1a416a commit cbfa8ca

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

controllers/cloudstackmachine_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type CloudStackMachineReconciler struct {
5555
}
5656

5757
const requeueTimeout = 5 * time.Second
58+
const destoryVMRequeueInterval = 10 * time.Second
5859

5960
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines,verbs=get;list;watch;create;update;patch;delete
6061
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines/status,verbs=get;update;patch
@@ -225,6 +226,10 @@ func (r *CloudStackMachineReconciler) reconcileDelete(
225226

226227
log.Info("Deleting instance", "instance-id", *csMachine.Spec.InstanceID)
227228
if err := r.CS.DestroyVMInstance(csMachine); err != nil {
229+
if err.Error() == "VM deletion in progress" {
230+
log.Info(err.Error())
231+
return ctrl.Result{RequeueAfter: destoryVMRequeueInterval}, nil
232+
}
228233
return ctrl.Result{}, err
229234
}
230235
controllerutil.RemoveFinalizer(csMachine, infrav1.MachineFinalizer)

pkg/cloud/client.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ type Client interface {
4141
}
4242

4343
type client struct {
44-
cs *cloudstack.CloudStackClient
45-
// This is a placeholder for sending non-blocking requests.
46-
// csA *cloudstack.CloudStackClient
44+
cs *cloudstack.CloudStackClient
45+
csAsync *cloudstack.CloudStackClient
4746
}
4847

4948
// cloud-config ini structure.
@@ -65,11 +64,9 @@ func NewClient(ccPath string) (Client, error) {
6564
return nil, errors.Wrapf(err, "error encountered while parsing [Global] section from config at path: %s", ccPath)
6665
}
6766

68-
// This is a placeholder for sending non-blocking requests.
69-
// c.csA = cloudstack.NewClient(apiUrl, apiKey, secretKey, false)
70-
// TODO: attempt a less clunky client liveliness check (not just listing zones).
7167
c.cs = cloudstack.NewAsyncClient(cfg.APIURL, cfg.APIKey, cfg.SecretKey, cfg.VerifySSL)
72-
_, err := c.cs.Zone.ListZones(c.cs.Zone.NewListZonesParams())
68+
c.csAsync = cloudstack.NewClient(cfg.APIURL, cfg.APIKey, cfg.SecretKey, cfg.VerifySSL)
69+
_, err := c.cs.APIDiscovery.ListApis(c.cs.APIDiscovery.NewListApisParams())
7370
if err != nil && strings.Contains(strings.ToLower(err.Error()), "i/o timeout") {
7471
return c, errors.Wrap(err, "Timeout while checking CloudStack API Client connectivity.")
7572
}

pkg/cloud/instance.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,26 @@ func (c *client) GetOrCreateVMInstance(
213213
// DestroyVMInstance Destroy a VM instance. Assumes machine has been fetched prior and has an instance ID.
214214
func (c *client) DestroyVMInstance(csMachine *infrav1.CloudStackMachine) error {
215215

216+
if err := c.ResolveVMInstanceDetails(csMachine); err == nil && csMachine.Status.InstanceState != "Running" {
217+
if csMachine.Status.InstanceState == "Stopping" ||
218+
csMachine.Status.InstanceState == "Stopped" {
219+
return errors.New("VM deletion in progress")
220+
} else if csMachine.Status.InstanceState == "Expunging" ||
221+
csMachine.Status.InstanceState == "Expunged" {
222+
// VM is stopped and getting expunged. So the desired state is getting satisfied. Let's move on.
223+
return nil
224+
}
225+
} else if err != nil && strings.Contains(strings.ToLower(err.Error()), "no match found") {
226+
// VM doesn't exist. So the desired state is in effect. Our work is done here.
227+
return nil
228+
}
229+
216230
p := c.cs.VirtualMachine.NewDestroyVirtualMachineParams(*csMachine.Spec.InstanceID)
217231
p.SetExpunge(true)
218-
_, err := c.cs.VirtualMachine.DestroyVirtualMachine(p)
219-
if err != nil && strings.Contains(strings.ToLower(err.Error()), "unable to find UUID for id ") {
232+
_, err := c.csAsync.VirtualMachine.DestroyVirtualMachine(p)
233+
if err != nil && strings.Contains(err.Error(), "Unable to find UUID for id") {
220234
// VM doesn't exist. So the desired state is in effect. Our work is done here.
221235
return nil
222236
}
223-
return err
237+
return errors.New("VM deletion in progress")
224238
}

0 commit comments

Comments
 (0)