From 31322026825710edaad1d42c95b1405860e0bec0 Mon Sep 17 00:00:00 2001 From: "dmitry.lopatin" Date: Tue, 3 Dec 2024 22:04:15 +0300 Subject: [PATCH] fix Signed-off-by: dmitry.lopatin --- .../pkg/controller/vd/internal/inuse.go | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go b/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go index 03f49e0d3..420f8bd4d 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go @@ -21,7 +21,10 @@ import ( "fmt" "slices" + corev1 "k8s.io/api/core/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" virtv1 "kubevirt.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -83,6 +86,22 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon allowUseForVM = true break } + + podList := corev1.PodList{} + err = h.client.List(ctx, &podList, &client.ListOptions{ + Namespace: vm.GetNamespace(), + LabelSelector: labels.SelectorFromSet(map[string]string{virtv1.VirtualMachineNameLabel: vm.GetName()}), + }) + if err != nil && !k8serrors.IsNotFound(err) { + return reconcile.Result{}, fmt.Errorf("unable to list virt-launcher Pod for VM %q: %w", vm.GetName(), err) + } + + for _, pod := range podList.Items { + if pod.Status.Phase == corev1.PodRunning { + allowUseForVM = true + break + } + } } } } @@ -137,17 +156,19 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon conditions.SetCondition(cb, &vd.Status.Conditions) } default: - if inUseCondition.Reason == vdcondition.AllowedForVirtualMachineUsage.String() && !allowUseForVM || inUseCondition.Reason == vdcondition.AllowedForImageUsage.String() && !allowUseForImage { - cb. - Generation(vd.Generation). - Status(metav1.ConditionUnknown). - Reason(conditions.ReasonUnknown). - Message("") - conditions.SetCondition(cb, &vd.Status.Conditions) + needChange := false + + if inUseCondition.Reason == vdcondition.AllowedForVirtualMachineUsage.String() && !allowUseForVM { + needChange = true } - if allowUseForImage || allowUseForVM { - return reconcile.Result{Requeue: true}, nil + if inUseCondition.Reason == vdcondition.AllowedForImageUsage.String() && !allowUseForImage { + needChange = true + } + + if needChange { + cb.Generation(vd.Generation).Status(metav1.ConditionUnknown).Reason(conditions.ReasonUnknown).Message("") + conditions.SetCondition(cb, &vd.Status.Conditions) } }