Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean importer pods on success #588

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 55 additions & 18 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,34 @@ 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) {
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])) {
liranr23 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -236,27 +264,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
ahadas marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,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 @@ -801,6 +801,11 @@ func (r *Migration) execute(vm *plan.VMStatus) (err error) {
err = liberr.Wrap(err)
return
}
err = r.deleteImporterPods(vm)
if err != nil {
err = liberr.Wrap(err)
return
ahadas marked this conversation as resolved.
Show resolved Hide resolved
}
step.MarkCompleted()
step.Phase = Completed
vm.Phase = r.next(vm.Phase)
Expand Down
Loading