Skip to content

Commit

Permalink
OVA: refactor cleanup of NFS PV and PVC used for migration.
Browse files Browse the repository at this point in the history
Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy committed Feb 20, 2024
1 parent 7cdb3d1 commit 87e51ea
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
42 changes: 25 additions & 17 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 2114 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2107-L2114

Added lines #L2107 - L2114 were not covered by tests
context.TODO(),
types.NamespacedName{
Name: getEntityPrefixName("pv", providerName, planName),
pvs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(pvLabels),

Check warning on line 2118 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2116-L2118

Added lines #L2116 - L2118 were not covered by tests
},
pv,
)

if err != nil {
if k8serr.IsNotFound(err) {
return nil, false, nil
Expand All @@ -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(

Check warning on line 2138 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2131-L2138

Added lines #L2131 - L2138 were not covered by tests
context.TODO(),
types.NamespacedName{
Name: getEntityPrefixName("pvc", providerName, planName),
Namespace: planNamespace,
pvcs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(pvcLabels),
Namespace: planNamespace,

Check warning on line 2143 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2140-L2143

Added lines #L2140 - L2143 were not covered by tests
},
pvc,
)

if err != nil {
if k8serr.IsNotFound(err) {
return nil, false, nil
Expand All @@ -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)

Check warning on line 2161 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2161

Added line #L2161 was not covered by tests

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"}

Check warning on line 2163 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2163

Added line #L2163 was not covered by tests
pv := &core.PersistentVolume{
ObjectMeta: meta.ObjectMeta{
GenerateName: pvcNamePrefix,
Expand Down Expand Up @@ -2184,7 +2192,7 @@ func (r *KubeVirt) CreatePvForNfs() (pvName string, err error) {

func (r *KubeVirt) CreatePvcForNfs(pvcNamePrefix string, pvName string) (pvcName string, err error) {

Check warning on line 2193 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2193

Added line #L2193 was not covered by tests
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"}

Check warning on line 2195 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2195

Added line #L2195 was not covered by tests
pvc := &core.PersistentVolumeClaim{
ObjectMeta: meta.ObjectMeta{
GenerateName: pvcNamePrefix,
Expand Down
36 changes: 20 additions & 16 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 521 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L521

Added line #L521 was not covered by tests
if err != nil {
r.Log.Error(err, "Failed to get the plan PVC")
r.Log.Error(err, "Failed to get the plan PVCs")

Check warning on line 523 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L523

Added line #L523 was not covered by tests
return
}
// The PVC was already deleted
if pvc == nil {
// The PVCs was already deleted
if len(pvcs.Items) == 0 {

Check warning on line 527 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L527

Added line #L527 was not covered by tests
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
}

Check warning on line 536 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L531-L536

Added lines #L531 - L536 were not covered by tests
}

pv, _, err := GetOvaPvNfs(r.Destination.Client, r.Plan.Name, r.Plan.Provider.Source.Name)
pvs, _, err := GetOvaPvNfs(r.Destination.Client, string(r.Plan.UID))

Check warning on line 539 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L539

Added line #L539 was not covered by tests
if err != nil {
r.Log.Error(err, "Failed to get the plan PV")
r.Log.Error(err, "Failed to get the plan PVs")

Check warning on line 541 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L541

Added line #L541 was not covered by tests
return
}
// The PV was already deleted
if pv == nil {
// The PVs was already deleted
if len(pvs.Items) == 0 {

Check warning on line 545 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L545

Added line #L545 was not covered by tests
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
}

Check warning on line 554 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L549-L554

Added lines #L549 - L554 were not covered by tests
}
return
}
Expand Down

0 comments on commit 87e51ea

Please sign in to comment.