Skip to content

Commit

Permalink
fix(api): add new logic allowed use for images and vms
Browse files Browse the repository at this point in the history
Signed-off-by: dmitry.lopatin <[email protected]>
  • Loading branch information
LopatinDmitr committed Nov 22, 2024
1 parent 0a3f181 commit eb1e890
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 40 deletions.
14 changes: 9 additions & 5 deletions api/core/v1alpha2/vdcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type (
// StorageClassReadyReason represents the various reasons for the Storageclass ready condition type.
StorageClassReadyReason string
// InUseReason represents the various reasons for the InUse condition type.
InUseReason = string
InUseReason string
)

func (s DatasourceReadyReason) String() string {
Expand All @@ -73,6 +73,10 @@ func (s StorageClassReadyReason) String() string {
return string(s)
}

func (s InUseReason) String() string {
return string(s)
}

const (
// DatasourceReady indicates that the datasource is ready for use, allowing the import process to start.
DatasourceReady DatasourceReadyReason = "DatasourceReady"
Expand Down Expand Up @@ -123,8 +127,8 @@ const (
// StorageClassNotFound indicates that the storage class is not ready
StorageClassNotFound StorageClassReadyReason = "StorageClassNotFound"

// InUseByVirtualImage indicates that the VirtualDisk is being used in a process of a VirtualImage creation.
InUseByVirtualImage InUseReason = "InUseByVirtualImage"
// InUseByVirtualMachine indicates that the VirtualDisk is attached to a running VirtualMachine.
InUseByVirtualMachine InUseReason = "InUseByVirtualMachine"
// AllowedForImageUsage indicates that the VirtualDisk is permitted for use in a process of an image creation.
AllowedForImageUsage InUseReason = "AllowedForImageUsage"
// AllowedForVirtualMachineUsage indicates that the VirtualDisk is permitted for use by a running VirtualMachine.
AllowedForVirtualMachineUsage InUseReason = "AllowedForVirtualMachineUsage"
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/watchers"
"github.com/deckhouse/virtualization-controller/pkg/logger"
Expand Down Expand Up @@ -166,8 +167,8 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
return false
}

oldInUseCondition, _ := service.GetCondition(vdcondition.InUseType, oldVD.Status.Conditions)
newInUseCondition, _ := service.GetCondition(vdcondition.InUseType, newVD.Status.Conditions)
oldInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, oldVD.Status.Conditions)
newInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, newVD.Status.Conditions)

if oldVD.Status.Phase != newVD.Status.Phase || len(oldVD.Status.AttachedToVirtualMachines) != len(newVD.Status.AttachedToVirtualMachines) || oldInUseCondition.Status != newInUseCondition.Status {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, cvi *virtv2.ClusterV
Reason(cvicondition.VirtualDiskNotReady).
Message(service.CapitalizeFirstLetter(err.Error()))
return reconcile.Result{}, nil
case errors.As(err, &source.VirtualDiskInUseError{}):
case errors.As(err, &source.VirtualDiskNotAllowedForUseError{}):
cb.
Status(metav1.ConditionFalse).
Reason(cvicondition.VirtualDiskInUseInRunningVirtualMachine).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ func NewVirtualDiskNotReadyError(name string) error {
}
}

