From 87e51eab537defd87e236d2e3a5da0c664eb6874 Mon Sep 17 00:00:00 2001 From: Bella Khizgiyaev Date: Tue, 20 Feb 2024 20:41:45 +0200 Subject: [PATCH] OVA: refactor cleanup of NFS PV and PVC used for migration. Signed-off-by: Bella Khizgiyaev --- pkg/controller/plan/kubevirt.go | 42 +++++++++++++++++++------------- pkg/controller/plan/migration.go | 36 +++++++++++++++------------ 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index 97dead78c..232a3b558 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -2104,16 +2104,20 @@ func (r *KubeVirt) EnsurePersistentVolume(vmRef ref.Ref, persistentVolumes []cor return } -func GetOvaPvNfs(client client.Client, planName string, providerName string) (pv *core.PersistentVolume, found bool, err error) { - pv = &core.PersistentVolume{} - err = client.Get( +func GetOvaPvNfs(dClient client.Client, planID string) (pvs *core.PersistentVolumeList, found bool, err error) { + pvs = &core.PersistentVolumeList{} + pvLabels := map[string]string{ + "plan": planID, + "ova": "nfs-pv", + } + + err = dClient.List( context.TODO(), - types.NamespacedName{ - Name: getEntityPrefixName("pv", providerName, planName), + pvs, + &client.ListOptions{ + LabelSelector: labels.SelectorFromSet(pvLabels), }, - pv, ) - if err != nil { if k8serr.IsNotFound(err) { return nil, false, nil @@ -2124,17 +2128,21 @@ func GetOvaPvNfs(client client.Client, planName string, providerName string) (pv return } -func GetOvaPvcNfs(client client.Client, planName string, planNamespace string, providerName string) (pvc *core.PersistentVolumeClaim, found bool, err error) { - pvc = &core.PersistentVolumeClaim{} - err = client.Get( +func GetOvaPvcNfs(dClient client.Client, planID string, planNamespace string) (pvcs *core.PersistentVolumeClaimList, found bool, err error) { + pvcs = &core.PersistentVolumeClaimList{} + pvcLabels := map[string]string{ + "plan": planID, + "ova": "nfs-pvc", + } + + err = dClient.List( context.TODO(), - types.NamespacedName{ - Name: getEntityPrefixName("pvc", providerName, planName), - Namespace: planNamespace, + pvcs, + &client.ListOptions{ + LabelSelector: labels.SelectorFromSet(pvcLabels), + Namespace: planNamespace, }, - pvc, ) - if err != nil { if k8serr.IsNotFound(err) { return nil, false, nil @@ -2152,7 +2160,7 @@ func (r *KubeVirt) CreatePvForNfs() (pvName string, err error) { nfsPath := splitted[1] pvcNamePrefix := getEntityPrefixName("pv", r.Source.Provider.Name, r.Plan.Name) - labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": r.Plan.Name} + labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": string(r.Plan.UID), "ova": "nfs-pv"} pv := &core.PersistentVolume{ ObjectMeta: meta.ObjectMeta{ GenerateName: pvcNamePrefix, @@ -2184,7 +2192,7 @@ func (r *KubeVirt) CreatePvForNfs() (pvName string, err error) { func (r *KubeVirt) CreatePvcForNfs(pvcNamePrefix string, pvName string) (pvcName string, err error) { sc := "" - labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": r.Plan.Name} + labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": string(r.Plan.UID), "ova": "nfs-pvc"} pvc := &core.PersistentVolumeClaim{ ObjectMeta: meta.ObjectMeta{ GenerateName: pvcNamePrefix, diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index 5e221c5be..576547e03 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -518,36 +518,40 @@ func (r *Migration) deleteImporterPods(vm *plan.VMStatus) (err error) { } func (r *Migration) deletePvcPvForOva() (err error) { - pvc, _, err := GetOvaPvcNfs(r.Destination.Client, r.Plan.Name, r.Plan.Spec.TargetNamespace, r.Plan.Provider.Source.Name) + pvcs, _, err := GetOvaPvcNfs(r.Destination.Client, r.Plan.Name, r.Plan.Spec.TargetNamespace) if err != nil { - r.Log.Error(err, "Failed to get the plan PVC") + r.Log.Error(err, "Failed to get the plan PVCs") return } - // The PVC was already deleted - if pvc == nil { + // The PVCs was already deleted + if len(pvcs.Items) == 0 { return } - err = r.Destination.Client.Delete(context.TODO(), pvc) - if err != nil { - r.Log.Error(err, "Failed to delete the plan PVC") - return + for _, pvc := range pvcs.Items { + err = r.Destination.Client.Delete(context.TODO(), &pvc) + if err != nil { + r.Log.Error(err, "Failed to delete the plan PVC", pvc) + return + } } - pv, _, err := GetOvaPvNfs(r.Destination.Client, r.Plan.Name, r.Plan.Provider.Source.Name) + pvs, _, err := GetOvaPvNfs(r.Destination.Client, string(r.Plan.UID)) if err != nil { - r.Log.Error(err, "Failed to get the plan PV") + r.Log.Error(err, "Failed to get the plan PVs") return } - // The PV was already deleted - if pv == nil { + // The PVs was already deleted + if len(pvs.Items) == 0 { return } - err = r.Destination.Client.Delete(context.TODO(), pv) - if err != nil { - r.Log.Error(err, "Failed to delete the plan PV") - return + for _, pv := range pvs.Items { + err = r.Destination.Client.Delete(context.TODO(), &pv) + if err != nil { + r.Log.Error(err, "Failed to delete the plan PV", pv) + return + } } return }