Skip to content

Commit 118f715

Browse files
committed
Fix crash on delete with no bastion
1 parent 2837b29 commit 118f715

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

controllers/openstackcluster_controller.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,13 @@ func deleteBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openStac
291291
}
292292

293293
// If no instance was created we currently need to check for orphaned
294-
// volumes. This requires resolving the instance spec.
295-
// TODO: write volumes to status resources on creation so this is no longer required.
294+
// volumes.
296295
if instanceStatus == nil {
297-
instanceSpec, err := bastionToInstanceSpec(openStackCluster, cluster)
298-
if err != nil {
299-
return err
300-
}
301-
if err := computeService.DeleteVolumes(instanceSpec); err != nil {
302-
return fmt.Errorf("delete volumes: %w", err)
296+
bastion := openStackCluster.Spec.Bastion
297+
if bastion != nil && bastion.Spec != nil {
298+
if err := computeService.DeleteVolumes(bastionName(cluster.Name), bastion.Spec.RootVolume, bastion.Spec.AdditionalBlockDevices); err != nil {
299+
return fmt.Errorf("delete volumes: %w", err)
300+
}
303301
}
304302
} else {
305303
instanceNS, err := instanceStatus.NetworkStatus()

controllers/openstackmachine_controller.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,12 @@ func (r *OpenStackMachineReconciler) reconcileDelete(scope *scope.WithLogger, cl
300300
}
301301

302302
// If no instance was created we currently need to check for orphaned
303-
// volumes. This requires resolving the instance spec.
304-
// TODO: write volumes to status resources on creation so this is no longer required.
305-
if instanceStatus == nil && openStackMachine.Status.Resolved != nil {
306-
instanceSpec, err := machineToInstanceSpec(openStackCluster, machine, openStackMachine, "")
307-
if err != nil {
308-
return ctrl.Result{}, err
309-
}
310-
if err := computeService.DeleteVolumes(instanceSpec); err != nil {
303+
// volumes.
304+
if instanceStatus == nil {
305+
if err := computeService.DeleteVolumes(openStackMachine.Name, openStackMachine.Spec.RootVolume, openStackMachine.Spec.AdditionalBlockDevices); err != nil {
311306
return ctrl.Result{}, fmt.Errorf("delete volumes: %w", err)
312307
}
313-
}
314-
315-
if instanceStatus != nil {
308+
} else {
316309
if err := computeService.DeleteInstance(openStackMachine, instanceStatus); err != nil {
317310
conditions.MarkFalse(openStackMachine, infrav1.InstanceReadyCondition, infrav1.InstanceDeleteFailedReason, clusterv1.ConditionSeverityError, "Deleting instance failed: %v", err)
318311
return ctrl.Result{}, fmt.Errorf("delete instance: %w", err)

pkg/cloud/services/compute/instance.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,14 @@ func (s *Service) DeleteInstance(eventObject runtime.Object, instanceStatus *Ins
431431
}
432432

433433
// DeleteVolumes deletes any cinder volumes which were created for the instance.
434-
// Note that this must only be called when the server was not successfully
434+
// Note that this need only be called when the server was not successfully
435435
// created. If the server was created the volume will have been added with
436436
// DeleteOnTermination=true, and will be automatically cleaned up with the
437437
// server.
438-
func (s *Service) DeleteVolumes(instanceSpec *InstanceSpec) error {
438+
// We don't pass InstanceSpec here because we only require instance name,
439+
// rootVolume, and additionalBlockDevices, and resolving the whole InstanceSpec
440+
// introduces unnecessary failure modes.
441+
func (s *Service) DeleteVolumes(instanceName string, rootVolume *infrav1.RootVolume, additionalBlockDevices []infrav1.AdditionalBlockDevice) error {
439442
/*
440443
Attaching volumes to an instance is a two-step process:
441444
@@ -455,13 +458,13 @@ func (s *Service) DeleteVolumes(instanceSpec *InstanceSpec) error {
455458
DeleteOnTermination will ensure it is deleted in that case.
456459
*/
457460

458-
if hasRootVolume(instanceSpec) {
459-
if err := s.deleteVolume(instanceSpec.Name, "root"); err != nil {
461+
if rootVolume != nil && rootVolume.SizeGiB > 0 {
462+
if err := s.deleteVolume(instanceName, "root"); err != nil {
460463
return err
461464
}
462465
}
463-
for _, volumeSpec := range instanceSpec.AdditionalBlockDevices {
464-
if err := s.deleteVolume(instanceSpec.Name, volumeSpec.Name); err != nil {
466+
for _, volumeSpec := range additionalBlockDevices {
467+
if err := s.deleteVolume(instanceName, volumeSpec.Name); err != nil {
465468
return err
466469
}
467470
}

0 commit comments

Comments
 (0)