Skip to content

Commit

Permalink
Clean importer pods on success
Browse files Browse the repository at this point in the history
Multiple importer pods may be created during warm migrations. This patch
cleans up all of them for the migrated VM once the migration completes
successfully.

Signed-off-by: Liran Rotenberg <lrotenbe@redhat.com>
  • Loading branch information
liranr23 committed Oct 1, 2023
1 parent b7b33f1 commit fa9ca40
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
75 changes: 57 additions & 18 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ func (r *KubeVirt) GetImporterPod(pvc core.PersistentVolumeClaim) (pod *core.Pod
return
}

// Get the importer pods for a PersistentVolumeClaim.
func (r *KubeVirt) GetImporterPods(pvc core.PersistentVolumeClaim) (pods []core.Pod, err error) {
pods = []core.Pod{}
if _, ok := pvc.Annotations[AnnImporterPodName]; !ok {
return
}

podList := &core.PodList{}

err = r.Destination.Client.List(
context.TODO(),
podList,
&client.ListOptions{
Namespace: r.Plan.Spec.TargetNamespace,
LabelSelector: labels.SelectorFromSet(map[string]string{"app": "containerized-data-importer"}),
},
)
if err != nil {
err = liberr.Wrap(err)
return
}
for _, pod := range podList.Items {
if strings.Contains(pod.Name, fmt.Sprintf("importer-%s-%s", r.Plan.Name, pvc.Annotations[kVM])) {
pods = append(pods, pod)
}
}

return
}

// Delete the DataVolumes associated with the VM.
func (r *KubeVirt) DeleteDataVolumes(vm *plan.VMStatus) (err error) {
dvs, err := r.getDVs(vm)
Expand All @@ -235,27 +265,36 @@ func (r *KubeVirt) DeleteDataVolumes(vm *plan.VMStatus) (err error) {
return
}

// Delete the importer pod for a PersistentVolumeClaim.
func (r *KubeVirt) DeleteImporterPod(pvc core.PersistentVolumeClaim) (err error) {
var pod *core.Pod
var found bool
pod, found, err = r.GetImporterPod(pvc)
if err != nil || !found {
return
}
err = r.Destination.Client.Delete(context.TODO(), pod)
// Delete the importer pods for a PersistentVolumeClaim.
func (r *KubeVirt) DeleteImporterPods(pvc core.PersistentVolumeClaim) (err error) {
pods, err := r.GetImporterPods(pvc)
if err != nil {
err = liberr.Wrap(err)
return
}
r.Log.Info(
"Deleted importer pod.",
"pod",
path.Join(
pod.Namespace,
pod.Name),
"pvc",
pvc.Name)
for _, pod := range pods {
err = r.Destination.Client.Delete(context.TODO(), &pod)
if err != nil {
err = liberr.Wrap(err)
r.Log.Error(
err,
"Deleting importer pod failed.",
"pod",
path.Join(
pod.Namespace,
pod.Name),
"pvc",
pvc.Name)
continue
}
r.Log.Info(
"Deleted importer pod.",
"pod",
path.Join(
pod.Namespace,
pod.Name),
"pvc",
pvc.Name)
}
return
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (r *Migration) deleteImporterPods(vm *plan.VMStatus) (err error) {
return
}
for _, pvc := range pvcs {
err = r.kubevirt.DeleteImporterPod(pvc)
err = r.kubevirt.DeleteImporterPods(pvc)
if err != nil {
return
}
Expand Down Expand Up @@ -748,14 +748,15 @@ func (r *Migration) execute(vm *plan.VMStatus) (err error) {
// Removing unnecessary DataVolumes
err = r.kubevirt.DeleteDataVolumes(vm)
if err != nil {
step.AddError(err.Error())
err = nil
break
err = liberr.Wrap(err)

Check failure on line 751 in pkg/controller/plan/migration.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `err` is never used (staticcheck)
}
err = r.kubevirt.DeletePVCConsumerPod(vm)
if err != nil {
err = liberr.Wrap(err)
return
}
err = r.deleteImporterPods(vm)
if err != nil {
err = liberr.Wrap(err)
}
step.MarkCompleted()
step.Phase = Completed
Expand Down

0 comments on commit fa9ca40

Please sign in to comment.