From fc7d1468cd0c787e1418bfedb2306a6cfadbef18 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Tue, 29 Oct 2024 17:05:03 +0100 Subject: [PATCH] Delete all PVCs if guest cluster is deleted in Rancher If a cluster is deleted, the PVCs of the workloads are not deleted in Harvester. This is because the node driver does not know why the associated VM has to be deleted, e.g. because its parameters have changed or because the cluster is deleted. To solve the problem, a finalizer on the Machine resource in Rancher will add an annotation to the VM which then is evaluated by the node driver when it running the Remove() handler. Signed-off-by: Volker Theile --- harvester/harvester.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/harvester/harvester.go b/harvester/harvester.go index d72e3fa3..9d03720a 100644 --- a/harvester/harvester.go +++ b/harvester/harvester.go @@ -23,6 +23,8 @@ const ( actionStart = "start" actionStop = "stop" actionRestart = "restart" + + removeAllPVCsAnnotationKey = "harvesterhci.io/removeAllPersistentVolumeClaims" ) // Driver is the driver used when no driver is selected. It is used to @@ -180,9 +182,16 @@ func (d *Driver) Remove() error { } return err } + + removeAll := false + if value, ok := vm.Annotations[removeAllPVCsAnnotationKey]; ok && value == "true" { + log.Debugf("Force the removal of all persistent volume claims") + removeAll = true + } + removedPVCs := make([]string, 0, len(vm.Spec.Template.Spec.Volumes)) for _, volume := range vm.Spec.Template.Spec.Volumes { - if volume.PersistentVolumeClaim == nil || volume.PersistentVolumeClaim.Hotpluggable { + if volume.PersistentVolumeClaim == nil || (!removeAll && volume.PersistentVolumeClaim.Hotpluggable) { continue } removedPVCs = append(removedPVCs, volume.PersistentVolumeClaim.ClaimName)