diff --git a/images/virtualization-artifact/pkg/controller/conditions/builder.go b/images/virtualization-artifact/pkg/controller/conditions/builder.go index 141a289fd..2c4c17171 100644 --- a/images/virtualization-artifact/pkg/controller/conditions/builder.go +++ b/images/virtualization-artifact/pkg/controller/conditions/builder.go @@ -17,7 +17,6 @@ limitations under the License. package conditions import ( - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -36,7 +35,7 @@ func HasCondition(conditionType Stringer, conditions []metav1.Condition) bool { } func SetCondition(c Conder, conditions *[]metav1.Condition) { - meta.SetStatusCondition(conditions, c.Condition()) + ApplyCondition(c.Condition(), conditions) } func GetCondition(condType Stringer, conditions []metav1.Condition) (metav1.Condition, bool) { diff --git a/images/virtualization-artifact/pkg/controller/service/condition.go b/images/virtualization-artifact/pkg/controller/conditions/condition.go similarity index 86% rename from images/virtualization-artifact/pkg/controller/service/condition.go rename to images/virtualization-artifact/pkg/controller/conditions/condition.go index db36fc91a..80a8fd030 100644 --- a/images/virtualization-artifact/pkg/controller/service/condition.go +++ b/images/virtualization-artifact/pkg/controller/conditions/condition.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package service +package conditions import ( "unicode" @@ -26,7 +26,7 @@ import ( cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1" ) -func GetCondition(condType string, conds []metav1.Condition) (metav1.Condition, bool) { +func GetConditionByType(condType string, conds []metav1.Condition) (metav1.Condition, bool) { for _, cond := range conds { if cond.Type == condType { return cond, true @@ -36,8 +36,12 @@ func GetCondition(condType string, conds []metav1.Condition) (metav1.Condition, return metav1.Condition{}, false } -func SetCondition(cond metav1.Condition, conditions *[]metav1.Condition) { - meta.SetStatusCondition(conditions, cond) +func ApplyCondition(cond metav1.Condition, conditions *[]metav1.Condition) { + if cond.Status != metav1.ConditionUnknown { + meta.SetStatusCondition(conditions, cond) + } else { + meta.RemoveStatusCondition(conditions, cond.Type) + } } func CapitalizeFirstLetter(s string) string { diff --git a/images/virtualization-artifact/pkg/controller/conditions/manager.go b/images/virtualization-artifact/pkg/controller/conditions/manager.go index 3fa04b85a..847bb05bc 100644 --- a/images/virtualization-artifact/pkg/controller/conditions/manager.go +++ b/images/virtualization-artifact/pkg/controller/conditions/manager.go @@ -19,7 +19,6 @@ package conditions import ( "slices" - "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -53,7 +52,7 @@ func (m *Manager) Add(c metav1.Condition) (addedCondition bool) { } func (m *Manager) Update(c metav1.Condition) { - meta.SetStatusCondition(&m.conds, c) + ApplyCondition(c, &m.conds) } func (m *Manager) Generate() []metav1.Condition { diff --git a/images/virtualization-artifact/pkg/controller/cvi/cvi_webhook.go b/images/virtualization-artifact/pkg/controller/cvi/cvi_webhook.go index 098517935..5989709ab 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/cvi_webhook.go +++ b/images/virtualization-artifact/pkg/controller/cvi/cvi_webhook.go @@ -25,7 +25,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - "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/cvicondition" ) @@ -63,7 +63,7 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj return nil, nil } - ready, _ := service.GetCondition(cvicondition.ReadyType, newCVI.Status.Conditions) + ready, _ := conditions.GetConditionByType(cvicondition.ReadyType, newCVI.Status.Conditions) if newCVI.Status.Phase == virtv2.ImageReady || ready.Status == metav1.ConditionTrue { return nil, fmt.Errorf("ClusterVirtualImage is in a Ready state: configuration changes are not available") } diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/datasource_ready.go b/images/virtualization-artifact/pkg/controller/cvi/internal/datasource_ready.go index 444758a02..7d38383bc 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/datasource_ready.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/datasource_ready.go @@ -24,8 +24,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/cvi/internal/source" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/cvicondition" ) @@ -41,15 +41,12 @@ func NewDatasourceReadyHandler(sources *source.Sources) *DatasourceReadyHandler } func (h DatasourceReadyHandler) Handle(ctx context.Context, cvi *virtv2.ClusterVirtualImage) (reconcile.Result, error) { - condition, ok := service.GetCondition(cvicondition.DatasourceReadyType, cvi.Status.Conditions) - if !ok { - condition = metav1.Condition{ - Type: cvicondition.DatasourceReadyType, - Status: metav1.ConditionUnknown, - } + condition := metav1.Condition{ + Type: cvicondition.DatasourceReadyType, + Status: metav1.ConditionUnknown, } - defer func() { service.SetCondition(condition, &cvi.Status.Conditions) }() + defer func() { conditions.ApplyCondition(condition, &cvi.Status.Conditions) }() if cvi.DeletionTimestamp != nil { return reconcile.Result{}, nil @@ -72,27 +69,27 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, cvi *virtv2.ClusterV case errors.Is(err, source.ErrSecretNotFound): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ContainerRegistrySecretNotFound - condition.Message = service.CapitalizeFirstLetter(err.Error()) + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) return reconcile.Result{}, nil case errors.As(err, &source.ImageNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ImageNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error()) + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) return reconcile.Result{}, nil case errors.As(err, &source.ClusterImageNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ClusterImageNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error()) + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) return reconcile.Result{}, nil case errors.As(err, &source.VirtualDiskNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.VirtualDiskNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error()) + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) return reconcile.Result{}, nil case errors.As(err, &source.VirtualDiskAttachedToRunningVMError{}): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.VirtualDiskNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error()) + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/cvi/internal/life_cycle.go index 10dea1b14..ccb29bc03 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/life_cycle.go @@ -24,8 +24,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/cvi/internal/source" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/cvicondition" ) @@ -43,15 +43,7 @@ func NewLifeCycleHandler(sources *source.Sources, client client.Client) *LifeCyc } func (h LifeCycleHandler) Handle(ctx context.Context, cvi *virtv2.ClusterVirtualImage) (reconcile.Result, error) { - readyCondition, ok := service.GetCondition(cvicondition.ReadyType, cvi.Status.Conditions) - if !ok { - readyCondition = metav1.Condition{ - Type: cvicondition.ReadyType, - Status: metav1.ConditionUnknown, - } - - service.SetCondition(readyCondition, &cvi.Status.Conditions) - } + readyCondition, _ := conditions.GetConditionByType(cvicondition.ReadyType, cvi.Status.Conditions) if cvi.DeletionTimestamp != nil { cvi.Status.Phase = virtv2.ImageTerminating @@ -62,7 +54,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, cvi *virtv2.ClusterVirtual cvi.Status.Phase = virtv2.ImagePending } - dataSourceReadyCondition, exists := service.GetCondition(cvicondition.DatasourceReadyType, cvi.Status.Conditions) + dataSourceReadyCondition, exists := conditions.GetConditionByType(cvicondition.DatasourceReadyType, cvi.Status.Conditions) if !exists { return reconcile.Result{}, fmt.Errorf("condition %s not found, but required", cvicondition.DatasourceReadyType) } diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/source/http.go b/images/virtualization-artifact/pkg/controller/cvi/internal/source/http.go index 9f992dfa3..ad00b73a3 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/source/http.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/source/http.go @@ -26,6 +26,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -59,8 +60,12 @@ func NewHTTPDataSource( func (ds HTTPDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "http") - condition, _ := service.GetCondition(cvicondition.ReadyType, cvi.Status.Conditions) - defer func() { service.SetCondition(condition, &cvi.Status.Conditions) }() + condition := metav1.Condition{ + Type: cvicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + + defer func() { conditions.ApplyCondition(condition, &cvi.Status.Conditions) }() supgen := supplements.NewGenerator(common.CVIShortName, cvi.Name, ds.controllerNamespace, cvi.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -121,7 +126,7 @@ func (ds HTTPDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualIma case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -150,12 +155,12 @@ func (ds HTTPDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualIma case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref.go b/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref.go index 36ba7f0ae..1d82cbf84 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref.go @@ -29,6 +29,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -72,8 +73,12 @@ func NewObjectRefDataSource( func (ds ObjectRefDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "objectref") - condition, _ := service.GetCondition(cvicondition.ReadyType, cvi.Status.Conditions) - defer func() { service.SetCondition(condition, &cvi.Status.Conditions) }() + condition := metav1.Condition{ + Type: cvicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + + defer func() { conditions.ApplyCondition(condition, &cvi.Status.Conditions) }() switch cvi.Spec.DataSource.ObjectRef.Kind { case virtv2.VirtualImageKind: @@ -172,7 +177,7 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtu case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -214,12 +219,12 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtu case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vd.go b/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vd.go index 606efd0f7..02f6b07da 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vd.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vd.go @@ -28,6 +28,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" cc "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -122,7 +123,7 @@ func (ds ObjectRefVirtualDisk) Sync(ctx context.Context, cvi *virtv2.ClusterVirt case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -150,12 +151,12 @@ func (ds ObjectRefVirtualDisk) Sync(ctx context.Context, cvi *virtv2.ClusterVirt case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vi_on_pvc.go b/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vi_on_pvc.go index bfea22122..57fe61392 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vi_on_pvc.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/source/object_ref_vi_on_pvc.go @@ -26,6 +26,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -116,7 +117,7 @@ func (ds ObjectRefVirtualImageOnPvc) Sync(ctx context.Context, cvi *virtv2.Clust case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -144,12 +145,12 @@ func (ds ObjectRefVirtualImageOnPvc) Sync(ctx context.Context, cvi *virtv2.Clust case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/source/registry.go b/images/virtualization-artifact/pkg/controller/cvi/internal/source/registry.go index 77d16f42c..528391e0b 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/source/registry.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/source/registry.go @@ -29,6 +29,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -66,8 +67,12 @@ func NewRegistryDataSource( func (ds RegistryDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "registry") - condition, _ := service.GetCondition(cvicondition.ReadyType, cvi.Status.Conditions) - defer func() { service.SetCondition(condition, &cvi.Status.Conditions) }() + condition := metav1.Condition{ + Type: cvicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + + defer func() { conditions.ApplyCondition(condition, &cvi.Status.Conditions) }() supgen := supplements.NewGenerator(common.CVIShortName, cvi.Name, ds.controllerNamespace, cvi.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -128,7 +133,7 @@ func (ds RegistryDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtua case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -156,12 +161,12 @@ func (ds RegistryDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtua case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/source/sources.go b/images/virtualization-artifact/pkg/controller/cvi/internal/source/sources.go index 992c9351d..7cb1565e7 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/source/sources.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/source/sources.go @@ -25,6 +25,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" cc "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "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/cvicondition" @@ -111,5 +112,5 @@ func setPhaseConditionToFailed(ready *metav1.Condition, phase *virtv2.ImagePhase *phase = virtv2.ImageFailed ready.Status = metav1.ConditionFalse ready.Reason = cvicondition.ProvisioningFailed - ready.Message = service.CapitalizeFirstLetter(err.Error()) + ready.Message = conditions.CapitalizeFirstLetter(err.Error()) } diff --git a/images/virtualization-artifact/pkg/controller/cvi/internal/source/upload.go b/images/virtualization-artifact/pkg/controller/cvi/internal/source/upload.go index 4b6183704..5f5a2ffa1 100644 --- a/images/virtualization-artifact/pkg/controller/cvi/internal/source/upload.go +++ b/images/virtualization-artifact/pkg/controller/cvi/internal/source/upload.go @@ -26,6 +26,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/controller/uploader" @@ -59,8 +60,12 @@ func NewUploadDataSource( func (ds UploadDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "upload") - condition, _ := service.GetCondition(cvicondition.ReadyType, cvi.Status.Conditions) - defer func() { service.SetCondition(condition, &cvi.Status.Conditions) }() + condition := metav1.Condition{ + Type: cvicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + + defer func() { conditions.ApplyCondition(condition, &cvi.Status.Conditions) }() supgen := supplements.NewGenerator(common.CVIShortName, cvi.Name, ds.controllerNamespace, cvi.UID) pod, err := ds.uploaderService.GetPod(ctx, supgen) @@ -127,7 +132,7 @@ func (ds UploadDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualI case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -156,12 +161,12 @@ func (ds UploadDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualI case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/service/disk_service.go b/images/virtualization-artifact/pkg/controller/service/disk_service.go index cba868a4f..1aede81ba 100644 --- a/images/virtualization-artifact/pkg/controller/service/disk_service.go +++ b/images/virtualization-artifact/pkg/controller/service/disk_service.go @@ -38,6 +38,7 @@ import ( dvutil "github.com/deckhouse/virtualization-controller/pkg/common/datavolume" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/kvbuilder" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/dvcr" @@ -457,7 +458,7 @@ func (s DiskService) CheckImportProcess(ctx context.Context, dv *cdiv1.DataVolum return nil } - dvRunning := GetDataVolumeCondition(cdiv1.DataVolumeRunning, dv.Status.Conditions) + dvRunning := conditions.GetDataVolumeCondition(cdiv1.DataVolumeRunning, dv.Status.Conditions) if dvRunning == nil || dvRunning.Status != corev1.ConditionFalse { return nil } @@ -481,12 +482,12 @@ func (s DiskService) CheckImportProcess(ctx context.Context, dv *cdiv1.DataVolum } if cdiImporterPrime != nil { - podInitializedCond, ok := GetPodCondition(corev1.PodInitialized, cdiImporterPrime.Status.Conditions) + podInitializedCond, ok := conditions.GetPodCondition(corev1.PodInitialized, cdiImporterPrime.Status.Conditions) if ok && podInitializedCond.Status == corev1.ConditionFalse && strings.Contains(podInitializedCond.Reason, "Error") { return fmt.Errorf("%w; %s error %s: %s", ErrDataVolumeNotRunning, key.String(), podInitializedCond.Reason, podInitializedCond.Message) } - podScheduledCond, ok := GetPodCondition(corev1.PodScheduled, cdiImporterPrime.Status.Conditions) + podScheduledCond, ok := conditions.GetPodCondition(corev1.PodScheduled, cdiImporterPrime.Status.Conditions) if ok && podScheduledCond.Status == corev1.ConditionFalse && strings.Contains(podScheduledCond.Reason, "Error") { return fmt.Errorf("%w; %s error %s: %s", ErrDataVolumeNotRunning, key.String(), podScheduledCond.Reason, podScheduledCond.Message) } diff --git a/images/virtualization-artifact/pkg/controller/service/snapshot_service.go b/images/virtualization-artifact/pkg/controller/service/snapshot_service.go index 4e6bb3f8a..7310ca967 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.GetConditionByType(vmcondition.TypeFilesystemReady.String(), 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.GetConditionByType(vmcondition.TypeAgentReady.String(), vm.Status.Conditions) return agentReady.Status == metav1.ConditionTrue } diff --git a/images/virtualization-artifact/pkg/controller/service/stat_service.go b/images/virtualization-artifact/pkg/controller/service/stat_service.go index e116be689..b8f2a4eda 100644 --- a/images/virtualization-artifact/pkg/controller/service/stat_service.go +++ b/images/virtualization-artifact/pkg/controller/service/stat_service.go @@ -30,6 +30,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common" cc "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/monitoring" "github.com/deckhouse/virtualization-controller/pkg/imageformat" "github.com/deckhouse/virtualization-controller/pkg/util" @@ -106,12 +107,12 @@ func (s StatService) CheckPod(pod *corev1.Pod) error { return errors.New("nil pod") } - podInitializedCond, ok := GetPodCondition(corev1.PodInitialized, pod.Status.Conditions) + podInitializedCond, ok := conditions.GetPodCondition(corev1.PodInitialized, pod.Status.Conditions) if ok && podInitializedCond.Status == corev1.ConditionFalse { return fmt.Errorf("provisioning Pod %s/%s is %w: %s", pod.Namespace, pod.Name, ErrNotInitialized, podInitializedCond.Message) } - podScheduledCond, ok := GetPodCondition(corev1.PodScheduled, pod.Status.Conditions) + podScheduledCond, ok := conditions.GetPodCondition(corev1.PodScheduled, pod.Status.Conditions) if ok && podScheduledCond.Status == corev1.ConditionFalse { return fmt.Errorf("provisioning Pod %s/%s is %w: %s", pod.Namespace, pod.Name, ErrNotScheduled, podScheduledCond.Message) } diff --git a/images/virtualization-artifact/pkg/controller/service/vm_operation.go b/images/virtualization-artifact/pkg/controller/service/vm_operation.go index 2ca1a1b35..4d3b29166 100644 --- a/images/virtualization-artifact/pkg/controller/service/vm_operation.go +++ b/images/virtualization-artifact/pkg/controller/service/vm_operation.go @@ -27,6 +27,7 @@ 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/powerstate" "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" "github.com/deckhouse/virtualization/api/client/kubeclient" @@ -234,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.GetConditionByType(vmopcondition.SignalSentType.String(), 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/vd/internal/datasource_ready.go b/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready.go index 795a02d68..b8fec40f6 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/datasource_ready.go @@ -24,7 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "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/vd/internal/source" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" @@ -43,15 +43,12 @@ func NewDatasourceReadyHandler(blank source.Handler, sources Sources) *Datasourc } func (h DatasourceReadyHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { - condition, ok := service.GetCondition(vdcondition.DatasourceReadyType, vd.Status.Conditions) - if !ok { - condition = metav1.Condition{ - Type: vdcondition.DatasourceReadyType, - Status: metav1.ConditionUnknown, - } + condition := metav1.Condition{ + Type: vdcondition.DatasourceReadyType, + Status: metav1.ConditionUnknown, } - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() if vd.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown @@ -61,6 +58,7 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, vd *virtv2.VirtualDi } var ds source.Handler + var ok bool if vd.Spec.DataSource == nil { ds = h.blank } else { @@ -80,22 +78,22 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, vd *virtv2.VirtualDi case errors.Is(err, source.ErrSecretNotFound): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ContainerRegistrySecretNotFound - condition.Message = service.CapitalizeFirstLetter(err.Error()) + "." + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) + "." return reconcile.Result{}, nil case errors.As(err, &source.ImageNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ImageNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error()) + "." + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) + "." return reconcile.Result{}, nil case errors.As(err, &source.ClusterImageNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ClusterImageNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error()) + "." + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) + "." return reconcile.Result{}, nil case errors.As(err, &source.VirtualDiskSnapshotNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.VirtualDiskSnapshotNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error()) + "." + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) + "." return reconcile.Result{}, nil default: return reconcile.Result{}, fmt.Errorf("validation failed for data source %s: %w", ds.Name(), err) 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..50bc3326c 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 @@ -51,11 +51,7 @@ func TestDatasourceReadyHandler_Handle(t *testing.T) { handler := NewDatasourceReadyHandler(nil, nil) _, err := handler.Handle(ctx, &vd) require.NoError(t, err) - - 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, len(vd.Status.Conditions), 0) }) t.Run("VirtualDisk with Blank DataSource", func(t *testing.T) { diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/vd/internal/life_cycle.go index 5b8134d15..d60859380 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/life_cycle.go @@ -24,7 +24,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "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/vd/internal/source" "github.com/deckhouse/virtualization-controller/pkg/logger" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -48,14 +48,11 @@ func NewLifeCycleHandler(blank source.Handler, sources *source.Sources, client c func (h LifeCycleHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { log := logger.FromContext(ctx).With(logger.SlogHandler("lifecycle")) - readyCondition, ok := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) + readyCondition, ok := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) if !ok { readyCondition = metav1.Condition{ - Type: vdcondition.ReadyType, Status: metav1.ConditionUnknown, } - - service.SetCondition(readyCondition, &vd.Status.Conditions) } if vd.DeletionTimestamp != nil { @@ -67,7 +64,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (r vd.Status.Phase = virtv2.DiskPending } - dataSourceReadyCondition, exists := service.GetCondition(vdcondition.DatasourceReadyType, vd.Status.Conditions) + dataSourceReadyCondition, exists := conditions.GetConditionByType(vdcondition.DatasourceReadyType, vd.Status.Conditions) if !exists { return reconcile.Result{}, fmt.Errorf("condition %s not found, but required", vdcondition.DatasourceReadyType) } diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go b/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go index b1d863bc2..f43887d4d 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/resizing.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/controller/common" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/logger" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -46,7 +46,7 @@ func NewResizingHandler(diskService DiskService) *ResizingHandler { func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { log := logger.FromContext(ctx).With(logger.SlogHandler("resizing")) - condition, ok := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + condition, ok := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) if !ok { condition = metav1.Condition{ Type: vdcondition.ResizedType, @@ -54,7 +54,7 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re } } - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() if vd.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown @@ -63,7 +63,7 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re return reconcile.Result{}, nil } - readyCondition, ok := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) + readyCondition, ok := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) if !ok || readyCondition.Status != metav1.ConditionTrue { condition.Status = metav1.ConditionUnknown condition.Reason = "" @@ -100,7 +100,7 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re // Synchronize VirtualDisk size with PVC size. vd.Status.Capacity = pvcStatusSize.String() - pvcResizing := service.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, pvc.Status.Conditions) + pvcResizing := conditions.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, pvc.Status.Conditions) if pvcResizing != nil && pvcResizing.Status == corev1.ConditionTrue { log.Info("Resizing is in progress", "msg", pvcResizing.Message) @@ -113,7 +113,7 @@ func (h ResizingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re // Expected disk size is GREATER THAN expected pvc size: resize needed, resizing to a larger size. if vdSpecSize != nil && vdSpecSize.Cmp(pvcSpecSize) == 1 { - snapshotting, _ := service.GetCondition(vdcondition.SnapshottingType, vd.Status.Conditions) + snapshotting, _ := conditions.GetConditionByType(vdcondition.SnapshottingType, vd.Status.Conditions) if snapshotting.Status == metav1.ConditionTrue { condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ResizingNotAvailable diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/resizing_test.go b/images/virtualization-artifact/pkg/controller/vd/internal/resizing_test.go index 2d0e8758f..299173208 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/resizing_test.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/resizing_test.go @@ -26,7 +26,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/logger" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -100,7 +100,7 @@ var _ = Describe("Resizing handler Run", func() { _, err := h.Handle(testContext(), vd) Expect(err).To(BeNil()) - resized, _ := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + resized, _ := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) Expect(resized.Status).To(Equal(metav1.ConditionFalse)) Expect(resized.Reason).To(Equal(vdcondition.InProgress)) }) @@ -112,7 +112,7 @@ var _ = Describe("Resizing handler Run", func() { _, err := h.Handle(testContext(), vd) Expect(err).To(BeNil()) - resized, _ := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + resized, _ := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) Expect(resized.Status).To(Equal(metav1.ConditionFalse)) Expect(resized.Reason).To(Equal(vdcondition.ResizingNotRequested)) }) @@ -124,7 +124,7 @@ var _ = Describe("Resizing handler Run", func() { _, err := h.Handle(testContext(), vd) Expect(err).To(BeNil()) - resized, _ := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + resized, _ := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) Expect(resized.Status).To(Equal(metav1.ConditionFalse)) Expect(resized.Reason).To(Equal(vdcondition.ResizingNotRequested)) }) @@ -134,7 +134,7 @@ var _ = Describe("Resizing handler Run", func() { _, err := h.Handle(testContext(), vd) Expect(err).To(BeNil()) - resized, _ := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + resized, _ := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) Expect(resized.Status).To(Equal(metav1.ConditionFalse)) Expect(resized.Reason).To(Equal(vdcondition.ResizingNotRequested)) }) @@ -146,7 +146,7 @@ var _ = Describe("Resizing handler Run", func() { _, err := h.Handle(testContext(), vd) Expect(err).To(BeNil()) - resized, _ := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + resized, _ := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) Expect(resized.Status).To(Equal(metav1.ConditionFalse)) Expect(resized.Reason).To(Equal(vdcondition.InProgress)) }) @@ -162,7 +162,7 @@ var _ = Describe("Resizing handler Run", func() { _, err := h.Handle(testContext(), vd) Expect(err).To(BeNil()) - resized, _ := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + resized, _ := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) Expect(resized.Status).To(Equal(metav1.ConditionTrue)) Expect(resized.Reason).To(Equal(vdcondition.Resized)) }) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go b/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go index 629342fa8..258f9cbcb 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/snapshotting.go @@ -22,6 +22,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "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" @@ -38,15 +39,12 @@ func NewSnapshottingHandler(diskService *service.DiskService) *SnapshottingHandl } func (h SnapshottingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { - condition, ok := service.GetCondition(vdcondition.SnapshottingType, vd.Status.Conditions) - if !ok { - condition = metav1.Condition{ - Type: vdcondition.SnapshottingType, - Status: metav1.ConditionUnknown, - } + condition := metav1.Condition{ + Type: vdcondition.SnapshottingType, + Status: metav1.ConditionUnknown, } - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() if vd.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown @@ -55,7 +53,7 @@ func (h SnapshottingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) return reconcile.Result{}, nil } - readyCondition, ok := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) + readyCondition, ok := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) if !ok || readyCondition.Status != metav1.ConditionTrue { condition.Status = metav1.ConditionUnknown condition.Reason = "" @@ -77,7 +75,7 @@ func (h SnapshottingHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) continue } - resized, _ := service.GetCondition(vdcondition.ResizedType, vd.Status.Conditions) + resized, _ := conditions.GetConditionByType(vdcondition.ResizedType, vd.Status.Conditions) if resized.Reason == vdcondition.InProgress { condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.SnapshottingNotAvailable diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/blank.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/blank.go index 9495557dc..27fda3160 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/blank.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/blank.go @@ -26,6 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/logger" @@ -53,8 +54,14 @@ func NewBlankDataSource( func (ds BlankDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, blankDataSource) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vdcondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) dv, err := ds.diskService.GetDataVolume(ctx, supgen) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/http.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/http.go index fbd026810..25d0d293d 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/http.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/http.go @@ -30,6 +30,7 @@ import ( cc "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -66,8 +67,14 @@ func NewHTTPDataSource( func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, httpDataSource) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vdcondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -143,12 +150,12 @@ func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (reco case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -178,7 +185,7 @@ func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (reco case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_cvi.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_cvi.go index 463c6ab34..20172204a 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_cvi.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_cvi.go @@ -29,6 +29,7 @@ import ( common2 "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/imageformat" @@ -63,8 +64,8 @@ func (ds ObjectRefClusterVirtualImage) Sync(ctx context.Context, vd *virtv2.Virt log, ctx := logger.GetDataSourceContext(ctx, objectRefDataSource) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, _ := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) dv, err := ds.diskService.GetDataVolume(ctx, supgen) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vdsnapshot.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vdsnapshot.go index 845e3b5b9..9de89ff6c 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vdsnapshot.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vdsnapshot.go @@ -28,6 +28,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/logger" @@ -55,8 +56,8 @@ func (ds ObjectRefVirtualDiskSnapshot) Sync(ctx context.Context, vd *virtv2.Virt supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, _ := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() pvc, err := ds.diskService.GetPersistentVolumeClaim(ctx, supgen) if err != nil { diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_dvcr.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_dvcr.go index ed03ab17c..19a92155b 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_dvcr.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_dvcr.go @@ -29,6 +29,7 @@ import ( common2 "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/imageformat" @@ -63,8 +64,8 @@ func (ds ObjectRefVirtualImageDVCR) Sync(ctx context.Context, vd *virtv2.Virtual log, ctx := logger.GetDataSourceContext(ctx, objectRefDataSource) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, _ := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) dv, err := ds.diskService.GetDataVolume(ctx, supgen) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_pvc.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_pvc.go index 53d17aa00..3a8b94b52 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_pvc.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vi_pvc.go @@ -27,6 +27,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/imageformat" @@ -53,8 +54,8 @@ func (ds ObjectRefVirtualImagePVC) Sync(ctx context.Context, vd *virtv2.VirtualD log, _ := logger.GetDataSourceContext(ctx, objectRefDataSource) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, _ := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) dv, err := ds.diskService.GetDataVolume(ctx, supgen) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/registry.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/registry.go index 76a004a8d..b3e1259e7 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/registry.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/registry.go @@ -32,6 +32,7 @@ import ( cc "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -72,8 +73,14 @@ func NewRegistryDataSource( func (ds RegistryDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, registryDataSource) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vdcondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -149,12 +156,12 @@ func (ds RegistryDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) ( case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -183,7 +190,7 @@ func (ds RegistryDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) ( case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/sources.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/sources.go index 0dafc5205..1e1982dd0 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/sources.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/sources.go @@ -29,6 +29,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" cc "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -193,7 +194,7 @@ func setPhaseConditionForPVCProvisioningDisk( vd.Status.Phase = virtv2.DiskFailed condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error()) + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) return nil default: return err @@ -220,5 +221,5 @@ func setPhaseConditionToFailed(ready *metav1.Condition, phase *virtv2.DiskPhase, *phase = virtv2.DiskFailed ready.Status = metav1.ConditionFalse ready.Reason = vdcondition.ProvisioningFailed - ready.Message = service.CapitalizeFirstLetter(err.Error()) + "." + ready.Message = conditions.CapitalizeFirstLetter(err.Error()) + "." } diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/source/upload.go b/images/virtualization-artifact/pkg/controller/vd/internal/source/upload.go index 51dbd524a..74a71399c 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/source/upload.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/source/upload.go @@ -30,6 +30,7 @@ import ( common2 "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/controller/uploader" @@ -66,8 +67,14 @@ func NewUploadDataSource( func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, uploadDataSource) - condition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) - defer func() { service.SetCondition(condition, &vd.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vdcondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vd.Status.Conditions) }() supgen := supplements.NewGenerator(common.VDShortName, vd.Name, vd.Namespace, vd.UID) pod, err := ds.uploaderService.GetPod(ctx, supgen) @@ -149,12 +156,12 @@ func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (re case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -210,7 +217,7 @@ func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (re case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vdcondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/stats.go b/images/virtualization-artifact/pkg/controller/vd/internal/stats.go index 870e8e460..6e378ed1a 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/stats.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/stats.go @@ -25,6 +25,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -48,14 +49,14 @@ func NewStatsHandler(stat *service.StatService, importer *service.ImporterServic func (h StatsHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) { sinceCreation := time.Since(vd.CreationTimestamp.Time).Truncate(time.Second) - datasourceReady, _ := service.GetCondition(vdcondition.DatasourceReadyType, vd.Status.Conditions) + datasourceReady, _ := conditions.GetConditionByType(vdcondition.DatasourceReadyType, vd.Status.Conditions) if datasourceReady.Status == metav1.ConditionTrue && vd.Status.Stats.CreationDuration.WaitingForDependencies == nil { vd.Status.Stats.CreationDuration.WaitingForDependencies = &metav1.Duration{ Duration: sinceCreation, } } - ready, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) if ready.Status == metav1.ConditionTrue && vd.Status.Stats.CreationDuration.TotalProvisioning == nil { duration := sinceCreation diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/validators/iso_source_validator.go b/images/virtualization-artifact/pkg/controller/vd/internal/validators/iso_source_validator.go index 1b645248c..c232df91c 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/validators/iso_source_validator.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/validators/iso_source_validator.go @@ -23,7 +23,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "github.com/deckhouse/virtualization-controller/pkg/controller" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/source" "github.com/deckhouse/virtualization-controller/pkg/imageformat" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -60,7 +60,7 @@ func (v *ISOSourceValidator) ValidateCreate(ctx context.Context, vd *virtv2.Virt if imageformat.IsISO(dvcrDataSource.GetFormat()) { return admission.Warnings{ - service.CapitalizeFirstLetter(source.ErrISOSourceNotSupported.Error()), + conditions.CapitalizeFirstLetter(source.ErrISOSourceNotSupported.Error()), }, nil } } @@ -91,7 +91,7 @@ func (v *ISOSourceValidator) ValidateUpdate(ctx context.Context, _, newVD *virtv if imageformat.IsISO(dvcrDataSource.GetFormat()) { return admission.Warnings{ - service.CapitalizeFirstLetter(source.ErrISOSourceNotSupported.Error()), + conditions.CapitalizeFirstLetter(source.ErrISOSourceNotSupported.Error()), }, nil } } diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/validators/pvc_size_validator.go b/images/virtualization-artifact/pkg/controller/vd/internal/validators/pvc_size_validator.go index d7dab22bb..cb73e6969 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/validators/pvc_size_validator.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/validators/pvc_size_validator.go @@ -29,6 +29,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "github.com/deckhouse/virtualization-controller/pkg/controller" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -111,7 +112,7 @@ func (v *PVCSizeValidator) ValidateCreate(ctx context.Context, vd *virtv2.Virtua return nil, nil case errors.Is(err, service.ErrInsufficientPVCSize): return admission.Warnings{ - service.CapitalizeFirstLetter(err.Error()), + conditions.CapitalizeFirstLetter(err.Error()), }, nil default: return nil, err @@ -131,7 +132,7 @@ func (v *PVCSizeValidator) ValidateUpdate(ctx context.Context, oldVD, newVD *vir oldSize = *s } - ready, _ := service.GetCondition(vdcondition.ReadyType, newVD.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdcondition.ReadyType, newVD.Status.Conditions) if s := newVD.Spec.PersistentVolumeClaim.Size; s != nil { newSize = *s } else if ready.Status == metav1.ConditionTrue || newVD.Status.Phase != virtv2.DiskPending && newVD.Status.Phase != virtv2.DiskProvisioning { @@ -208,7 +209,7 @@ func (v *PVCSizeValidator) ValidateUpdate(ctx context.Context, oldVD, newVD *vir return nil, nil case errors.Is(err, service.ErrInsufficientPVCSize): return admission.Warnings{ - service.CapitalizeFirstLetter(err.Error()), + conditions.CapitalizeFirstLetter(err.Error()), }, nil default: return nil, err diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/validators/spec_changes_validator.go b/images/virtualization-artifact/pkg/controller/vd/internal/validators/spec_changes_validator.go index 158eafda4..8e30dec51 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/validators/spec_changes_validator.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/validators/spec_changes_validator.go @@ -24,7 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - "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" ) @@ -44,7 +44,7 @@ func (v *SpecChangesValidator) ValidateUpdate(_ context.Context, oldVD, newVD *v return nil, nil } - ready, _ := service.GetCondition(vdcondition.ReadyType, newVD.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdcondition.ReadyType, newVD.Status.Conditions) if ready.Status == metav1.ConditionTrue || newVD.Status.Phase == virtv2.DiskReady || newVD.Status.Phase == virtv2.DiskLost || newVD.Status.Phase == virtv2.DiskTerminating { if !reflect.DeepEqual(oldVD.Spec.DataSource, newVD.Spec.DataSource) { return nil, fmt.Errorf("VirtualDisk has already been attached: data source cannot be changed while disk is attached to VirtualMachine") diff --git a/images/virtualization-artifact/pkg/controller/vd/vd_reconciler.go b/images/virtualization-artifact/pkg/controller/vd/vd_reconciler.go index 0b0e7cace..1506e4d66 100644 --- a/images/virtualization-artifact/pkg/controller/vd/vd_reconciler.go +++ b/images/virtualization-artifact/pkg/controller/vd/vd_reconciler.go @@ -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/vd/internal/watcher" "github.com/deckhouse/virtualization-controller/pkg/controller/watchers" @@ -159,7 +160,7 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr return true } - dvRunning := service.GetDataVolumeCondition(cdiv1.DataVolumeRunning, newDV.Status.Conditions) + dvRunning := conditions.GetDataVolumeCondition(cdiv1.DataVolumeRunning, newDV.Status.Conditions) return dvRunning != nil && dvRunning.Reason == "Error" }, }, @@ -190,8 +191,8 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr return true } - if service.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, oldPVC.Status.Conditions) != nil || - service.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, newPVC.Status.Conditions) != nil { + if conditions.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, oldPVC.Status.Conditions) != nil || + conditions.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, newPVC.Status.Conditions) != nil { return true } 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 99c121d68..5b13b248c 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/life_cycle.go @@ -27,6 +27,7 @@ import ( "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/logger" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -46,7 +47,7 @@ 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) + condition, ok := conditions.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) if !ok { condition = metav1.Condition{ Type: vdscondition.VirtualDiskSnapshotReadyType, @@ -54,7 +55,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual } } - defer func() { service.SetCondition(condition, &vdSnapshot.Status.Conditions) }() + defer func() { conditions.ApplyCondition(condition, &vdSnapshot.Status.Conditions) }() vs, err := h.snapshotter.GetVolumeSnapshot(ctx, vdSnapshot.Name, vdSnapshot.Namespace) if err != nil { @@ -102,7 +103,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual return reconcile.Result{}, nil } - virtualDiskReadyCondition, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + virtualDiskReadyCondition, _ := conditions.GetConditionByType(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) if vd == nil || virtualDiskReadyCondition.Status != metav1.ConditionTrue { vdSnapshot.Status.Phase = virtv2.VirtualDiskSnapshotPhasePending condition.Status = metav1.ConditionFalse @@ -288,5 +289,5 @@ func setPhaseConditionToFailed(cond *metav1.Condition, phase *virtv2.VirtualDisk *phase = virtv2.VirtualDiskSnapshotPhaseFailed cond.Status = metav1.ConditionFalse cond.Reason = vdscondition.VirtualDiskSnapshotFailed - cond.Message = service.CapitalizeFirstLetter(err.Error()) + cond.Message = conditions.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..75822332a 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" ) @@ -98,7 +98,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) Expect(ready.Message).ToNot(BeEmpty()) @@ -119,7 +119,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotFailed)) Expect(ready.Message).ToNot(BeEmpty()) @@ -135,7 +135,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) Expect(ready.Message).ToNot(BeEmpty()) @@ -154,7 +154,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionTrue)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotReady)) Expect(ready.Message).To(BeEmpty()) @@ -199,7 +199,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.FileSystemFreezing)) Expect(ready.Message).ToNot(BeEmpty()) @@ -215,7 +215,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) Expect(ready.Message).ToNot(BeEmpty()) @@ -231,7 +231,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.PotentiallyInconsistent)) Expect(ready.Message).ToNot(BeEmpty()) @@ -247,7 +247,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.Snapshotting)) Expect(ready.Message).ToNot(BeEmpty()) @@ -268,7 +268,7 @@ 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.GetConditionByType(vdscondition.VirtualDiskSnapshotReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionTrue)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskSnapshotReady)) 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 b1094e7a0..53366a67c 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 @@ -23,7 +23,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "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" @@ -40,15 +40,12 @@ 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, - } + condition := metav1.Condition{ + Type: vdscondition.VirtualDiskReadyType, + Status: metav1.ConditionUnknown, } - defer func() { service.SetCondition(condition, &vdSnapshot.Status.Conditions) }() + defer func() { conditions.ApplyCondition(condition, &vdSnapshot.Status.Conditions) }() if vdSnapshot.DeletionTimestamp != nil { condition.Status = metav1.ConditionUnknown @@ -85,7 +82,7 @@ func (h VirtualDiskReadyHandler) Handle(ctx context.Context, vdSnapshot *virtv2. switch vd.Status.Phase { case virtv2.DiskReady: - snapshotting, _ := service.GetCondition(vdcondition.SnapshottingType, vd.Status.Conditions) + snapshotting, _ := conditions.GetConditionByType(vdcondition.SnapshottingType, vd.Status.Conditions) if snapshotting.Status != metav1.ConditionTrue { condition.Status = metav1.ConditionFalse condition.Reason = vdscondition.VirtualDiskNotReadyForSnapshotting 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 1cc5b3a34..42536bebe 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,7 +67,7 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionTrue)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskReady)) Expect(ready.Message).To(BeEmpty()) @@ -83,7 +83,7 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) Expect(ready.Message).ToNot(BeEmpty()) @@ -98,7 +98,7 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) Expect(ready.Message).ToNot(BeEmpty()) @@ -113,7 +113,7 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) Expect(ready.Message).ToNot(BeEmpty()) @@ -134,7 +134,7 @@ var _ = Describe("VirtualDiskReady handler", func() { _, err := h.Handle(testContext(), vdSnapshot) Expect(err).To(BeNil()) - ready, _ := service.GetCondition(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) + ready, _ := conditions.GetConditionByType(vdscondition.VirtualDiskReadyType, vdSnapshot.Status.Conditions) Expect(ready.Status).To(Equal(metav1.ConditionFalse)) Expect(ready.Reason).To(Equal(vdscondition.VirtualDiskNotReadyForSnapshotting)) 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..d7377862a 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.GetConditionByType(vmcondition.TypeFilesystemReady.String(), oldKVVMI.Status.Conditions) + newFSReady, _ := conditions.GetConditionByType(vmcondition.TypeFilesystemReady.String(), newKVVMI.Status.Conditions) return oldFSReady.Status != newFSReady.Status } diff --git a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/vd_watcher.go b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/vd_watcher.go index 6e390a4c3..298b892db 100644 --- a/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/vd_watcher.go +++ b/images/virtualization-artifact/pkg/controller/vdsnapshot/internal/watcher/vd_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/vdcondition" ) @@ -107,8 +107,8 @@ func (w VirtualDiskWatcher) filterUpdateEvents(e event.UpdateEvent) bool { return true } - oldSnapshotting, _ := service.GetCondition(vdcondition.SnapshottingType, oldVD.Status.Conditions) - newSnapshotting, _ := service.GetCondition(vdcondition.SnapshottingType, newVD.Status.Conditions) + oldSnapshotting, _ := conditions.GetConditionByType(vdcondition.SnapshottingType, oldVD.Status.Conditions) + newSnapshotting, _ := conditions.GetConditionByType(vdcondition.SnapshottingType, newVD.Status.Conditions) return oldSnapshotting.Status != newSnapshotting.Status || oldSnapshotting.Reason != newSnapshotting.Reason } diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/datasource_ready.go b/images/virtualization-artifact/pkg/controller/vi/internal/datasource_ready.go index 9d11ed922..19f73474a 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/datasource_ready.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/datasource_ready.go @@ -24,7 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "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/vi/internal/source" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/cvicondition" @@ -42,15 +42,12 @@ func NewDatasourceReadyHandler(sources *source.Sources) *DatasourceReadyHandler } func (h DatasourceReadyHandler) Handle(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { - condition, ok := service.GetCondition(vicondition.DatasourceReadyType, vi.Status.Conditions) - if !ok { - condition = metav1.Condition{ - Type: vicondition.DatasourceReadyType, - Status: metav1.ConditionUnknown, - } + condition := metav1.Condition{ + Type: vicondition.DatasourceReadyType, + Status: metav1.ConditionUnknown, } - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() if vi.DeletionTimestamp != nil { return reconcile.Result{}, nil @@ -73,27 +70,27 @@ func (h DatasourceReadyHandler) Handle(ctx context.Context, vi *virtv2.VirtualIm case errors.Is(err, source.ErrSecretNotFound): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ContainerRegistrySecretNotFound - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.As(err, &source.ImageNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ImageNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.As(err, &source.ClusterImageNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ClusterImageNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.As(err, &source.VirtualDiskNotReadyError{}): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.VirtualDiskNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.As(err, &source.VirtualDiskAttachedToRunningVMError{}): condition.Status = metav1.ConditionFalse condition.Reason = cvicondition.VirtualDiskNotReady - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/vi/internal/life_cycle.go index 4b1004d2c..afd6dff0f 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/life_cycle.go @@ -24,7 +24,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "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/vi/internal/source" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vicondition" @@ -43,14 +43,11 @@ func NewLifeCycleHandler(sources *source.Sources, client client.Client) *LifeCyc } func (h LifeCycleHandler) Handle(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { - readyCondition, ok := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) + readyCondition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) if !ok { readyCondition = metav1.Condition{ - Type: vicondition.ReadyType, Status: metav1.ConditionUnknown, } - - service.SetCondition(readyCondition, &vi.Status.Conditions) } if vi.DeletionTimestamp != nil { @@ -62,7 +59,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vi *virtv2.VirtualImage) ( vi.Status.Phase = virtv2.ImagePending } - dataSourceReadyCondition, exists := service.GetCondition(vicondition.DatasourceReadyType, vi.Status.Conditions) + dataSourceReadyCondition, exists := conditions.GetConditionByType(vicondition.DatasourceReadyType, vi.Status.Conditions) if !exists { return reconcile.Result{}, fmt.Errorf("condition %s not found, but required", vicondition.DatasourceReadyType) } diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/source/http.go b/images/virtualization-artifact/pkg/controller/vi/internal/source/http.go index a22e2dafc..e18f8c14d 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/source/http.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/source/http.go @@ -31,6 +31,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/common/datasource" cc "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -67,8 +68,14 @@ func NewHTTPDataSource( func (ds HTTPDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "http") - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() supgen := supplements.NewGenerator(cc.VIShortName, vi.Name, vi.Namespace, vi.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -129,7 +136,7 @@ func (ds HTTPDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.VirtualImag case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -178,8 +185,14 @@ func (ds HTTPDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.VirtualImag func (ds HTTPDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "http") - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() supgen := supplements.NewGenerator(cc.VIShortName, vi.Name, vi.Namespace, vi.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -275,7 +288,7 @@ func (ds HTTPDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualImage case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref.go b/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref.go index a840af752..8c0e80542 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref.go @@ -33,6 +33,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -81,8 +82,14 @@ func NewObjectRefDataSource( func (ds ObjectRefDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, objectRefDataSource) - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() switch vi.Spec.DataSource.ObjectRef.Kind { case virtv2.VirtualImageKind: @@ -240,8 +247,14 @@ func (ds ObjectRefDataSource) StoreToPVC(ctx context.Context, vi *virtv2.Virtual func (ds ObjectRefDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "objectref") - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() switch vi.Spec.DataSource.ObjectRef.Kind { case virtv2.VirtualImageKind: @@ -343,7 +356,7 @@ func (ds ObjectRefDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.Virtua case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vd.go b/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vd.go index 7dc5ea460..ce2d64626 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vd.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vd.go @@ -31,6 +31,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -128,7 +129,7 @@ func (ds ObjectRefVirtualDisk) StoreToDVCR(ctx context.Context, vi *virtv2.Virtu case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -156,12 +157,12 @@ func (ds ObjectRefVirtualDisk) StoreToDVCR(ctx context.Context, vi *virtv2.Virtu case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningNotStarted - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vi_on_pvc.go b/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vi_on_pvc.go index cecbc6f15..572b517eb 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vi_on_pvc.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/source/object_ref_vi_on_pvc.go @@ -30,6 +30,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -132,7 +133,7 @@ func (ds ObjectRefDataVirtualImageOnPVC) StoreToDVCR(ctx context.Context, vi, vi case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/source/registry.go b/images/virtualization-artifact/pkg/controller/vi/internal/source/registry.go index ee338f64a..d757b87f5 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/source/registry.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/source/registry.go @@ -33,6 +33,7 @@ import ( cc "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/importer" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" @@ -75,8 +76,14 @@ func NewRegistryDataSource( func (ds RegistryDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, registryDataSource) - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() supgen := supplements.NewGenerator(common.VIShortName, vi.Name, vi.Namespace, vi.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -171,7 +178,7 @@ func (ds RegistryDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualI case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -248,8 +255,14 @@ func (ds RegistryDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualI func (ds RegistryDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "registry") - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() supgen := supplements.NewGenerator(common.VIShortName, vi.Name, vi.Namespace, vi.UID) pod, err := ds.importerService.GetPod(ctx, supgen) @@ -309,7 +322,7 @@ func (ds RegistryDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.Virtual case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/source/sources.go b/images/virtualization-artifact/pkg/controller/vi/internal/source/sources.go index 3a7f2bad2..ac544eaef 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/source/sources.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/source/sources.go @@ -28,6 +28,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" cc "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -132,7 +133,7 @@ func setPhaseConditionToFailed(ready *metav1.Condition, phase *virtv2.ImagePhase *phase = virtv2.ImageFailed ready.Status = metav1.ConditionFalse ready.Reason = vicondition.ProvisioningFailed - ready.Message = service.CapitalizeFirstLetter(err.Error()) + ready.Message = conditions.CapitalizeFirstLetter(err.Error()) } func setPhaseConditionForPVCProvisioningImage( @@ -163,7 +164,7 @@ func setPhaseConditionForPVCProvisioningImage( vi.Status.Phase = virtv2.ImageProvisioning condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error()) + condition.Message = conditions.CapitalizeFirstLetter(err.Error()) return nil case errors.Is(err, service.ErrStorageClassNotFound): vi.Status.Phase = virtv2.ImageProvisioning @@ -189,12 +190,12 @@ func setPhaseConditionFromPodError(ready *metav1.Condition, vi *virtv2.VirtualIm case errors.Is(err, service.ErrNotInitialized), errors.Is(err, service.ErrNotScheduled): ready.Status = metav1.ConditionFalse ready.Reason = vicondition.ProvisioningNotStarted - ready.Message = service.CapitalizeFirstLetter(err.Error() + ".") + ready.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return nil case errors.Is(err, service.ErrProvisioningFailed): ready.Status = metav1.ConditionFalse ready.Reason = vicondition.ProvisioningFailed - ready.Message = service.CapitalizeFirstLetter(err.Error() + ".") + ready.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return nil default: return err diff --git a/images/virtualization-artifact/pkg/controller/vi/internal/source/upload.go b/images/virtualization-artifact/pkg/controller/vi/internal/source/upload.go index 480841f4d..bda69f18b 100644 --- a/images/virtualization-artifact/pkg/controller/vi/internal/source/upload.go +++ b/images/virtualization-artifact/pkg/controller/vi/internal/source/upload.go @@ -31,6 +31,7 @@ import ( cc "github.com/deckhouse/virtualization-controller/pkg/common" "github.com/deckhouse/virtualization-controller/pkg/common/datasource" "github.com/deckhouse/virtualization-controller/pkg/controller/common" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/supplements" "github.com/deckhouse/virtualization-controller/pkg/controller/uploader" @@ -69,8 +70,14 @@ func NewUploadDataSource( func (ds UploadDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, uploadDataSource) - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() supgen := supplements.NewGenerator(common.VIShortName, vi.Name, vi.Namespace, vi.UID) pod, err := ds.uploaderService.GetPod(ctx, supgen) @@ -200,7 +207,7 @@ func (ds UploadDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualIma case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err @@ -280,8 +287,14 @@ func (ds UploadDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualIma func (ds UploadDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.VirtualImage) (reconcile.Result, error) { log, ctx := logger.GetDataSourceContext(ctx, "upload") - condition, _ := service.GetCondition(vicondition.ReadyType, vi.Status.Conditions) - defer func() { service.SetCondition(condition, &vi.Status.Conditions) }() + condition, ok := conditions.GetConditionByType(vicondition.ReadyType, vi.Status.Conditions) + if !ok { + condition = metav1.Condition{ + Type: vicondition.ReadyType, + Status: metav1.ConditionUnknown, + } + } + defer func() { conditions.ApplyCondition(condition, &vi.Status.Conditions) }() supgen := supplements.NewGenerator(common.VIShortName, vi.Name, vi.Namespace, vi.UID) pod, err := ds.uploaderService.GetPod(ctx, supgen) @@ -347,7 +360,7 @@ func (ds UploadDataSource) StoreToDVCR(ctx context.Context, vi *virtv2.VirtualIm case errors.Is(err, service.ErrProvisioningFailed): condition.Status = metav1.ConditionFalse condition.Reason = vicondition.ProvisioningFailed - condition.Message = service.CapitalizeFirstLetter(err.Error() + ".") + condition.Message = conditions.CapitalizeFirstLetter(err.Error() + ".") return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vi/vi_reconciler.go b/images/virtualization-artifact/pkg/controller/vi/vi_reconciler.go index b34fa5f88..44d661caa 100644 --- a/images/virtualization-artifact/pkg/controller/vi/vi_reconciler.go +++ b/images/virtualization-artifact/pkg/controller/vi/vi_reconciler.go @@ -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/watchers" "github.com/deckhouse/virtualization-controller/pkg/logger" @@ -157,7 +158,7 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr return true } - dvRunning := service.GetDataVolumeCondition(cdiv1.DataVolumeRunning, newDV.Status.Conditions) + dvRunning := conditions.GetDataVolumeCondition(cdiv1.DataVolumeRunning, newDV.Status.Conditions) return dvRunning != nil && dvRunning.Reason == "Error" }, }, @@ -188,8 +189,8 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr return true } - if service.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, oldPVC.Status.Conditions) != nil || - service.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, newPVC.Status.Conditions) != nil { + if conditions.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, oldPVC.Status.Conditions) != nil || + conditions.GetPersistentVolumeClaimCondition(corev1.PersistentVolumeClaimResizing, newPVC.Status.Conditions) != nil { return true } diff --git a/images/virtualization-artifact/pkg/controller/vi/vi_webhook.go b/images/virtualization-artifact/pkg/controller/vi/vi_webhook.go index a96e2baa8..9e52aef6a 100644 --- a/images/virtualization-artifact/pkg/controller/vi/vi_webhook.go +++ b/images/virtualization-artifact/pkg/controller/vi/vi_webhook.go @@ -25,7 +25,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - "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/vicondition" ) @@ -63,7 +63,7 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj return nil, nil } - ready, _ := service.GetCondition(vicondition.ReadyType, newVI.Status.Conditions) + ready, _ := conditions.GetConditionByType(vicondition.ReadyType, newVI.Status.Conditions) if newVI.Status.Phase == virtv2.ImageReady || ready.Status == metav1.ConditionTrue { return nil, fmt.Errorf("VirtualImage is in the Ready state: spec is immutable now") } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/agent.go b/images/virtualization-artifact/pkg/controller/vm/internal/agent.go index a12420949..23eb0314e 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/agent.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/agent.go @@ -31,11 +31,6 @@ import ( const nameAgentHandler = "AgentHandler" -var agentConditions = []vmcondition.Type{ - vmcondition.TypeAgentReady, - vmcondition.TypeAgentVersionNotSupported, -} - func NewAgentHandler() *AgentHandler { return &AgentHandler{} } @@ -49,10 +44,6 @@ func (h *AgentHandler) Handle(ctx context.Context, s state.VirtualMachineState) current := s.VirtualMachine().Current() changed := s.VirtualMachine().Changed() - if update := addAllUnknown(changed, agentConditions...); update { - return reconcile.Result{Requeue: true}, nil - } - if isDeletion(current) { return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/block_device.go b/images/virtualization-artifact/pkg/controller/vm/internal/block_device.go index 4b1dab110..be9fa39e2 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/block_device.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/block_device.go @@ -71,10 +71,6 @@ func (h *BlockDeviceHandler) Handle(ctx context.Context, s state.VirtualMachineS current := s.VirtualMachine().Current() changed := s.VirtualMachine().Changed() - if update := addAllUnknown(changed, vmcondition.TypeBlockDevicesReady); update { - return reconcile.Result{Requeue: true}, nil - } - //nolint:staticcheck mgr := conditions.NewManager(changed.Status.Conditions) cb := conditions.NewConditionBuilder(vmcondition.TypeBlockDevicesReady). diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/class.go b/images/virtualization-artifact/pkg/controller/vm/internal/class.go index 0f86d3d02..f66c85e51 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/class.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/class.go @@ -57,10 +57,6 @@ func (h *ClassHandler) Handle(ctx context.Context, s state.VirtualMachineState) current := s.VirtualMachine().Current() changed := s.VirtualMachine().Changed() - if update := addAllUnknown(changed, vmcondition.TypeClassReady); update { - return reconcile.Result{Requeue: true}, nil - } - if isDeletion(current) { return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/filesystem.go b/images/virtualization-artifact/pkg/controller/vm/internal/filesystem.go index 8458f3669..ebdb303b0 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/filesystem.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/filesystem.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" "github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" ) @@ -44,10 +43,6 @@ func (h *FilesystemHandler) Handle(ctx context.Context, s state.VirtualMachineSt changed := s.VirtualMachine().Changed() - if update := addAllUnknown(changed, vmcondition.TypeFilesystemReady); update { - return reconcile.Result{Requeue: true}, nil - } - if isDeletion(changed) { return reconcile.Result{}, nil } @@ -68,7 +63,7 @@ func (h *FilesystemHandler) Handle(ctx context.Context, s state.VirtualMachineSt return reconcile.Result{}, nil } - agentReady, _ := service.GetCondition(vmcondition.TypeAgentReady.String(), changed.Status.Conditions) + agentReady, _ := conditions.GetConditionByType(vmcondition.TypeAgentReady.String(), changed.Status.Conditions) if agentReady.Status != metav1.ConditionTrue { cb.Status(metav1.ConditionUnknown).Reason(vmcondition.ReasonUnknown) return reconcile.Result{}, nil diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/ipam.go b/images/virtualization-artifact/pkg/controller/vm/internal/ipam.go index 9cd1c9eb1..01a7fb7b7 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/ipam.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/ipam.go @@ -71,10 +71,6 @@ func (h *IPAMHandler) Handle(ctx context.Context, s state.VirtualMachineState) ( current := s.VirtualMachine().Current() changed := s.VirtualMachine().Changed() - if update := addAllUnknown(changed, vmcondition.TypeIPAddressReady); update { - return reconcile.Result{Requeue: true}, nil - } - //nolint:staticcheck mgr := conditions.NewManager(changed.Status.Conditions) cb := conditions.NewConditionBuilder(vmcondition.TypeIPAddressReady). diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/lifecycle.go b/images/virtualization-artifact/pkg/controller/vm/internal/lifecycle.go index 94e1ff1a6..b2fab9e3b 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/lifecycle.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/lifecycle.go @@ -30,20 +30,12 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/controller/common" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" - "github.com/deckhouse/virtualization-controller/pkg/controller/service" "github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state" "github.com/deckhouse/virtualization-controller/pkg/logger" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" ) -var lifeCycleConditions = []vmcondition.Type{ - vmcondition.TypeRunning, - vmcondition.TypeMigrating, - vmcondition.TypeMigratable, - vmcondition.TypePodStarted, -} - const nameLifeCycleHandler = "LifeCycleHandler" func NewLifeCycleHandler(client client.Client, recorder record.EventRecorder) *LifeCycleHandler { @@ -82,11 +74,6 @@ func (h *LifeCycleHandler) Handle(ctx context.Context, s state.VirtualMachineSta return reconcile.Result{}, nil } - if updated := addAllUnknown(changed, lifeCycleConditions...); updated || changed.Status.Phase == "" { - changed.Status.Phase = virtv2.MachinePending - return reconcile.Result{Requeue: true}, nil - } - kvvm, err := s.KVVM(ctx) if err != nil { return reconcile.Result{}, err @@ -156,7 +143,7 @@ func (h *LifeCycleHandler) syncMigrationState(vm *virtv2.VirtualMachine, kvvm *v cbMigratable := conditions.NewConditionBuilder(vmcondition.TypeMigratable).Generation(vm.GetGeneration()) if kvvm != nil { - liveMigratable := service.GetKVVMCondition(string(virtv1.VirtualMachineInstanceIsMigratable), kvvm.Status.Conditions) + liveMigratable := conditions.GetKVVMCondition(string(virtv1.VirtualMachineInstanceIsMigratable), kvvm.Status.Conditions) if liveMigratable != nil && liveMigratable.Status == corev1.ConditionFalse && liveMigratable.Reason == virtv1.VirtualMachineInstanceReasonDisksNotMigratable { cbMigratable.Status(metav1.ConditionFalse). Reason(vmcondition.ReasonNotMigratable). @@ -193,7 +180,7 @@ func (h *LifeCycleHandler) syncPodStarted(vm *virtv2.VirtualMachine, kvvm *virtv if kvvm != nil { // Try to extract error from kvvm PodScheduled condition. - cond := service.GetKVVMCondition(string(corev1.PodScheduled), kvvm.Status.Conditions) + cond := conditions.GetKVVMCondition(string(corev1.PodScheduled), kvvm.Status.Conditions) if cond != nil && cond.Status == corev1.ConditionFalse && cond.Message != "" { cb.Status(metav1.ConditionFalse). Reason(vmcondition.ReasonPodNotStarted). @@ -208,7 +195,7 @@ func (h *LifeCycleHandler) syncPodStarted(vm *virtv2.VirtualMachine, kvvm *virtv if kvvmi != nil { msg = fmt.Sprintf("%s, %s", msg, kvvmi.Status.Phase) } - synchronized := service.GetKVVMCondition(string(virtv1.VirtualMachineInstanceSynchronized), kvvm.Status.Conditions) + synchronized := conditions.GetKVVMCondition(string(virtv1.VirtualMachineInstanceSynchronized), kvvm.Status.Conditions) if synchronized != nil && synchronized.Status == corev1.ConditionFalse && synchronized.Message != "" { msg = fmt.Sprintf("%s; %s: %s", msg, synchronized.Reason, synchronized.Message) } @@ -240,7 +227,7 @@ func (h *LifeCycleHandler) syncRunning(vm *virtv2.VirtualMachine, kvvm *virtv1.V msg = fmt.Sprintf("%s, %s", msg, kvvmi.Status.Phase) } - synchronized := service.GetKVVMCondition(string(virtv1.VirtualMachineInstanceSynchronized), kvvm.Status.Conditions) + synchronized := conditions.GetKVVMCondition(string(virtv1.VirtualMachineInstanceSynchronized), kvvm.Status.Conditions) if synchronized != nil && synchronized.Status == corev1.ConditionFalse && synchronized.Message != "" { msg = fmt.Sprintf("%s; %s: %s", msg, synchronized.Reason, synchronized.Message) } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/provisioning.go b/images/virtualization-artifact/pkg/controller/vm/internal/provisioning.go index 045aa50ad..4367da402 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/provisioning.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/provisioning.go @@ -29,7 +29,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" "github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" @@ -54,10 +53,6 @@ func (h *ProvisioningHandler) Handle(ctx context.Context, s state.VirtualMachine current := s.VirtualMachine().Current() changed := s.VirtualMachine().Changed() - if update := addAllUnknown(changed, vmcondition.TypeProvisioningReady); update { - return reconcile.Result{Requeue: true}, nil - } - if isDeletion(current) { return reconcile.Result{}, nil } @@ -128,7 +123,7 @@ func (h *ProvisioningHandler) genConditionFromSecret(ctx context.Context, builde case errors.As(err, new(secretNotFoundError)): builder.Status(metav1.ConditionFalse). Reason(vmcondition.ReasonProvisioningNotReady). - Message(service.CapitalizeFirstLetter(err.Error())) + Message(conditions.CapitalizeFirstLetter(err.Error())) return nil case errors.Is(err, errSecretIsNotValid): @@ -140,7 +135,7 @@ func (h *ProvisioningHandler) genConditionFromSecret(ctx context.Context, builde case errors.As(err, new(unexpectedSecretTypeError)): builder.Status(metav1.ConditionFalse). Reason(vmcondition.ReasonProvisioningNotReady). - Message(service.CapitalizeFirstLetter(err.Error())) + Message(conditions.CapitalizeFirstLetter(err.Error())) return nil default: diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/size_policy.go b/images/virtualization-artifact/pkg/controller/vm/internal/size_policy.go index 7a70eea9f..b4fffdbb6 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/size_policy.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/size_policy.go @@ -54,8 +54,6 @@ func (h *SizePolicyHandler) Handle(ctx context.Context, s state.VirtualMachineSt defer func() { conditions.SetCondition(cb, &changed.Status.Conditions) }() - addAllUnknown(changed, vmcondition.TypeSizingPolicyMatched) - if isDeletion(current) { return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/snapshotting.go b/images/virtualization-artifact/pkg/controller/vm/internal/snapshotting.go index ffe8de797..a28297424 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/snapshotting.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/snapshotting.go @@ -46,10 +46,6 @@ func (h *SnapshottingHandler) Handle(ctx context.Context, s state.VirtualMachine vm := s.VirtualMachine().Changed() - if update := addAllUnknown(vm, vmcondition.TypeSnapshotting); update { - return reconcile.Result{Requeue: true}, nil - } - if isDeletion(vm) { return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go b/images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go index f696ae02d..eda325b05 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go @@ -48,11 +48,6 @@ import ( const nameSyncKvvmHandler = "SyncKvvmHandler" -var syncKVVMConditions = []vmcondition.Type{ - vmcondition.TypeConfigurationApplied, - vmcondition.TypeAwaitingRestartToApplyConfiguration, -} - func NewSyncKvvmHandler(dvcrSettings *dvcr.Settings, client client.Client, recorder record.EventRecorder) *SyncKvvmHandler { return &SyncKvvmHandler{ dvcrSettings: dvcrSettings, @@ -77,10 +72,6 @@ func (h *SyncKvvmHandler) Handle(ctx context.Context, s state.VirtualMachineStat current := s.VirtualMachine().Current() changed := s.VirtualMachine().Changed() - if update := addAllUnknown(changed, syncKVVMConditions...); update { - return reconcile.Result{Requeue: true}, nil - } - if isDeletion(current) { return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vm/internal/util.go b/images/virtualization-artifact/pkg/controller/vm/internal/util.go index b614c8397..dc3ea0bd7 100644 --- a/images/virtualization-artifact/pkg/controller/vm/internal/util.go +++ b/images/virtualization-artifact/pkg/controller/vm/internal/util.go @@ -26,10 +26,8 @@ import ( virtv1 "kubevirt.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" - "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" ) func isDeletion(vm *virtv2.VirtualMachine) bool { @@ -38,21 +36,6 @@ func isDeletion(vm *virtv2.VirtualMachine) bool { type updaterProtection func(p *service.ProtectionService) func(ctx context.Context, objs ...client.Object) error -func addAllUnknown(vm *virtv2.VirtualMachine, conds ...vmcondition.Type) (update bool) { - for _, cond := range conds { - if conditions.HasCondition(cond, vm.Status.Conditions) { - continue - } - cb := conditions.NewConditionBuilder(cond). - Generation(vm.GetGeneration()). - Reason(vmcondition.ReasonUnknown). - Status(metav1.ConditionUnknown) - conditions.SetCondition(cb, &vm.Status.Conditions) - update = true - } - return -} - func conditionStatus(status string) metav1.ConditionStatus { status = strings.ToLower(status) switch { diff --git a/images/virtualization-artifact/pkg/controller/vmbda/internal/block_device_ready.go b/images/virtualization-artifact/pkg/controller/vmbda/internal/block_device_ready.go index 8b1f2b662..2797e6040 100644 --- a/images/virtualization-artifact/pkg/controller/vmbda/internal/block_device_ready.go +++ b/images/virtualization-artifact/pkg/controller/vmbda/internal/block_device_ready.go @@ -92,7 +92,7 @@ func (h BlockDeviceReadyHandler) Handle(ctx context.Context, vmbda *virtv2.Virtu } if vd.Status.Phase == virtv2.DiskReady { - diskReadyCondition, _ := service.GetCondition(vdcondition.ReadyType, vd.Status.Conditions) + diskReadyCondition, _ := conditions.GetConditionByType(vdcondition.ReadyType, vd.Status.Conditions) if diskReadyCondition.Status != metav1.ConditionTrue { cb. Status(metav1.ConditionFalse). diff --git a/images/virtualization-artifact/pkg/controller/vmbda/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/vmbda/internal/life_cycle.go index b8e41d866..5ed0fe842 100644 --- a/images/virtualization-artifact/pkg/controller/vmbda/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vmbda/internal/life_cycle.go @@ -189,7 +189,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmbda *virtv2.VirtualMachi cb. Status(metav1.ConditionFalse). Reason(vmbdacondition.AttachmentRequestSent). - Message(service.CapitalizeFirstLetter(err.Error() + ".")) + Message(conditions.CapitalizeFirstLetter(err.Error() + ".")) return reconcile.Result{}, nil } @@ -230,7 +230,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmbda *virtv2.VirtualMachi cb. Status(metav1.ConditionFalse). Reason(vmbdacondition.Conflict). - Message(service.CapitalizeFirstLetter(err.Error())) + Message(conditions.CapitalizeFirstLetter(err.Error())) return reconcile.Result{}, nil case errors.Is(err, service.ErrHotPlugRequestAlreadySent): log.Info("Attachment request sent: attachment is in progress.") @@ -248,7 +248,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmbda *virtv2.VirtualMachi cb. Status(metav1.ConditionFalse). Reason(vmbdacondition.NotAttached). - Message(service.CapitalizeFirstLetter(err.Error())) + Message(conditions.CapitalizeFirstLetter(err.Error())) return reconcile.Result{}, nil default: return reconcile.Result{}, err diff --git a/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vd_watcher.go b/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vd_watcher.go index c8f652d14..117def9ca 100644 --- a/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vd_watcher.go +++ b/images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/vd_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/vdcondition" ) @@ -101,8 +101,8 @@ func (w VirtualDiskWatcher) filterUpdateEvents(e event.UpdateEvent) bool { return true } - oldReadyCondition, _ := service.GetCondition(vdcondition.ReadyType, oldVD.Status.Conditions) - newReadyCondition, _ := service.GetCondition(vdcondition.ReadyType, newVD.Status.Conditions) + oldReadyCondition, _ := conditions.GetConditionByType(vdcondition.ReadyType, oldVD.Status.Conditions) + newReadyCondition, _ := conditions.GetConditionByType(vdcondition.ReadyType, newVD.Status.Conditions) return oldReadyCondition.Status != newReadyCondition.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..ef522c46a 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.GetConditionByType(vmcondition.TypeRunning.String(), oldVM.Status.Conditions) + newRunningCondition, _ := conditions.GetConditionByType(vmcondition.TypeRunning.String(), newVM.Status.Conditions) return newRunningCondition.Status != oldRunningCondition.Status } diff --git a/images/virtualization-artifact/pkg/controller/vmclass/internal/discovery.go b/images/virtualization-artifact/pkg/controller/vmclass/internal/discovery.go index 4620293cc..6b5599bdb 100644 --- a/images/virtualization-artifact/pkg/controller/vmclass/internal/discovery.go +++ b/images/virtualization-artifact/pkg/controller/vmclass/internal/discovery.go @@ -49,10 +49,6 @@ func (h *DiscoveryHandler) Handle(ctx context.Context, s state.VirtualMachineCla current := s.VirtualMachineClass().Current() changed := s.VirtualMachineClass().Changed() - if updated := addAllUnknown(changed, vmclasscondition.TypeDiscovered); updated { - return reconcile.Result{Requeue: true}, nil - } - if isDeletion(current) { return reconcile.Result{}, nil } diff --git a/images/virtualization-artifact/pkg/controller/vmclass/internal/lifecycle.go b/images/virtualization-artifact/pkg/controller/vmclass/internal/lifecycle.go index 96b9d60e8..3df2b60a5 100644 --- a/images/virtualization-artifact/pkg/controller/vmclass/internal/lifecycle.go +++ b/images/virtualization-artifact/pkg/controller/vmclass/internal/lifecycle.go @@ -52,11 +52,6 @@ func (h *LifeCycleHandler) Handle(_ context.Context, s state.VirtualMachineClass return reconcile.Result{}, nil } - if updated := addAllUnknown(changed, vmclasscondition.TypeReady); updated { - changed.Status.Phase = virtv2.ClassPhasePending - return reconcile.Result{Requeue: true}, nil - } - //nolint:staticcheck mgr := conditions.NewManager(changed.Status.Conditions) cb := conditions.NewConditionBuilder(vmclasscondition.TypeReady). diff --git a/images/virtualization-artifact/pkg/controller/vmclass/internal/util.go b/images/virtualization-artifact/pkg/controller/vmclass/internal/util.go index d3751eaeb..ef54cba46 100644 --- a/images/virtualization-artifact/pkg/controller/vmclass/internal/util.go +++ b/images/virtualization-artifact/pkg/controller/vmclass/internal/util.go @@ -17,29 +17,9 @@ limitations under the License. package internal import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" - "github.com/deckhouse/virtualization/api/core/v1alpha2/vmclasscondition" ) func isDeletion(class *virtv2.VirtualMachineClass) bool { return class == nil || !class.GetDeletionTimestamp().IsZero() } - -func addAllUnknown(class *virtv2.VirtualMachineClass, conds ...vmclasscondition.Type) (update bool) { - //nolint:staticcheck - mgr := conditions.NewManager(class.Status.Conditions) - for _, c := range conds { - if add := mgr.Add(conditions.NewConditionBuilder(c). - Generation(class.GetGeneration()). - Reason(vmclasscondition.ReasonUnknown). - Status(metav1.ConditionUnknown). - Condition()); add { - update = true - } - } - class.Status.Conditions = mgr.Generate() - return -} 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 1ecdb1585..4b15a5318 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.GetConditionByType(vmipcondition.BoundType.String(), 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..312ab8871 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.GetConditionByType(vmiplcondition.BoundType.String(), 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 218ed2364..c64b6be1a 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/client" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + "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.GetConditionByType(vmipcondition.BoundType.String(), 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..2789d3542 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.GetConditionByType(vmipcondition.BoundType.String(), 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..9e34e5d99 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.GetConditionByType(vmopcondition.TypeCompleted.String(), 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/vmrestore/internal/life_cycle.go b/images/virtualization-artifact/pkg/controller/vmrestore/internal/life_cycle.go index daddd6efa..d6a89a853 100644 --- a/images/virtualization-artifact/pkg/controller/vmrestore/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vmrestore/internal/life_cycle.go @@ -29,7 +29,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" "github.com/deckhouse/virtualization-controller/pkg/controller/vmrestore/internal/restorer" "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" @@ -138,7 +137,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmRestore *virtv2.VirtualM cb. Status(metav1.ConditionFalse). Reason(vmrestorecondition.VirtualMachineSnapshotNotReady). - Message(service.CapitalizeFirstLetter(err.Error()) + ".") + Message(conditions.CapitalizeFirstLetter(err.Error()) + ".") return reconcile.Result{}, nil default: setPhaseConditionToFailed(cb, &vmRestore.Status.Phase, err) @@ -182,7 +181,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmRestore *virtv2.VirtualM cb. Status(metav1.ConditionFalse). Reason(vmrestorecondition.VirtualMachineRestoreConflict). - Message(service.CapitalizeFirstLetter(err.Error()) + ".") + Message(conditions.CapitalizeFirstLetter(err.Error()) + ".") return reconcile.Result{}, nil default: setPhaseConditionToFailed(cb, &vmRestore.Status.Phase, err) @@ -270,5 +269,5 @@ func setPhaseConditionToFailed(cb *conditions.ConditionBuilder, phase *virtv2.Vi cb. Status(metav1.ConditionFalse). Reason(vmrestorecondition.VirtualMachineRestoreFailed). - Message(service.CapitalizeFirstLetter(err.Error()) + ".") + Message(conditions.CapitalizeFirstLetter(err.Error()) + ".") } 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 bbcacbd11..034b4da87 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/life_cycle.go @@ -134,7 +134,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmSnapshot *virtv2.Virtual cb. Status(metav1.ConditionFalse). Reason(vmscondition.BlockDevicesNotReady). - Message(service.CapitalizeFirstLetter(err.Error() + ".")) + Message(conditions.CapitalizeFirstLetter(err.Error() + ".")) return reconcile.Result{}, nil default: setPhaseConditionToFailed(cb, &vmSnapshot.Status.Phase, err) @@ -201,7 +201,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmSnapshot *virtv2.Virtual case err == nil: case errors.Is(err, ErrVolumeSnapshotClassNotFound), errors.Is(err, ErrCannotTakeSnapshot): vmSnapshot.Status.Phase = virtv2.VirtualMachineSnapshotPhaseFailed - msg := service.CapitalizeFirstLetter(err.Error()) + msg := conditions.CapitalizeFirstLetter(err.Error()) if !strings.HasSuffix(msg, ".") { msg += "." } @@ -261,7 +261,7 @@ func setPhaseConditionToFailed(cb *conditions.ConditionBuilder, phase *virtv2.Vi cb. Status(metav1.ConditionFalse). Reason(vmscondition.VirtualMachineSnapshotFailed). - Message(service.CapitalizeFirstLetter(err.Error()) + ".") + Message(conditions.CapitalizeFirstLetter(err.Error()) + ".") } func (h LifeCycleHandler) fillStatusVirtualDiskSnapshotNames(vmSnapshot *virtv2.VirtualMachineSnapshot, vm *virtv2.VirtualMachine) { 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 2107ae9f0..7db8b9dc9 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,7 +144,7 @@ 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{ + conditions.ApplyCondition(metav1.Condition{ Type: vmcondition.TypeBlockDevicesReady.String(), Status: metav1.ConditionFalse, }, &vm.Status.Conditions) @@ -180,7 +179,7 @@ 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{ + conditions.ApplyCondition(metav1.Condition{ Type: vdcondition.Ready, Status: metav1.ConditionFalse, }, &vd.Status.Conditions) @@ -199,7 +198,7 @@ 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{ + conditions.ApplyCondition(metav1.Condition{ Type: vdcondition.Resized, Status: metav1.ConditionFalse, Reason: vdcondition.InProgress, 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 c233f626b..0b406d8ba 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.GetConditionByType(vmcondition.TypeSnapshotting.String(), 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/vd_watcher.go b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/watcher/vd_watcher.go index 5796440a6..20ff00016 100644 --- a/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/watcher/vd_watcher.go +++ b/images/virtualization-artifact/pkg/controller/vmsnapshot/internal/watcher/vd_watcher.go @@ -32,7 +32,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/vdcondition" ) @@ -105,8 +105,8 @@ func (w VirtualDiskWatcher) filterUpdateEvents(e event.UpdateEvent) bool { return false } - oldResized, _ := service.GetCondition(vdcondition.ResizedType, oldVD.Status.Conditions) - newResized, _ := service.GetCondition(vdcondition.ResizedType, newVD.Status.Conditions) + oldResized, _ := conditions.GetConditionByType(vdcondition.ResizedType, oldVD.Status.Conditions) + newResized, _ := conditions.GetConditionByType(vdcondition.ResizedType, newVD.Status.Conditions) return oldResized.Status != newResized.Status || oldResized.Reason != newResized.Reason } 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..84a373c4c 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.GetConditionByType(vmcondition.TypeFilesystemReady.String(), oldVM.Status.Conditions) + newFSReady, _ := conditions.GetConditionByType(vmcondition.TypeFilesystemReady.String(), 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.GetConditionByType(vmcondition.TypeSnapshotting.String(), oldVM.Status.Conditions) + newSnapshotting, _ := conditions.GetConditionByType(vmcondition.TypeSnapshotting.String(), newVM.Status.Conditions) return oldSnapshotting.Status != newSnapshotting.Status } diff --git a/images/virtualization-artifact/pkg/controller/watchers/cvi_enqueuer.go b/images/virtualization-artifact/pkg/controller/watchers/cvi_enqueuer.go index 9e308dbc0..3e671a7b3 100644 --- a/images/virtualization-artifact/pkg/controller/watchers/cvi_enqueuer.go +++ b/images/virtualization-artifact/pkg/controller/watchers/cvi_enqueuer.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "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/cvicondition" ) @@ -60,7 +60,7 @@ func (w ClusterVirtualImageRequestEnqueuer) EnqueueRequests(ctx context.Context, } for _, cvi := range cvis.Items { - dsReady, _ := service.GetCondition(cvicondition.DatasourceReadyType, cvi.Status.Conditions) + dsReady, _ := conditions.GetConditionByType(cvicondition.DatasourceReadyType, cvi.Status.Conditions) if dsReady.Status == metav1.ConditionTrue { continue } diff --git a/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go b/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go index 95bf14482..9ad308265 100644 --- a/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go +++ b/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "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" ) @@ -60,7 +60,7 @@ func (w VirtualDiskRequestEnqueuer) EnqueueRequests(ctx context.Context, obj cli } for _, vd := range vds.Items { - dsReady, _ := service.GetCondition(vdcondition.DatasourceReadyType, vd.Status.Conditions) + dsReady, _ := conditions.GetConditionByType(vdcondition.DatasourceReadyType, vd.Status.Conditions) if dsReady.Status == metav1.ConditionTrue { continue } diff --git a/images/virtualization-artifact/pkg/controller/watchers/vi_enqueuer.go b/images/virtualization-artifact/pkg/controller/watchers/vi_enqueuer.go index 0046ae6ca..1d70f02c9 100644 --- a/images/virtualization-artifact/pkg/controller/watchers/vi_enqueuer.go +++ b/images/virtualization-artifact/pkg/controller/watchers/vi_enqueuer.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "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/vicondition" ) @@ -60,7 +60,7 @@ func (w VirtualImageRequestEnqueuer) EnqueueRequests(ctx context.Context, obj cl } for _, vi := range vis.Items { - dsReady, _ := service.GetCondition(vicondition.DatasourceReadyType, vi.Status.Conditions) + dsReady, _ := conditions.GetConditionByType(vicondition.DatasourceReadyType, vi.Status.Conditions) if dsReady.Status == metav1.ConditionTrue { continue } 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..93a57af1e 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.GetConditionByType(vmcondition.TypeAwaitingRestartToApplyConfiguration.String(), vm.Status.Conditions); found && cond.Status == metav1.ConditionTrue { awaitingRestartToApplyConfiguration = true } - if cond, found := service.GetCondition(vmcondition.TypeConfigurationApplied.String(), + if cond, found := conditions.GetConditionByType(vmcondition.TypeConfigurationApplied.String(), vm.Status.Conditions); found && cond.Status == metav1.ConditionTrue { configurationApplied = true }