diff --git a/api/core/v1alpha2/virtual_machine_ip_address_lease.go b/api/core/v1alpha2/virtual_machine_ip_address_lease.go index 524c6f275..97997c935 100644 --- a/api/core/v1alpha2/virtual_machine_ip_address_lease.go +++ b/api/core/v1alpha2/virtual_machine_ip_address_lease.go @@ -18,6 +18,11 @@ package v1alpha2 import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +const ( + VirtualMachineIPAddressLeaseKind = "VirtualMachineIPAddressLease" + VirtualMachineIPAddressLeaseResource = "virtualmachineipaddresleases" +) + // VirtualMachineIPAddressLease defines fact of issued lease for `VirtualMachineIPAddress`. // +genclient // +genclient:nonNamespaced diff --git a/api/core/v1alpha2/virtual_machine_operation.go b/api/core/v1alpha2/virtual_machine_operation.go index 7cf347eee..41dbf23fb 100644 --- a/api/core/v1alpha2/virtual_machine_operation.go +++ b/api/core/v1alpha2/virtual_machine_operation.go @@ -19,8 +19,8 @@ package v1alpha2 import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" const ( - VMOPKind = "VirtualMachineOperation" - VMOPResource = "virtualmachineoperations" + VirtualMachineOperationKind = "VirtualMachineOperation" + VirtualmachineOperationResource = "virtualmachineoperations" ) // VirtualMachineOperation resource provides the ability to declaratively manage state changes of virtual machines. diff --git a/images/virtualization-artifact/pkg/controller/service/resource.go b/images/virtualization-artifact/pkg/controller/service/resource.go index 10dcb992f..845c6c074 100644 --- a/images/virtualization-artifact/pkg/controller/service/resource.go +++ b/images/virtualization-artifact/pkg/controller/service/resource.go @@ -30,7 +30,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/common/patch" + "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" ) type ResourceObject[T, ST any] interface { @@ -103,11 +105,68 @@ func (r *Resource[T, ST]) Changed() T { return r.changedObj } +// rewriteObject is part of the transition from version 1.14, where you can specify empty reasons. After version 1.15, this feature is not needed. +// TODO: Delete me after release v1.15 +func rewriteObject(obj client.Object) { + var conds []metav1.Condition + + switch obj.GetObjectKind().GroupVersionKind().Kind { + case virtv2.VirtualMachineKind: + vm := obj.(*virtv2.VirtualMachine) + conds = vm.Status.Conditions + case virtv2.VirtualDiskKind: + vd := obj.(*virtv2.VirtualDisk) + conds = vd.Status.Conditions + case virtv2.VirtualImageKind: + vi := obj.(*virtv2.VirtualImage) + conds = vi.Status.Conditions + case virtv2.ClusterVirtualImageKind: + cvi := obj.(*virtv2.ClusterVirtualImage) + conds = cvi.Status.Conditions + case virtv2.VirtualMachineBlockDeviceAttachmentKind: + vmbda := obj.(*virtv2.VirtualMachineBlockDeviceAttachment) + conds = vmbda.Status.Conditions + case virtv2.VirtualMachineIPAddressKind: + ip := obj.(*virtv2.VirtualMachineIPAddress) + conds = ip.Status.Conditions + case virtv2.VirtualMachineIPAddressLeaseKind: + ipl := obj.(*virtv2.VirtualMachineIPAddressLease) + conds = ipl.Status.Conditions + case virtv2.VirtualMachineOperationKind: + vmop := obj.(*virtv2.VirtualMachineOperation) + conds = vmop.Status.Conditions + case virtv2.VirtualDiskSnapshotKind: + snap := obj.(*virtv2.VirtualDiskSnapshot) + conds = snap.Status.Conditions + case virtv2.VirtualMachineClassKind: + class := obj.(*virtv2.VirtualMachineClass) + conds = class.Status.Conditions + case virtv2.VirtualMachineRestoreKind: + restore := obj.(*virtv2.VirtualMachineRestore) + conds = restore.Status.Conditions + case virtv2.VirtualMachineSnapshotKind: + snap := obj.(*virtv2.VirtualMachineSnapshot) + conds = snap.Status.Conditions + } + + rewriteEmptyReason(conds) +} + +func rewriteEmptyReason(conds []metav1.Condition) { + for i := range conds { + if conds[i].Reason == "" { + conds[i].Reason = conditions.ReasonUnknown.String() + } + } +} + func (r *Resource[T, ST]) Update(ctx context.Context) error { if r.IsEmpty() { return nil } + rewriteObject(r.changedObj) + if !reflect.DeepEqual(r.getObjStatus(r.currentObj), r.getObjStatus(r.changedObj)) { // Save some metadata fields. finalizers := r.changedObj.GetFinalizers() diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready.go b/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready.go index 7ec8ab283..9ca106b6c 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready.go @@ -57,7 +57,7 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, vd *virtv2.VirtualDi if vd.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready_test.go b/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready_test.go index ccbc90921..325fe00ad 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready_test.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/source" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" @@ -55,7 +56,7 @@ func TestDatasourceReadyHandler_Handle(t *testing.T) { condition := vd.Status.Conditions[0] require.Equal(t, vdcondition.DatasourceReadyType, condition.Type) require.Equal(t, metav1.ConditionUnknown, condition.Status) - require.Equal(t, "", condition.Reason) + require.Equal(t, conditions.ReasonUnknown.String(), condition.Reason) }) t.Run("VirtualDisk with Blank DataSource", func(t *testing.T) { diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go b/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go index 08bbcdb24..e6e2894a4 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go @@ -60,7 +60,7 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re if vd.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil } @@ -68,7 +68,7 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re readyCondition, ok := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) if !ok || readyCondition.Status != metav1.ConditionTrue { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil } @@ -81,14 +81,14 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re if pvc == nil { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "Underlying PersistentVolumeClaim not found: resizing is not possible." return reconcile.Result{}, nil } if pvc.Status.Phase != corev1.ClaimBound { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "Underlying PersistentVolumeClaim not bound: resizing is not possible." return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go b/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go index b6c3b5941..eedb25184 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go @@ -52,7 +52,7 @@ func (h SnapshottingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) if vd.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil } @@ -60,7 +60,7 @@ func (h SnapshottingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) readyCondition, ok := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) if !ok || readyCondition.Status != metav1.ConditionTrue { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil } @@ -94,7 +94,7 @@ func (h SnapshottingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) } condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go index 252de1022..0964a1e3c 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go @@ -79,7 +79,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual if vdSnapshot.DeletionTimestamp != nil { vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseTerminating condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil diff --git a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready.go b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready.go index 0c278ce6b..8bf62b33f 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready.go @@ -54,7 +54,7 @@ func (h VirtualDiskReadyHandler) Handle(ctx context.Context, vdSnapshot *virtv2. if vdSnapshot.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown - condition.Reason = "" + condition.Reason = conditions.ReasonUnknown.String() condition.Message = "" return reconcile.Result{}, nil } diff --git a/tests/performance/shatal/internal/shatal/modifier.go b/tests/performance/shatal/internal/shatal/modifier.go index 70e539696..d8d459c48 100644 --- a/tests/performance/shatal/internal/shatal/modifier.go +++ b/tests/performance/shatal/internal/shatal/modifier.go @@ -76,7 +76,7 @@ func (s *Modifier) Do(ctx context.Context, vm v1alpha2.VirtualMachine) { vmop := v1alpha2.VirtualMachineOperation{ TypeMeta: metav1.TypeMeta{ - Kind: v1alpha2.VMOPKind, + Kind: v1alpha2.VirtualMachineOperationKind, APIVersion: v1alpha2.Version, }, ObjectMeta: metav1.ObjectMeta{