Skip to content

Commit

Permalink
fix(vd): fix update generation in conditions
Browse files Browse the repository at this point in the history
Signed-off-by: dmitry.lopatin <[email protected]>
  • Loading branch information
LopatinDmitr committed Jan 14, 2025
1 parent c93042a commit 9ef497a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (ds ObjectRefVirtualDisk) Validate(ctx context.Context, cvi *virtv2.Cluster
inUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, vd.Status.Conditions)
if inUseCondition.Status == metav1.ConditionTrue &&
inUseCondition.Reason == vdcondition.UsedForImageCreation.String() &&
inUseCondition.ObservedGeneration == vd.Status.ObservedGeneration {
inUseCondition.ObservedGeneration == vd.Generation {
return nil
}

Expand Down
54 changes: 25 additions & 29 deletions images/virtualization-artifact/pkg/controller/vd/internal/inuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
inUseCondition = cb.Condition()
}

allowUseForVM, allowUseForImage := false, false
usedByVM, usedByImage := false, false

var vms virtv2.VirtualMachineList
err := h.client.List(ctx, &vms, &client.ListOptions{
Expand All @@ -78,9 +78,9 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
for _, vm := range vms.Items {
if h.isVDAttachedToVM(vd.GetName(), vm) {
if vm.Status.Phase != virtv2.MachineStopped {
allowUseForVM = isVMCanStart(vm.Status.Conditions)
usedByVM = isVMCanStart(vm.Status.Conditions)

if allowUseForVM {
if usedByVM {
break
}
} else {
Expand All @@ -90,7 +90,7 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
}

if kvvm != nil && kvvm.Status.StateChangeRequests != nil {
allowUseForVM = true
usedByVM = true
break
}

Expand All @@ -105,7 +105,7 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon

for _, pod := range podList.Items {
if pod.Status.Phase == corev1.PodRunning {
allowUseForVM = true
usedByVM = true
break
}
}
Expand All @@ -129,7 +129,7 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
vi.Spec.DataSource.ObjectRef != nil &&
vi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind &&
vi.Spec.DataSource.ObjectRef.Name == vd.Name {
allowUseForImage = true
usedByImage = true
break
}
}
Expand All @@ -145,40 +145,36 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
cvi.Spec.DataSource.ObjectRef != nil &&
cvi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind &&
cvi.Spec.DataSource.ObjectRef.Name == vd.Name {
allowUseForImage = true
usedByImage = true
}
}

cb := conditions.NewConditionBuilder(vdcondition.InUseType)
switch {
case allowUseForVM && inUseCondition.Status != metav1.ConditionTrue:
if inUseCondition.Reason != vdcondition.AttachedToVirtualMachine.String() {
cb.
Generation(vd.Generation).
Status(metav1.ConditionTrue).
Reason(vdcondition.AttachedToVirtualMachine).
Message("")
conditions.SetCondition(cb, &vd.Status.Conditions)
return reconcile.Result{}, nil
}
case allowUseForImage && inUseCondition.Status != metav1.ConditionTrue:
if inUseCondition.Reason != vdcondition.UsedForImageCreation.String() {
cb.
Generation(vd.Generation).
Status(metav1.ConditionTrue).
Reason(vdcondition.UsedForImageCreation).
Message("")
conditions.SetCondition(cb, &vd.Status.Conditions)
return reconcile.Result{}, nil
}
case usedByVM && inUseCondition.Status != metav1.ConditionTrue:
cb.
Generation(vd.Generation).
Status(metav1.ConditionTrue).
Reason(vdcondition.AttachedToVirtualMachine).
Message("")
conditions.SetCondition(cb, &vd.Status.Conditions)
return reconcile.Result{}, nil
case usedByImage && inUseCondition.Status != metav1.ConditionTrue:
cb.
Generation(vd.Generation).
Status(metav1.ConditionTrue).
Reason(vdcondition.UsedForImageCreation).
Message("")
conditions.SetCondition(cb, &vd.Status.Conditions)
return reconcile.Result{}, nil
default:
setNotInUse := false

if inUseCondition.Reason == vdcondition.AttachedToVirtualMachine.String() && !allowUseForVM {
if inUseCondition.Reason == vdcondition.AttachedToVirtualMachine.String() && !usedByVM {
setNotInUse = true
}

if inUseCondition.Reason == vdcondition.UsedForImageCreation.String() && !allowUseForImage {
if inUseCondition.Reason == vdcondition.UsedForImageCreation.String() && !usedByImage {
setNotInUse = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (ds ObjectRefVirtualDisk) Validate(ctx context.Context, vi *virtv2.VirtualI
inUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, vd.Status.Conditions)
if inUseCondition.Status == metav1.ConditionTrue &&
inUseCondition.Reason == vdcondition.UsedForImageCreation.String() &&
inUseCondition.ObservedGeneration == vd.Status.ObservedGeneration {
inUseCondition.ObservedGeneration == vd.Generation {
return nil
}

Expand Down

0 comments on commit 9ef497a

Please sign in to comment.