Skip to content

Commit 6c01769

Browse files
committed
add instance deletion logic
1 parent 702fe1b commit 6c01769

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ This project aims to follow the Cluster API [Provider contract](https://cluster-
7979
8080
- [ ] Register virtual machine instance with Controlplane load balancer
8181
82-
- [ ] Delete virtual machine instance
82+
- [x] Delete virtual machine instance
8383
8484
- [ ] Un-register virtual machine instance with Controlplane load balancer
8585

cloud/services/compute/instance/reconcile.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,23 @@ func (s *Service) GetRandomNode() (*node.Node, error) {
169169
return nodes[r.Intn(len(nodes))], nil
170170
}
171171

172-
// wip
173172
func (s *Service) Delete(ctx context.Context) error {
173+
log := log.FromContext(ctx)
174+
log.Info("Deleting instance resources")
175+
174176
instance, err := s.GetInstance(ctx)
175177
if err != nil {
176178
if !IsNotFound(err) {
177179
return err
178180
}
179181
return nil
180182
}
181-
// to do : stop instance before deletion
183+
184+
// must stop or pause instance before deletion
185+
// otherwise deletion will be fail
186+
if err := EnsureStoppedOrPaused(*instance); err != nil {
187+
return err
188+
}
182189
return instance.Delete()
183190
}
184191

@@ -249,11 +256,23 @@ func EnsureRunning(instance vm.VirtualMachine) error {
249256
return err
250257
}
251258
default:
252-
errors.Errorf("unexpected status : %s", instance.Status)
259+
return errors.Errorf("unexpected status : %s", instance.Status)
253260
}
254261
return nil
255262
}
256263

257-
// func EnsureStopped(instance vm.VirtualMachine) error {
258-
// return nil
259-
// }
264+
func EnsureStoppedOrPaused(instance vm.VirtualMachine) error {
265+
switch instance.Status {
266+
case vm.ProcessStatusRunning:
267+
if err := instance.Stop(); err != nil {
268+
return err
269+
}
270+
case vm.ProcessStatusPaused:
271+
return nil
272+
case vm.ProcessStatusStopped:
273+
return nil
274+
default:
275+
return errors.Errorf("unexpected status : %s", instance.Status)
276+
}
277+
return nil
278+
}

controllers/proxmoxmachine_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ func (r *ProxmoxMachineReconciler) reconcile(ctx context.Context, machineScope *
183183
}
184184
}
185185

186-
// wip
187186
func (r *ProxmoxMachineReconciler) reconcileDelete(ctx context.Context, machineScope *scope.MachineScope) (ctrl.Result, error) {
188187
log := log.FromContext(ctx)
189188
log.Info("Reconciling Delete ProxmoxMachine")

0 commit comments

Comments
 (0)