type VirtualDiskInUseError struct {
type VirtualDiskNotAllowedForUseError struct {
name string
}

func (e VirtualDiskInUseError) Error() string {
return fmt.Sprintf("reading from the VirtualDisk is not possible while it is in use by the running VirtualMachine/%s", e.name)
func (e VirtualDiskNotAllowedForUseError) Error() string {
return fmt.Sprintf("use of VirtualDisk %s is not allowed, it may be connected to a running VirtualMachine", e.name)
}

func NewVirtualDiskInUseError(name string) error {
return VirtualDiskInUseError{
func NewVirtualDiskNotAllowedForUseError(name string) error {
return VirtualDiskNotAllowedForUseError{
name: name,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/cvicondition"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vicondition"
)

type ObjectRefVirtualDisk struct {
Expand Down Expand Up @@ -226,12 +225,12 @@ func (ds ObjectRefVirtualDisk) Validate(ctx context.Context, cvi *virtv2.Cluster
return NewVirtualDiskNotReadyError(cvi.Spec.DataSource.ObjectRef.Name)
}

inUseCondition, _ := service.GetCondition(vdcondition.InUseType, vd.Status.Conditions)
inUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, vd.Status.Conditions)
if inUseCondition.Status == metav1.ConditionTrue {
if inUseCondition.Reason == vdcondition.InUseByVirtualMachine {
return NewVirtualDiskInUseError(vd.Name)
if inUseCondition.Reason == vdcondition.AllowedForImageUsage.String() {
return nil
}
}

return nil
return NewVirtualDiskNotAllowedForUseError(vd.Name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, vi *virtv2.VirtualIm
Reason(vicondition.VirtualDiskNotReady).
Message(service.CapitalizeFirstLetter(err.Error() + "."))
return reconcile.Result{}, nil
case errors.As(err, &source.VirtualDiskInUseError{}):
case errors.As(err, &source.VirtualDiskNotAllowedForUseError{}):
cb.
Status(metav1.ConditionFalse).
Reason(vicondition.VirtualDiskInUseInRunningVirtualMachine).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ func NewVirtualDiskNotReadyError(name string) error {
}
}

type VirtualDiskInUseError struct {
type VirtualDiskNotAllowedForUseError struct {
name string
}

func (e VirtualDiskInUseError) Error() string {
return fmt.Sprintf("reading from the VirtualDisk is not possible while it is in use by the running VirtualMachine/%s", e.name)
func (e VirtualDiskNotAllowedForUseError) Error() string {
return fmt.Sprintf("use of VirtualDisk %s is not allowed, it may be connected to a running VirtualMachine", e.name)
}

func NewVirtualDiskInUseError(name string) error {
return VirtualDiskInUseError{
func NewVirtualDiskNotAllowedForUseError(name string) error {
return VirtualDiskNotAllowedForUseError{
name: name,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,12 @@ func (ds ObjectRefVirtualDisk) Validate(ctx context.Context, vi *virtv2.VirtualI
return NewVirtualDiskNotReadyError(vi.Spec.DataSource.ObjectRef.Name)
}

inUseCondition, _ := service.GetCondition(vdcondition.InUseType, vd.Status.Conditions)
inUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, vd.Status.Conditions)
if inUseCondition.Status == metav1.ConditionTrue {
if inUseCondition.Reason == vdcondition.InUseByVirtualMachine {
return NewVirtualDiskInUseError(vd.Name)
if inUseCondition.Reason == vdcondition.AllowedForImageUsage.String() {
return nil
}
}
return nil

return NewVirtualDiskNotAllowedForUseError(vd.Name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/vi/internal/watcher"
"github.com/deckhouse/virtualization-controller/pkg/controller/watchers"
Expand Down Expand Up @@ -221,8 +222,8 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
return false
}

oldInUseCondition, _ := service.GetCondition(vdcondition.InUseType, oldVD.Status.Conditions)
newInUseCondition, _ := service.GetCondition(vdcondition.InUseType, newVD.Status.Conditions)
oldInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, oldVD.Status.Conditions)
newInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, newVD.Status.Conditions)

if oldVD.Status.Phase != newVD.Status.Phase || len(oldVD.Status.AttachedToVirtualMachines) != len(newVD.Status.AttachedToVirtualMachines) || oldInUseCondition.Status != newInUseCondition.Status {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,15 @@ func (h *BlockDeviceHandler) countReadyBlockDevices(vm *virtv2.VirtualMachine, s
canStartKVVM = false
continue
}
readyCondition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions)
readyCondition, _ := conditions.GetCondition(vdcondition.ReadyType, vd.Status.Conditions)
if readyCondition.Status == metav1.ConditionTrue {
ready++
inUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, vd.Status.Conditions)
if inUseCondition.Status == metav1.ConditionTrue && inUseCondition.Reason == vdcondition.AllowedForVirtualMachineUsage.String() {
ready++
} else {
msg := fmt.Sprintf("virtual disk %s is not allowed for use in the virtual machine, it may be used to create an image", vd.Name)
warnings = append(warnings, msg)
}
} else {
msg := fmt.Sprintf("virtual disk %s is waiting for the it's pvc to be bound", vd.Name)
warnings = append(warnings, msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ var _ = Describe("BlockDeviceHandler", func() {
Target: virtv2.DiskTarget{PersistentVolumeClaim: "pvc-foo"},
Conditions: []metav1.Condition{
{
Type: vdcondition.ReadyType,
Reason: vdcondition.Ready,
Type: vdcondition.ReadyType.String(),
Reason: vdcondition.Ready.String(),
Status: metav1.ConditionTrue,
},
{
Type: vdcondition.InUseType.String(),
Reason: vdcondition.AllowedForVirtualMachineUsage.String(),
Status: metav1.ConditionTrue,
},
},
Expand All @@ -85,8 +90,13 @@ var _ = Describe("BlockDeviceHandler", func() {
Target: virtv2.DiskTarget{PersistentVolumeClaim: "pvc-bar"},
Conditions: []metav1.Condition{
{
Type: vdcondition.ReadyType,
Reason: vdcondition.Ready,
Type: vdcondition.ReadyType.String(),
Reason: vdcondition.Ready.String(),
Status: metav1.ConditionTrue,
},
{
Type: vdcondition.InUseType.String(),
Reason: vdcondition.AllowedForVirtualMachineUsage.String(),
Status: metav1.ConditionTrue,
},
},
Expand Down Expand Up @@ -183,10 +193,15 @@ var _ = Describe("BlockDeviceHandler", func() {
vdFoo.Status.Phase = virtv2.DiskProvisioning
vdFoo.Status.Conditions = []metav1.Condition{
{
Type: vdcondition.ReadyType,
Reason: vdcondition.Provisioning,
Type: vdcondition.ReadyType.String(),
Reason: vdcondition.Provisioning.String(),
Status: metav1.ConditionFalse,
},
{
Type: vdcondition.InUseType.String(),
Reason: vdcondition.AllowedForVirtualMachineUsage.String(),
Status: metav1.ConditionTrue,
},
}
state := getBlockDevicesState(vi, cvi, vdFoo, vdBar)
ready, canStart, warnings := h.countReadyBlockDevices(vm, state, logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/indexer"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state"
Expand Down Expand Up @@ -218,8 +219,8 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
return false
}

oldInUseCondition, _ := service.GetCondition(vdcondition.InUseType, oldVd.Status.Conditions)
newInUseCondition, _ := service.GetCondition(vdcondition.InUseType, newVd.Status.Conditions)
oldInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, oldVd.Status.Conditions)
newInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, newVd.Status.Conditions)

if oldVd.Status.Phase != newVd.Status.Phase || oldInUseCondition.Status != newInUseCondition.Status {
return true
Expand Down

0 comments on commit eb1e890

Please sign in to comment.