From bbdd9ce126e446515be9ea0157c0d59fe9c2d807 Mon Sep 17 00:00:00 2001 From: Liran Rotenberg Date: Wed, 1 Nov 2023 16:35:11 +0200 Subject: [PATCH] Refactor the code Signed-off-by: Liran Rotenberg --- pkg/controller/plan/kubevirt.go | 4 ++-- pkg/controller/plan/migration.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index c71b2e463..e670314e7 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -862,7 +862,7 @@ func (r *KubeVirt) SetPopulatorPodOwnership(vm *plan.VMStatus) (err error) { return } for _, pod := range pods { - pvcId := strings.Split(pod.Name, "populate-")[1] + pvcId := pod.Name[len(PopulatorPodPrefix):] for _, pvc := range pvcs { if string(pvc.UID) != pvcId { continue @@ -898,7 +898,7 @@ func (r *KubeVirt) getPopulatorPods() (pods []core.Pod, err error) { return nil, liberr.Wrap(err) } for _, pod := range migrationPods.Items { - if strings.HasPrefix(pod.Name, "populate-") { + if strings.HasPrefix(pod.Name, PopulatorPodPrefix) { pods = append(pods, pod) } } diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index 488e3a6fa..8fca6f624 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -84,7 +84,8 @@ const ( ) const ( - TransferCompleted = "Transfer completed." + TransferCompleted = "Transfer completed." + PopulatorPodPrefix = "populate-" ) var ( @@ -1601,7 +1602,7 @@ func (r *Migration) updatePopulatorCopyProgress(vm *plan.VMStatus, step *plan.St continue } - err = r.isPopulatorFailed(populatorPods, string(pvc.UID)) + _, err = r.isPopulatorFailed(populatorPods, string(pvc.UID)) if err != nil { return } @@ -1628,17 +1629,18 @@ func (r *Migration) updatePopulatorCopyProgress(vm *plan.VMStatus, step *plan.St return } -func (r *Migration) isPopulatorFailed(populatorPods []core.Pod, givenPvcId string) error { +func (r *Migration) isPopulatorFailed(populatorPods []core.Pod, givenPvcId string) (bool, error) { for _, pod := range populatorPods { - pvcId := strings.Split(pod.Name, "populate-")[1] + pvcId := pod.Name[len(PopulatorPodPrefix):] if givenPvcId != pvcId { continue } if pod.Status.Phase == core.PodFailed { - return fmt.Errorf("populator pod %s/%s failed for PVC %s. Please check the pod logs.", pod.Namespace, pod.Name, pvcId) + return true, fmt.Errorf("populator pod %s/%s failed for PVC %s. Please check the pod logs.", pod.Namespace, pod.Name, pvcId) } + break } - return nil + return false, nil } func (r *Migration) setPopulatorPodsWithLabels(vm *plan.VMStatus, migrationID string) { @@ -1647,7 +1649,7 @@ func (r *Migration) setPopulatorPodsWithLabels(vm *plan.VMStatus, migrationID st return } for _, pod := range podList.Items { - if strings.HasPrefix(pod.Name, "populate-") { + if strings.HasPrefix(pod.Name, PopulatorPodPrefix) { // it's populator pod if _, ok := pod.Labels["migration"]; !ok { // un-labeled pod, we need to set it