Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
Signed-off-by: Liran Rotenberg <[email protected]>
  • Loading branch information
liranr23 committed Nov 1, 2023
1 parent 4fabbf5 commit f7211d5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,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
Expand Down Expand Up @@ -987,7 +987,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)
}
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const (

const (
TransferCompleted = "Transfer completed."
PopulatorPodPrefix = "populate-"
)

var (
Expand Down Expand Up @@ -1620,7 +1621,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
}
Expand All @@ -1647,17 +1648,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) {
Expand All @@ -1666,7 +1668,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
Expand Down

0 comments on commit f7211d5

Please sign in to comment.