diff --git a/api/core/v1alpha2/vdscondition/condition.go b/api/core/v1alpha2/vdscondition/condition.go index 60842c3bc..148c357e2 100644 --- a/api/core/v1alpha2/vdscondition/condition.go +++ b/api/core/v1alpha2/vdscondition/condition.go @@ -17,7 +17,11 @@ limitations under the License. package vdscondition // Type represents the various condition types for the `VirtualDiskSnapshot`. -type Type = string +type Type string + +func (s Type) String() string { + return string(s) +} const ( // VirtualDiskReadyType indicates that the source `VirtualDisk` is ready for snapshotting. @@ -28,11 +32,23 @@ const ( type ( // VirtualDiskReadyReason represents the various reasons for the `VirtualDiskReady` condition type. - VirtualDiskReadyReason = string + VirtualDiskReadyReason string // VirtualDiskSnapshotReadyReason represents the various reasons for the `VirtualDiskSnapshotReady` condition type. - VirtualDiskSnapshotReadyReason = string + VirtualDiskSnapshotReadyReason string ) +func (s VirtualDiskReadyReason) String() string { + return string(s) +} + +func (s VirtualDiskSnapshotReadyReason) String() string { + return string(s) +} + +func (s Type) VirtualDiskSnapshotReadyReason() string { + return string(s) +} + const ( // VirtualDiskReady signifies that the source virtual disk is ready for snapshotting, allowing the snapshot process to begin. VirtualDiskReady VirtualDiskReadyReason = "VirtualDiskReady" diff --git a/images/virtualization-artifact/pkg/controller/service/condition.go b/images/virtualization-artifact/pkg/controller/service/condition.go index db36fc91a..d8b9ec424 100644 --- a/images/virtualization-artifact/pkg/controller/service/condition.go +++ b/images/virtualization-artifact/pkg/controller/service/condition.go @@ -20,26 +20,10 @@ import ( "unicode" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" virtv1 "kubevirt.io/api/core/v1" cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1" ) -func GetCondition(condType string, conds []metav1.Condition) (metav1.Condition, bool) { - for _, cond := range conds { - if cond.Type == condType { - return cond, true - } - } - - return metav1.Condition{}, false -} - -func SetCondition(cond metav1.Condition, conditions *[]metav1.Condition) { - meta.SetStatusCondition(conditions, cond) -} - func CapitalizeFirstLetter(s string) string { if s == "" { return "" diff --git a/images/virtualization-artifact/pkg/controller/service/snapshot_service.go b/images/virtualization-artifact/pkg/controller/service/snapshot_service.go index 4e6bb3f8a..f6b3566c5 100644 --- a/images/virtualization-artifact/pkg/controller/service/snapshot_service.go +++ b/images/virtualization-artifact/pkg/controller/service/snapshot_service.go @@ -27,6 +27,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" "github.com/deckhouse/virtualization/api/client/kubeclient" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -53,7 +54,7 @@ func (s *SnapshotService) IsFrozen(vm *virtv2.VirtualMachine) bool { return false } - filesystemReady, _ := GetCondition(vmcondition.TypeFilesystemReady.String(), vm.Status.Conditions) + filesystemReady, _ := conditions.GetCondition(vmcondition.TypeFilesystemReady, vm.Status.Conditions) return filesystemReady.Status == metav1.ConditionFalse && filesystemReady.Reason == vmcondition.ReasonFilesystemFrozen.String() } @@ -63,7 +64,7 @@ func (s *SnapshotService) CanFreeze(vm *virtv2.VirtualMachine) bool { return false } - agentReady, _ := GetCondition(vmcondition.TypeAgentReady.String(), vm.Status.Conditions) + agentReady, _ := conditions.GetCondition(vmcondition.TypeAgentReady, vm.Status.Conditions) return agentReady.Status == metav1.ConditionTrue } diff --git a/images/virtualization-artifact/pkg/controller/service/vm_operation.go b/images/virtualization-artifact/pkg/controller/service/vm_operation.go index 4a93ff74a..bd9b9040c 100644 --- a/images/virtualization-artifact/pkg/controller/service/vm_operation.go +++ b/images/virtualization-artifact/pkg/controller/service/vm_operation.go @@ -235,7 +235,7 @@ func (s VMOperationService) IsComplete(ctx context.Context, vmop *virtv2.Virtual func (s VMOperationService) isAfterSignalSentOrCreation(timestamp time.Time, vmop *virtv2.VirtualMachineOperation) bool { // Use vmop creation time or time from SignalSent condition. signalSentTime := vmop.GetCreationTimestamp().Time - signalSendCond, found := GetCondition(vmopcondition.SignalSentType.String(), vmop.Status.Conditions) + signalSendCond, found := conditions.GetCondition(vmopcondition.SignalSentType, vmop.Status.Conditions) if found && signalSendCond.Status == metav1.ConditionTrue && signalSendCond.LastTransitionTime.After(signalSentTime) { signalSentTime = signalSendCond.LastTransitionTime.Time } 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 0964a1e3c..30aba4297 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go @@ -47,40 +47,34 @@ func NewLifeCycleHandler(snapshotter LifeCycleSnapshotter) *LifeCycleHandler { func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.VirtualDiskSnapshot) (reconcile.Result, error) { log := logger.FromContext(ctx).With(logger.SlogHandler("lifecycle")) - condition, ok := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) - if !ok { - condition = metav1.Condition{ - Type: vdscondition.VirtualDiskSnapshotReadyType, - Status: metav1.ConditionUnknown, - Reason: conditions.ReasonUnknown.String(), - } - } + cb := conditions.NewConditionBuilder(vdscondition.VirtualDiskSnapshotReadyType).Generation(vdSnapshot.Generation) - defer func() { service.SetCondition(condition, &vdSnapshot.Status.Conditions) }() + defer func() { conditions.SetCondition(cb, &vdSnapshot.Status.Conditions) }() vs, err := h.snapshotter.GetVolumeSnapshot(ctx, vdSnapshot.Name, vdSnapshot.Namespace) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } vd, err := h.snapshotter.GetVirtualDisk(ctx, vdSnapshot.Spec.VirtualDiskName, vdSnapshot.Namespace) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } vm, err := getVirtualMachine(ctx, vd, h.snapshotter) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } if vdSnapshot.DeletionTimestamp != nil { vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseTerminating - condition.Status = metav1.ConditionUnknown - condition.Reason = conditions.ReasonUnknown.String() - condition.Message = "" + cb. + Status(metav1.ConditionUnknown). + Reason(conditions.ReasonUnknown). + Message("") return reconcile.Result{}, nil } @@ -91,25 +85,28 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual case virtv2.VirtualDiskSnapshotPhaseReady: if vs == nil || vs.Status == nil || vs.Status.ReadyToUse == nil || !*vs.Status.ReadyToUse { vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseFailed - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.VolumeSnapshotLost - condition.Message = fmt.Sprintf("The underlieng volume snapshot %q is not ready to use.", vdSnapshot.Status.VolumeSnapshotName) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.VolumeSnapshotLost). + Message(fmt.Sprintf("The underlieng volume snapshot %q is not ready to use.", vdSnapshot.Status.VolumeSnapshotName)) return reconcile.Result{Requeue: true}, nil } vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseReady - condition.Status = metav1.ConditionTrue - condition.Reason = vdscondition.VirtualDiskSnapshotReady - condition.Message = "" + cb. + Status(metav1.ConditionTrue). + Reason(vdscondition.VirtualDiskSnapshotReady). + Message("") return reconcile.Result{}, nil } - virtualDiskReadyCondition, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + virtualDiskReadyCondition, _ := conditions.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) if vd == nil || virtualDiskReadyCondition.Status != metav1.ConditionTrue { vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhasePending - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.WaitingForTheVirtualDisk - condition.Message = fmt.Sprintf("Waiting for the virtual disk %q to be ready for snapshotting.", vdSnapshot.Spec.VirtualDiskName) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.WaitingForTheVirtualDisk). + Message(fmt.Sprintf("Waiting for the virtual disk %q to be ready for snapshotting.", vdSnapshot.Spec.VirtualDiskName)) return reconcile.Result{}, nil } @@ -117,16 +114,17 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual if vd.Status.Target.PersistentVolumeClaim != "" { pvc, err = h.snapshotter.GetPersistentVolumeClaim(ctx, vd.Status.Target.PersistentVolumeClaim, vd.Namespace) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } } if pvc == nil || pvc.Status.Phase != corev1.ClaimBound { vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhasePending - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.WaitingForTheVirtualDisk - condition.Message = "Waiting for the virtual disk's pvc to be in phase Bound." + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.WaitingForTheVirtualDisk). + Message("Waiting for the virtual disk's pvc to be in phase Bound.") return reconcile.Result{}, nil } @@ -138,30 +136,32 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual err = h.snapshotter.Freeze(ctx, vm.Name, vm.Namespace) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseInProgress - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.FileSystemFreezing - condition.Message = fmt.Sprintf( - "The virtual machine %q with an attached virtual disk %q is in the process of being frozen for taking a snapshot.", - vm.Name, vdSnapshot.Spec.VirtualDiskName, - ) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.FileSystemFreezing). + Message(fmt.Sprintf( + "The virtual machine %q with an attached virtual disk %q is in the process of being frozen for taking a snapshot.", + vm.Name, vdSnapshot.Spec.VirtualDiskName, + )) return reconcile.Result{}, nil } if vdSnapshot.Spec.RequiredConsistency { vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhasePending - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.PotentiallyInconsistent - condition.Message = fmt.Sprintf( - "The virtual machine %q with an attached virtual disk %q is %s: "+ - "the snapshotting of virtual disk might result in an inconsistent snapshot: "+ - "waiting for the virtual machine to be %s or the disk to be detached", - vm.Name, vd.Name, vm.Status.Phase, virtv2.MachineStopped, - ) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.PotentiallyInconsistent). + Message(fmt.Sprintf( + "The virtual machine %q with an attached virtual disk %q is %s: "+ + "the snapshotting of virtual disk might result in an inconsistent snapshot: "+ + "waiting for the virtual machine to be %s or the disk to be detached", + vm.Name, vd.Name, vm.Status.Phase, virtv2.MachineStopped, + )) return reconcile.Result{}, nil } } @@ -203,31 +203,34 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual vs, err = h.snapshotter.CreateVolumeSnapshot(ctx, vs) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseInProgress vdSnapshot.Status.VolumeSnapshotName = vs.Name - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.Snapshotting - condition.Message = fmt.Sprintf("The snapshotting process for virtual disk %q has started.", vdSnapshot.Spec.VirtualDiskName) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.Snapshotting). + Message(fmt.Sprintf("The snapshotting process for virtual disk %q has started.", vdSnapshot.Spec.VirtualDiskName)) return reconcile.Result{}, nil case vs.Status != nil && vs.Status.Error != nil && vs.Status.Error.Message != nil: log.Debug("The volume snapshot has an error") vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseFailed - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.VirtualDiskSnapshotFailed - condition.Message = fmt.Sprintf("VolumeSnapshot %q has an error: %s.", vs.Name, *vs.Status.Error.Message) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.VirtualDiskSnapshotFailed). + Message(fmt.Sprintf("VolumeSnapshot %q has an error: %s.", vs.Name, *vs.Status.Error.Message)) return reconcile.Result{}, nil case vs.Status == nil || vs.Status.ReadyToUse == nil || !*vs.Status.ReadyToUse: log.Debug("Waiting for the volume snapshot to be ready to use") vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseInProgress - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.Snapshotting - condition.Message = fmt.Sprintf("Waiting fot the volume snapshot %q to be ready to use.", vdSnapshot.Name) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.Snapshotting). + Message(fmt.Sprintf("Waiting fot the volume snapshot %q to be ready to use.", vdSnapshot.Name)) return reconcile.Result{}, nil default: log.Debug("The volume snapshot is ready to use") @@ -241,7 +244,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual var canUnfreeze bool canUnfreeze, err = h.snapshotter.CanUnfreeze(ctx, vdSnapshot.Name, vm) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } @@ -250,16 +253,17 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual err = h.snapshotter.Unfreeze(ctx, vm.Name, vm.Namespace) if err != nil { - setPhaseConditionToFailed(&condition, &vdSnapshot.Status.Phase, err) + setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err) return reconcile.Result{}, err } } } vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhaseReady - condition.Status = metav1.ConditionTrue - condition.Reason = vdscondition.VirtualDiskSnapshotReady - condition.Message = "" + cb. + Status(metav1.ConditionTrue). + Reason(vdscondition.VirtualDiskSnapshotReady). + Message("") return reconcile.Result{}, nil } @@ -286,9 +290,10 @@ func getVirtualMachine(ctx context.Context, vd *virtv2.VirtualDisk, snapshotter } } -func setPhaseConditionToFailed(cond *metav1.Condition, phase *virtv2.VirtualDiskSnapshotPhase, err error) { +func setPhaseConditionToFailed(cb *conditions.ConditionBuilder, phase *virtv2.VirtualDiskSnapshotPhase, err error) { *phase = virtv2.VirtualDiskSnapshotPhaseFailed - cond.Status = metav1.ConditionFalse - cond.Reason = vdscondition.VirtualDiskSnapshotFailed - cond.Message = service.CapitalizeFirstLetter(err.Error()) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.VirtualDiskSnapshotFailed). + Message(service.CapitalizeFirstLetter(err.Error())) } diff --git a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle_test.go b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle_test.go index 56bd06717..661e987f5 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle_test.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle_test.go @@ -26,7 +26,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdscondition" ) @@ -65,7 +65,7 @@ var _ = Describe("LifeCycle handler", func() { Status: virtv2.VirtualDiskSnapshotStatus{ Conditions: []metav1.Condition{ { - Type: vdscondition.VirtualDiskReadyType, + Type: vdscondition.VirtualDiskReadyType.String(), Status: metav1.ConditionTrue, }, }, @@ -98,9 +98,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseInProgress)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.Snapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -119,9 +119,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseFailed)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotFailed)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotFailed.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -135,9 +135,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseInProgress)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.Snapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -154,9 +154,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseReady)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionTrue)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotReady)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotReady.String())) Expect(ready.Message).To(BeEmpty()) }) }) @@ -199,9 +199,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseInProgress)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.FileSystemFreezing)) + Expect(ready.Reason).To(Equal(vdscondition.FileSystemFreezing.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -215,9 +215,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseInProgress)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.Snapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -231,9 +231,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhasePending)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.PotentiallyInconsistent)) + Expect(ready.Reason).To(Equal(vdscondition.PotentiallyInconsistent.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -247,9 +247,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseInProgress)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.Snapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -268,9 +268,9 @@ var _ = Describe("LifeCycle handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) Expect(vdSnapshot.Status.Phase).To(Equal(virtv2.VirtualDiskSnapshotPhaseReady)) - ready, _ := service.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionTrue)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotReady)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotReady.String())) Expect(ready.Message).To(BeEmpty()) }) }) 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 ab53e09e2..50fb138ed 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 @@ -24,7 +24,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdscondition" @@ -41,28 +40,23 @@ func NewVirtualDiskReadyHandler(snapshotter VirtualDiskReadySnapshotter) *Virtua } func (h VirtualDiskReadyHandler) Handle(ctx context.Context, vdSnapshot *virtv2.VirtualDiskSnapshot) (reconcile.Result, error) { - condition, ok := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) - if !ok { - condition = metav1.Condition{ - Type: vdscondition.VirtualDiskReadyType, - Status: metav1.ConditionUnknown, - Reason: conditions.ReasonUnknown.String(), - } - } + cb := conditions.NewConditionBuilder(vdscondition.VirtualDiskReadyType).Generation(vdSnapshot.Generation) - defer func() { service.SetCondition(condition, &vdSnapshot.Status.Conditions) }() + defer func() { conditions.SetCondition(cb, &vdSnapshot.Status.Conditions) }() if vdSnapshot.DeletionTimestamp != nil { - condition.Status = metav1.ConditionUnknown - condition.Reason = conditions.ReasonUnknown.String() - condition.Message = "" + cb. + Status(metav1.ConditionUnknown). + Reason(conditions.ReasonUnknown). + Message("") return reconcile.Result{}, nil } if vdSnapshot.Status.Phase == virtv2.VirtualDiskSnapshotPhaseReady { - condition.Status = metav1.ConditionTrue - condition.Reason = vdscondition.VirtualDiskReady - condition.Message = "" + cb. + Status(metav1.ConditionTrue). + Reason(vdscondition.VirtualDiskReady). + Message("") return reconcile.Result{}, nil } @@ -72,16 +66,18 @@ func (h VirtualDiskReadyHandler) Handle(ctx context.Context, vdSnapshot *virtv2. } if vd == nil { - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.VirtualDiskNotReadyForSnapshotting - condition.Message = fmt.Sprintf("The virtual disk %q not found.", vdSnapshot.Spec.VirtualDiskName) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.VirtualDiskNotReadyForSnapshotting). + Message(fmt.Sprintf("The virtual disk %q not found.", vdSnapshot.Spec.VirtualDiskName)) return reconcile.Result{}, nil } if vd.GetDeletionTimestamp() != nil { - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.VirtualDiskNotReadyForSnapshotting - condition.Message = fmt.Sprintf("The virtual disk %q is in process of deletion.", vd.Name) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.VirtualDiskNotReadyForSnapshotting). + Message(fmt.Sprintf("The virtual disk %q is in process of deletion.", vd.Name)) return reconcile.Result{}, nil } @@ -89,20 +85,23 @@ func (h VirtualDiskReadyHandler) Handle(ctx context.Context, vdSnapshot *virtv2. case virtv2.DiskReady: snapshotting, _ := conditions.GetCondition(vdcondition.SnapshottingType, vd.Status.Conditions) if snapshotting.Status != metav1.ConditionTrue { - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.VirtualDiskNotReadyForSnapshotting - condition.Message = snapshotting.Message + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.VirtualDiskNotReadyForSnapshotting). + Message(snapshotting.Message) return reconcile.Result{}, nil } - condition.Status = metav1.ConditionTrue - condition.Reason = vdscondition.VirtualDiskReady - condition.Message = "" + cb. + Status(metav1.ConditionTrue). + Reason(vdscondition.VirtualDiskReady). + Message("") return reconcile.Result{}, nil default: - condition.Status = metav1.ConditionFalse - condition.Reason = vdscondition.VirtualDiskNotReadyForSnapshotting - condition.Message = fmt.Sprintf("The virtual disk %q is in the %q phase: waiting for it to reach the Ready phase.", vd.Name, vd.Status.Phase) + cb. + Status(metav1.ConditionFalse). + Reason(vdscondition.VirtualDiskNotReadyForSnapshotting). + Message(fmt.Sprintf("The virtual disk %q is in the %q phase: waiting for it to reach the Ready phase.", vd.Name, vd.Status.Phase)) return reconcile.Result{}, nil } } diff --git a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready_test.go b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready_test.go index 880642e16..7874397d5 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready_test.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/virtual_disk_ready_test.go @@ -24,7 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdscondition" @@ -67,9 +67,9 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionTrue)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskReady)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskReady.String())) Expect(ready.Message).To(BeEmpty()) }) }) @@ -83,9 +83,9 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -98,9 +98,9 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -113,9 +113,9 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) @@ -125,7 +125,7 @@ var _ = Describe("VirtualDiskReady handler", func() { vd.Status.Conditions = append(vd.Status.Conditions, metav1.Condition{ Type: vdcondition.SnapshottingType.String(), Status: metav1.ConditionFalse, - Reason: vdscondition.VirtualDiskNotReadyForSnapshotting, + Reason: vdscondition.VirtualDiskNotReadyForSnapshotting.String(), Message: "error", }) return vd, nil @@ -134,9 +134,9 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) - Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) + Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting.String())) Expect(ready.Message).ToNot(BeEmpty()) }) }) diff --git a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/kvvmi_watcher.go b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/kvvmi_watcher.go index 1e6014dc6..9aecc36d6 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/kvvmi_watcher.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/kvvmi_watcher.go @@ -31,7 +31,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" ) @@ -117,8 +117,8 @@ func (w VirtualMachineWatcher) filterUpdateEvents(e event.UpdateEvent) bool { return false } - oldFSReady, _ := service.GetCondition(vmcondition.TypeFilesystemReady.String(), oldKVVMI.Status.Conditions) - newFSReady, _ := service.GetCondition(vmcondition.TypeFilesystemReady.String(), newKVVMI.Status.Conditions) + oldFSReady, _ := conditions.GetCondition(vmcondition.TypeFilesystemReady, oldKVVMI.Status.Conditions) + newFSReady, _ := conditions.GetCondition(vmcondition.TypeFilesystemReady, newKVVMI.Status.Conditions) return oldFSReady.Status != newFSReady.Status } diff --git a/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vm_watcher.go b/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vm_watcher.go index 938e9505b..c721d2c3b 100644 --- a/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vm_watcher.go +++ b/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vm_watcher.go @@ -31,7 +31,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" ) @@ -97,8 +97,8 @@ func (w VirtualMachineWatcher) filterUpdateEvents(e event.UpdateEvent) bool { return false } - oldRunningCondition, _ := service.GetCondition(vmcondition.TypeRunning.String(), oldVM.Status.Conditions) - newRunningCondition, _ := service.GetCondition(vmcondition.TypeRunning.String(), newVM.Status.Conditions) + oldRunningCondition, _ := conditions.GetCondition(vmcondition.TypeRunning, oldVM.Status.Conditions) + newRunningCondition, _ := conditions.GetCondition(vmcondition.TypeRunning, newVM.Status.Conditions) return newRunningCondition.Status != oldRunningCondition.Status } diff --git a/images/virtualization-artifact/pkg/controller/vmip/internal/iplease_handler.go b/images/virtualization-artifact/pkg/controller/vmip/internal/iplease_handler.go index bf77a8b0f..9b431a200 100644 --- a/images/virtualization-artifact/pkg/controller/vmip/internal/iplease_handler.go +++ b/images/virtualization-artifact/pkg/controller/vmip/internal/iplease_handler.go @@ -63,7 +63,7 @@ func (h IPLeaseHandler) Handle(ctx context.Context, state state.VMIPState) (reco if err != nil { return reconcile.Result{}, err } - condition, _ := service.GetCondition(vmipcondition.BoundType.String(), vmipStatus.Conditions) + condition, _ := conditions.GetCondition(vmipcondition.BoundType, vmipStatus.Conditions) switch { case lease == nil && vmipStatus.Address != "" && condition.Reason != vmipcondition.VirtualMachineIPAddressLeaseAlreadyExists.String(): diff --git a/images/virtualization-artifact/pkg/controller/vmip/internal/state/state.go b/images/virtualization-artifact/pkg/controller/vmip/internal/state/state.go index 3e392ea1f..ffc3f93f0 100644 --- a/images/virtualization-artifact/pkg/controller/vmip/internal/state/state.go +++ b/images/virtualization-artifact/pkg/controller/vmip/internal/state/state.go @@ -25,9 +25,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/indexer" "github.com/deckhouse/virtualization-controller/pkg/controller/ipam" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/vmip/internal/util" "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -87,7 +87,7 @@ func (s *state) VirtualMachineIPLease(ctx context.Context) (*virtv2.VirtualMachi } for i, lease := range leases.Items { - boundCondition, exist := service.GetCondition(vmiplcondition.BoundType.String(), lease.Status.Conditions) + boundCondition, exist := conditions.GetCondition(vmiplcondition.BoundType, lease.Status.Conditions) if exist && boundCondition.Status == metav1.ConditionTrue { s.lease = &leases.Items[i] break diff --git a/images/virtualization-artifact/pkg/controller/vmip/vmip_webhook.go b/images/virtualization-artifact/pkg/controller/vmip/vmip_webhook.go index 8dce9fc99..690755a63 100644 --- a/images/virtualization-artifact/pkg/controller/vmip/vmip_webhook.go +++ b/images/virtualization-artifact/pkg/controller/vmip/vmip_webhook.go @@ -28,6 +28,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "github.com/deckhouse/deckhouse/pkg/log" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/vmip/internal/util" "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -112,7 +113,7 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj "old.address", oldVmip.Spec.StaticIP, "new.address", newVmip.Spec.StaticIP, ) - boundCondition, exist := service.GetCondition(vmipcondition.BoundType.String(), oldVmip.Status.Conditions) + boundCondition, exist := conditions.GetCondition(vmipcondition.BoundType, oldVmip.Status.Conditions) if exist && boundCondition.Status == metav1.ConditionTrue { if oldVmip.Spec.Type == v1alpha2.VirtualMachineIPAddressTypeAuto && newVmip.Spec.Type == v1alpha2.VirtualMachineIPAddressTypeStatic { v.log.Info("Change the VirtualMachineIP address type to 'Auto' from 'Static' for ip: ", "address", newVmip.Spec.StaticIP) diff --git a/images/virtualization-artifact/pkg/controller/vmiplease/internal/retention_handler.go b/images/virtualization-artifact/pkg/controller/vmiplease/internal/retention_handler.go index 5058c86a2..b7c93c297 100644 --- a/images/virtualization-artifact/pkg/controller/vmiplease/internal/retention_handler.go +++ b/images/virtualization-artifact/pkg/controller/vmiplease/internal/retention_handler.go @@ -23,7 +23,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/vmiplease/internal/state" "github.com/deckhouse/virtualization-controller/pkg/logger" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmipcondition" @@ -61,7 +61,7 @@ func (h *RetentionHandler) Handle(ctx context.Context, state state.VMIPLeaseStat } leaseStatus := &lease.Status - boundCondition, _ := service.GetCondition(vmipcondition.BoundType.String(), leaseStatus.Conditions) + boundCondition, _ := conditions.GetCondition(vmipcondition.BoundType, leaseStatus.Conditions) if boundCondition.Reason == vmiplcondition.Released.String() { currentTime := time.Now().UTC() diff --git a/images/virtualization-artifact/pkg/controller/vmop/internal/operation.go b/images/virtualization-artifact/pkg/controller/vmop/internal/operation.go index 5e7186580..62ad939bc 100644 --- a/images/virtualization-artifact/pkg/controller/vmop/internal/operation.go +++ b/images/virtualization-artifact/pkg/controller/vmop/internal/operation.go @@ -72,7 +72,7 @@ func (h OperationHandler) Handle(ctx context.Context, s state.VMOperationState) // VirtualMachineOperation should contain Complete condition in Unknown state to perform operation. // Other statuses may indicate waiting state, e.g. non-existent VM or other VMOPs in progress. - completeCondition, found := service.GetCondition(vmopcondition.TypeCompleted.String(), changed.Status.Conditions) + completeCondition, found := conditions.GetCondition(vmopcondition.TypeCompleted, changed.Status.Conditions) if !found { log.Debug("Skip operation, no Complete condition found", "vmop.phase", changed.Status.Phase) return reconcile.Result{}, nil diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go index 67e9d7295..8f0f4c636 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go @@ -353,7 +353,7 @@ func (h LifeCycleHandler) ensureVirtualDiskSnapshots(ctx context.Context, vmSnap //nolint:staticcheck vdSnapshotReady, _ := conditions.GetCondition(conditions.DeprecatedWrappedString(vdscondition.VirtualDiskSnapshotReadyType), vdSnapshot.Status.Conditions) - if vdSnapshotReady.Reason == vdscondition.VirtualDiskSnapshotFailed || vdSnapshot.Status.Phase == virtv2.VirtualDiskSnapshotPhaseFailed { + if vdSnapshotReady.Reason == vdscondition.VirtualDiskSnapshotFailed.String() || vdSnapshot.Status.Phase == virtv2.VirtualDiskSnapshotPhaseFailed { return nil, fmt.Errorf("the virtual disk snapshot %q is failed: %w. %s", vdSnapshot.Name, ErrCannotTakeSnapshot, vdSnapshotReady.Message) } diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go index 24c4e5b5b..f48af5c79 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle_test.go @@ -27,7 +27,6 @@ import ( "k8s.io/utils/ptr" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" @@ -145,10 +144,10 @@ var _ = Describe("LifeCycle handler", func() { Context("The block devices of the virtual machine are not in the consistent state", func() { It("The BlockDevicesReady condition of the virtual machine isn't True", func() { snapshotter.GetVirtualMachineFunc = func(_ context.Context, _, _ string) (*virtv2.VirtualMachine, error) { - service.SetCondition(metav1.Condition{ - Type: vmcondition.TypeBlockDevicesReady.String(), - Status: metav1.ConditionFalse, - }, &vm.Status.Conditions) + cb := conditions.NewConditionBuilder(vmcondition.TypeBlockDevicesReady). + Generation(vm.Generation). + Status(metav1.ConditionFalse) + conditions.SetCondition(cb, &vm.Status.Conditions) return vm, nil } h := NewLifeCycleHandler(snapshotter, storer) @@ -180,10 +179,10 @@ var _ = Describe("LifeCycle handler", func() { It("The virtual disk is not Ready", func() { snapshotter.GetVirtualDiskFunc = func(_ context.Context, _, _ string) (*virtv2.VirtualDisk, error) { - service.SetCondition(metav1.Condition{ - Type: vdcondition.Ready.String(), - Status: metav1.ConditionFalse, - }, &vd.Status.Conditions) + cb := conditions.NewConditionBuilder(vdcondition.Ready). + Generation(vd.Generation). + Status(metav1.ConditionFalse) + conditions.SetCondition(cb, &vd.Status.Conditions) return vd, nil } h := NewLifeCycleHandler(snapshotter, storer) @@ -199,11 +198,11 @@ var _ = Describe("LifeCycle handler", func() { It("The virtual disk is the process of Resizing", func() { snapshotter.GetVirtualDiskFunc = func(_ context.Context, _, _ string) (*virtv2.VirtualDisk, error) { - service.SetCondition(metav1.Condition{ - Type: vdcondition.Resized.String(), - Status: metav1.ConditionFalse, - Reason: vdcondition.InProgress.String(), - }, &vd.Status.Conditions) + cb := conditions.NewConditionBuilder(vdcondition.Resized). + Generation(vd.Generation). + Status(metav1.ConditionFalse). + Reason(vdcondition.InProgress) + conditions.SetCondition(cb, &vd.Status.Conditions) return vd, nil } h := NewLifeCycleHandler(snapshotter, storer) diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/virtual_machine_ready.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/virtual_machine_ready.go index 67643d8eb..b2abe2ffa 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/virtual_machine_ready.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/virtual_machine_ready.go @@ -24,7 +24,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmscondition" @@ -85,7 +84,7 @@ func (h VirtualMachineReadyHandler) Handle(ctx context.Context, vmSnapshot *virt switch vm.Status.Phase { case virtv2.MachineRunning, virtv2.MachineStopped: - snapshotting, _ := service.GetCondition(vmcondition.TypeSnapshotting.String(), vm.Status.Conditions) + snapshotting, _ := conditions.GetCondition(vmcondition.TypeSnapshotting, vm.Status.Conditions) if snapshotting.Status != metav1.ConditionTrue { cb.Status(metav1.ConditionFalse).Reason(vmscondition.VirtualMachineNotReadyForSnapshotting) if snapshotting.Message == "" { diff --git a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/watcher/vm_watcher.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/watcher/vm_watcher.go index 182bf9e3b..a8c559429 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/watcher/vm_watcher.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/watcher/vm_watcher.go @@ -32,8 +32,8 @@ 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" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" ) @@ -98,15 +98,15 @@ func (w VirtualMachineWatcher) filterUpdateEvents(e event.UpdateEvent) bool { return false } - oldFSReady, _ := service.GetCondition(vmcondition.TypeFilesystemReady.String(), oldVM.Status.Conditions) - newFSReady, _ := service.GetCondition(vmcondition.TypeFilesystemReady.String(), newVM.Status.Conditions) + oldFSReady, _ := conditions.GetCondition(vmcondition.TypeFilesystemReady, oldVM.Status.Conditions) + newFSReady, _ := conditions.GetCondition(vmcondition.TypeFilesystemReady, newVM.Status.Conditions) if oldFSReady.Reason != newFSReady.Reason { return true } - oldSnapshotting, _ := service.GetCondition(vmcondition.TypeSnapshotting.String(), oldVM.Status.Conditions) - newSnapshotting, _ := service.GetCondition(vmcondition.TypeSnapshotting.String(), newVM.Status.Conditions) + oldSnapshotting, _ := conditions.GetCondition(vmcondition.TypeSnapshotting, oldVM.Status.Conditions) + newSnapshotting, _ := conditions.GetCondition(vmcondition.TypeSnapshotting, newVM.Status.Conditions) return oldSnapshotting.Status != newSnapshotting.Status } diff --git a/images/virtualization-artifact/pkg/monitoring/metrics/virtualmachine/data_metric.go b/images/virtualization-artifact/pkg/monitoring/metrics/virtualmachine/data_metric.go index 428b3824c..a1fc12989 100644 --- a/images/virtualization-artifact/pkg/monitoring/metrics/virtualmachine/data_metric.go +++ b/images/virtualization-artifact/pkg/monitoring/metrics/virtualmachine/data_metric.go @@ -22,7 +22,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/monitoring/metrics/promutil" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" @@ -62,11 +62,11 @@ func newDataMetric(vm *virtv2.VirtualMachine) *dataMetric { awaitingRestartToApplyConfiguration bool configurationApplied bool ) - if cond, found := service.GetCondition(vmcondition.TypeAwaitingRestartToApplyConfiguration.String(), + if cond, found := conditions.GetCondition(vmcondition.TypeAwaitingRestartToApplyConfiguration, vm.Status.Conditions); found && cond.Status == metav1.ConditionTrue { awaitingRestartToApplyConfiguration = true } - if cond, found := service.GetCondition(vmcondition.TypeConfigurationApplied.String(), + if cond, found := conditions.GetCondition(vmcondition.TypeConfigurationApplied, vm.Status.Conditions); found && cond.Status == metav1.ConditionTrue { configurationApplied = true }