Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Khorunzhin <[email protected]>
  • Loading branch information
Valeriy Khorunzhin committed Dec 27, 2024
1 parent 7b6711f commit ef2c3d0
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/core/v1alpha2/vicondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const (
StorageClassNotReady ReadyReason = "StorageClassNotReady"
// Ready indicates that the import process is complete and the `VirtualImage` is ready for use.
Ready ReadyReason = "Ready"
// QuotaExceeded indicates that the VirtualImage is reached project quotas and can not be provisioned.
QuotaExceeded ReadyReason = "QuotaExceeded"

// Lost indicates that the underlying PersistentVolumeClaim has been lost and the `VirtualImage` can no longer be used.
Lost ReadyReason = "PVCLost"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ func (ds HTTPDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualImage
return reconcile.Result{}, err
}

quotaExceeded, quotaExceededMessage := checkIfQuotaExceeded(dv)

switch {
case isDiskProvisioningFinished(condition):
log.Info("Image provisioning finished: clean up")
Expand Down Expand Up @@ -342,6 +344,12 @@ func (ds HTTPDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualImage
Message("DVCR Provisioner not found: create the new one.")

return reconcile.Result{Requeue: true}, nil
case quotaExceeded:
cb.
Status(metav1.ConditionFalse).
Reason(vicondition.QuotaExceeded).
Message(quotaExceededMessage)
return reconcile.Result{}, nil
case pvc == nil:
vi.Status.Phase = virtv2.ImageProvisioning
cb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func (ds ObjectRefDataSource) StoreToPVC(ctx context.Context, vi *virtv2.Virtual
return reconcile.Result{}, err
}

quotaExceeded, quotaExceededMessage := checkIfQuotaExceeded(dv)

switch {
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")
Expand Down Expand Up @@ -211,6 +213,12 @@ func (ds ObjectRefDataSource) StoreToPVC(ctx context.Context, vi *virtv2.Virtual
Message("PVC Provisioner not found: create the new one.")

return reconcile.Result{Requeue: true}, nil
case quotaExceeded:
cb.
Status(metav1.ConditionFalse).
Reason(vicondition.QuotaExceeded).
Message(quotaExceededMessage)
return reconcile.Result{}, nil
case pvc == nil:
vi.Status.Phase = virtv2.ImageProvisioning
cb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ func (ds ObjectRefVirtualDisk) StoreToPVC(ctx context.Context, vi *virtv2.Virtua
return reconcile.Result{}, err
}

quotaExceeded, quotaExceededMessage := checkIfQuotaExceeded(dv)

switch {
case isDiskProvisioningFinished(cb.Condition()):
log.Info("Disk provisioning finished: clean up")
Expand Down Expand Up @@ -282,6 +284,12 @@ func (ds ObjectRefVirtualDisk) StoreToPVC(ctx context.Context, vi *virtv2.Virtua
Message("PVC Provisioner not found: create the new one.")

return reconcile.Result{Requeue: true}, nil
case quotaExceeded:
cb.
Status(metav1.ConditionFalse).
Reason(vicondition.QuotaExceeded).
Message(quotaExceededMessage)
return reconcile.Result{}, nil
case pvc == nil:
vi.Status.Phase = virtv2.ImageProvisioning
cb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func (ds ObjectRefDataVirtualImageOnPVC) StoreToPVC(ctx context.Context, vi, viR
return reconcile.Result{}, err
}

quotaExceeded, quotaExceededMessage := checkIfQuotaExceeded(dv)

switch {
case isDiskProvisioningFinished(cb.Condition()):
log.Info("Disk provisioning finished: clean up")
Expand Down Expand Up @@ -271,6 +273,12 @@ func (ds ObjectRefDataVirtualImageOnPVC) StoreToPVC(ctx context.Context, vi, viR
Message("PVC Provisioner not found: create the new one.")

return reconcile.Result{Requeue: true}, nil
case quotaExceeded:
cb.
Status(metav1.ConditionFalse).
Reason(vicondition.QuotaExceeded).
Message(quotaExceededMessage)
return reconcile.Result{}, nil
case pvc == nil:
vi.Status.Phase = virtv2.ImageProvisioning
cb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (ds RegistryDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualI
return reconcile.Result{}, err
}

quotaExceeded, quotaExceededMessage := checkIfQuotaExceeded(dv)

switch {
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")
Expand Down Expand Up @@ -223,6 +225,12 @@ func (ds RegistryDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualI
Message("PVC Provisioner not found: create the new one.")

return reconcile.Result{Requeue: true}, nil
case quotaExceeded:
cb.
Status(metav1.ConditionFalse).
Reason(vicondition.QuotaExceeded).
Message(quotaExceededMessage)
return reconcile.Result{}, nil
case pvc == nil:
vi.Status.Phase = virtv2.ImageProvisioning
cb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,55 @@ func setQuotaExceededPhaseCondition(cb *conditions.ConditionBuilder, phase *virt
cb.Message(fmt.Sprintf("Quota exceeded: %s; Retry in %d minute.", err, retryPeriod))
return reconcile.Result{RequeueAfter: retryPeriod * time.Minute}
}

const (
DVBoundConditionType string = "Bound"
DVRunningConditionType string = "Running"
DVReadyConditionType string = "Ready"
DVErrExceededQuotaReason string = "ErrExceededQuota"
)

func checkIfQuotaExceeded(dv *cdiv1.DataVolume) (bool, string) {
if dv == nil {
return false, ""
}

boundCondition, ok := getDVCondition(dv, DVBoundConditionType)
if !ok {
return false, ""
}

readyDVCondition, ok := getDVCondition(dv, DVReadyConditionType)
if !ok {
return false, ""
}

runningCondition, ok := getDVCondition(dv, DVRunningConditionType)
if !ok {
return false, ""
}

if boundCondition.Reason == DVErrExceededQuotaReason {
return true, service.CapitalizeFirstLetter(boundCondition.Message)
}

if readyDVCondition.Reason == DVErrExceededQuotaReason {
return true, service.CapitalizeFirstLetter(readyDVCondition.Message)
}

if runningCondition.Reason == DVErrExceededQuotaReason {
return true, service.CapitalizeFirstLetter(runningCondition.Message)
}

return false, ""
}

func getDVCondition(dv *cdiv1.DataVolume, conditionType string) (*cdiv1.DataVolumeCondition, bool) {
for _, cond := range dv.Status.Conditions {
if string(cond.Type) == conditionType {
return &cond, true
}
}

return nil, false
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func (ds UploadDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualIma
return reconcile.Result{}, err
}

quotaExceeded, quotaExceededMessage := checkIfQuotaExceeded(dv)

switch {
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")
Expand Down Expand Up @@ -265,6 +267,12 @@ func (ds UploadDataSource) StoreToPVC(ctx context.Context, vi *virtv2.VirtualIma
Message("PVC Provisioner not found: create the new one.")

return reconcile.Result{Requeue: true}, nil
case quotaExceeded:
cb.
Status(metav1.ConditionFalse).
Reason(vicondition.QuotaExceeded).
Message(quotaExceededMessage)
return reconcile.Result{}, nil
case pvc == nil:
vi.Status.Phase = virtv2.ImageProvisioning
cb.
Expand Down

0 comments on commit ef2c3d0

Please sign in to comment.