Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: dmitry.lopatin <[email protected]>
  • Loading branch information
LopatinDmitr committed Dec 9, 2024
1 parent aa7a52b commit c7c091c
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions images/virtualization-artifact/pkg/controller/vd/internal/inuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ 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"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/common/object"
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition"
Expand Down Expand Up @@ -74,7 +77,7 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
break
}
} else {
kvvm, err := helper.FetchObject(ctx, types.NamespacedName{Name: vm.Name, Namespace: vm.Namespace}, h.client, &virtv1.VirtualMachine{})
kvvm, err := object.FetchObject(ctx, types.NamespacedName{Name: vm.Name, Namespace: vm.Namespace}, h.client, &virtv1.VirtualMachine{})
if err != nil {
return reconcile.Result{}, fmt.Errorf("error getting kvvms: %w", err)
}
Expand All @@ -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
}
}
}
}
}
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit c7c091c

Please sign in to comment.