Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
Signed-off-by: dmitry.lopatin <[email protected]>
  • Loading branch information
LopatinDmitr committed Dec 12, 2024
1 parent c7c091c commit d75eaf6
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 188 deletions.
2 changes: 1 addition & 1 deletion api/core/v1alpha2/vdcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
SnapshottingType Type = "Snapshotting"
// StorageClassReadyType indicates whether the storage class is ready.
StorageClassReadyType Type = "StorageClassReady"
// InUseType indicates whether the VirtualDisk is attached to a running VirtualMachine or is being used in a process of a VirtualImage creation.
// InUseType indicates whether the VirtualDisk is attached to a running VirtualMachine or is being used in a process of an image creation.
InUseType Type = "InUse"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ 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.AllowedForImageUsage.String() {
if inUseCondition.Status == metav1.ConditionTrue &&
inUseCondition.Reason == vdcondition.AllowedForImageUsage.String() &&
inUseCondition.ObservedGeneration == vd.Status.ObservedGeneration {
return nil
}

Expand Down
67 changes: 37 additions & 30 deletions images/virtualization-artifact/pkg/controller/vd/internal/inuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"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"
Expand Down Expand Up @@ -71,7 +70,7 @@ 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 = isVMReady(vm.Status.Conditions)
allowUseForVM = isVMCanStart(vm.Status.Conditions)

if allowUseForVM {
break
Expand All @@ -92,7 +91,7 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
Namespace: vm.GetNamespace(),
LabelSelector: labels.SelectorFromSet(map[string]string{virtv1.VirtualMachineNameLabel: vm.GetName()}),
})
if err != nil && !k8serrors.IsNotFound(err) {
if err != nil {
return reconcile.Result{}, fmt.Errorf("unable to list virt-launcher Pod for VM %q: %w", vm.GetName(), err)
}

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

var vis virtv2.VirtualImageList
err = h.client.List(ctx, &vis, &client.ListOptions{
Namespace: vd.GetNamespace(),
})
if err != nil {
return reconcile.Result{}, fmt.Errorf("error getting virtual images: %w", err)
}
if !allowUseForVM {
var vis virtv2.VirtualImageList
err = h.client.List(ctx, &vis, &client.ListOptions{
Namespace: vd.GetNamespace(),
})
if err != nil {
return reconcile.Result{}, fmt.Errorf("error getting virtual images: %w", err)
}

allowedPhases := []virtv2.ImagePhase{virtv2.ImageProvisioning, virtv2.ImagePending}
allowedPhases := []virtv2.ImagePhase{virtv2.ImageProvisioning, virtv2.ImagePending}

for _, vi := range vis.Items {
if slices.Contains(allowedPhases, vi.Status.Phase) && vi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef && vi.Spec.DataSource.ObjectRef != nil && vi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind {
allowUseForImage = true
break
for _, vi := range vis.Items {
if slices.Contains(allowedPhases, vi.Status.Phase) &&
vi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef &&
vi.Spec.DataSource.ObjectRef != nil &&
vi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind &&
vi.Spec.DataSource.ObjectRef.Name == vd.Name {
allowUseForImage = true
break
}
}
}

var cvis virtv2.ClusterVirtualImageList
err = h.client.List(ctx, &cvis, &client.ListOptions{})
if err != nil {
return reconcile.Result{}, fmt.Errorf("error getting cluster virtual images: %w", err)
}
for _, cvi := range cvis.Items {
if slices.Contains(allowedPhases, cvi.Status.Phase) && cvi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef && cvi.Spec.DataSource.ObjectRef != nil && cvi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind {
allowUseForImage = true
var cvis virtv2.ClusterVirtualImageList
err = h.client.List(ctx, &cvis, &client.ListOptions{})
if err != nil {
return reconcile.Result{}, fmt.Errorf("error getting cluster virtual images: %w", err)
}
for _, cvi := range cvis.Items {
if slices.Contains(allowedPhases, cvi.Status.Phase) &&
cvi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef &&
cvi.Spec.DataSource.ObjectRef != nil && cvi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind && cvi.Spec.DataSource.ObjectRef.Name == vd.Name {
allowUseForImage = true
}
}

}

cb := conditions.NewConditionBuilder(vdcondition.InUseType)

switch {
case allowUseForVM && inUseCondition.Status == metav1.ConditionUnknown:
if inUseCondition.Reason != vdcondition.AllowedForVirtualMachineUsage.String() {
Expand All @@ -156,17 +163,17 @@ func (h InUseHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (recon
conditions.SetCondition(cb, &vd.Status.Conditions)
}
default:
needChange := false
setUnknown := false

if inUseCondition.Reason == vdcondition.AllowedForVirtualMachineUsage.String() && !allowUseForVM {
needChange = true
setUnknown = true
}

if inUseCondition.Reason == vdcondition.AllowedForImageUsage.String() && !allowUseForImage {
needChange = true
setUnknown = true
}

if needChange {
if setUnknown {
cb.Generation(vd.Generation).Status(metav1.ConditionUnknown).Reason(conditions.ReasonUnknown).Message("")
conditions.SetCondition(cb, &vd.Status.Conditions)
}
Expand All @@ -185,7 +192,7 @@ func (h InUseHandler) isVDAttachedToVM(vdName string, vm virtv2.VirtualMachine)
return false
}

func isVMReady(conditions []metav1.Condition) bool {
func isVMCanStart(conditions []metav1.Condition) bool {
critConditions := []string{
vmcondition.TypeIPAddressReady.String(),
vmcondition.TypeClassReady.String(),
Expand Down
Loading

0 comments on commit d75eaf6

Please sign in to comment.