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 12, 2023
1 parent 90733c4 commit bbdd9ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ const (
)

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

var (
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit bbdd9ce

Please sign in to comment.