Skip to content

Commit

Permalink
fix cdi patch
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Khorunzhin <[email protected]>
  • Loading branch information
Valeriy Khorunzhin committed Jan 14, 2025
1 parent 5013881 commit 89d0c67
Showing 1 changed file with 72 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/pkg/controller/datavolume/controller-base.go b/pkg/controller/datavolume/controller-base.go
index acd09cb94..b856d5e38 100644
index acd09cb94..2fb859150 100644
--- a/pkg/controller/datavolume/controller-base.go
+++ b/pkg/controller/datavolume/controller-base.go
@@ -55,6 +55,7 @@ import (
Expand All @@ -10,33 +10,16 @@ index acd09cb94..b856d5e38 100644
"kubevirt.io/containerized-data-importer/pkg/token"
"kubevirt.io/containerized-data-importer/pkg/util"
)
@@ -1035,6 +1036,24 @@ func (r *ReconcilerBase) updateConditions(dataVolume *cdiv1.DataVolume, pvc *cor
@@ -1035,6 +1036,7 @@ func (r *ReconcilerBase) updateConditions(dataVolume *cdiv1.DataVolume, pvc *cor
dataVolume.Status.Conditions = updateBoundCondition(dataVolume.Status.Conditions, pvc, message, reason)
dataVolume.Status.Conditions = UpdateReadyCondition(dataVolume.Status.Conditions, readyStatus, message, reason)
dataVolume.Status.Conditions = updateRunningCondition(dataVolume.Status.Conditions, anno)
+ patchedDV.CreateDVQuotaIsNotExceededConditionIfNotExists(&dataVolume.Status.Conditions)
+
+ readyCondition := FindConditionByType(cdiv1.DataVolumeReady, dataVolume.Status.Conditions)
+ boundCondition := FindConditionByType(cdiv1.DataVolumeBound, dataVolume.Status.Conditions)
+ runningCondition := FindConditionByType(cdiv1.DataVolumeRunning, dataVolume.Status.Conditions)
+
+ switch {
+ case readyCondition != nil && readyCondition.Reason == cc.ErrExceededQuota:
+ dataVolume.Status.Conditions = patchedDV.UpdateDVQuotaNotExceededCondition(dataVolume.Status.Conditions, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", readyCondition.Message), patchedDV.QuotaExceededReason)
+ case boundCondition != nil && boundCondition.Reason == cc.ErrExceededQuota:
+ dataVolume.Status.Conditions = patchedDV.UpdateDVQuotaNotExceededCondition(dataVolume.Status.Conditions, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", readyCondition.Message), patchedDV.QuotaExceededReason)
+ case runningCondition != nil:
+ if runningCondition.Reason == cc.ErrExceededQuota {
+ dataVolume.Status.Conditions = patchedDV.UpdateDVQuotaNotExceededCondition(dataVolume.Status.Conditions, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", readyCondition.Message), patchedDV.QuotaExceededReason)
+ } else if runningCondition.Status == corev1.ConditionTrue {
+ dataVolume.Status.Conditions = patchedDV.UpdateDVQuotaNotExceededCondition(dataVolume.Status.Conditions, corev1.ConditionTrue, "", patchedDV.QuotaNotExceededReason)
+ }
+ }
+ dataVolume.Status.Conditions = patchedDV.UpdateDVQuotaNotExceededCondition(dataVolume.Status.Conditions)
}

func (r *ReconcilerBase) emitConditionEvent(dataVolume *cdiv1.DataVolume, originalCond []cdiv1.DataVolumeCondition) {
diff --git a/pkg/controller/import-controller.go b/pkg/controller/import-controller.go
index 49f1ff898..4ce5c085e 100644
index 49f1ff898..972f8ab5f 100644
--- a/pkg/controller/import-controller.go
+++ b/pkg/controller/import-controller.go
@@ -34,6 +34,7 @@ import (
Expand All @@ -47,18 +30,21 @@ index 49f1ff898..4ce5c085e 100644
"kubevirt.io/containerized-data-importer/pkg/util"
"kubevirt.io/containerized-data-importer/pkg/util/naming"
sdkapi "kubevirt.io/controller-lifecycle-operator-sdk/api"
@@ -753,6 +754,9 @@ func (r *ImportReconciler) createScratchPvcForPod(pvc *corev1.PersistentVolumeCl
@@ -753,6 +754,12 @@ func (r *ImportReconciler) createScratchPvcForPod(pvc *corev1.PersistentVolumeCl
// Scratch PVC doesn't exist yet, create it. Determine which storage class to use.
_, err = createScratchPersistentVolumeClaim(r.client, pvc, pod, scratchPVCName, storageClassName, r.installerLabels, r.recorder)
if err != nil {
+ if strings.Contains(err.Error(), "exceeded quota") {
+ patchedDV.UpdateDVQuotaNotExceededConditionByPVC(r.client, pvc, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", err.Error()), patchedDV.QuotaExceededReason)
+ innerErr := patchedDV.UpdateDVQuotaNotExceededConditionByPVC(r.client, pvc, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", err.Error()), patchedDV.QuotaExceededReason)
+ if innerErr != nil {
+ return innerErr
+ }
+ }
return err
}
anno[cc.AnnBoundCondition] = "false"
diff --git a/pkg/controller/populators/populator-base.go b/pkg/controller/populators/populator-base.go
index 6c6fd8f8a..9df58c3fd 100644
index 6c6fd8f8a..8fcda592c 100644
--- a/pkg/controller/populators/populator-base.go
+++ b/pkg/controller/populators/populator-base.go
@@ -18,7 +18,9 @@ package populators
Expand Down Expand Up @@ -91,18 +77,21 @@ index 6c6fd8f8a..9df58c3fd 100644
// Assemble PVC' spec
pvcPrime := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
@@ -213,6 +221,9 @@ func (r *ReconcilerBase) createPVCPrime(pvc *corev1.PersistentVolumeClaim, sourc
@@ -213,6 +221,12 @@ func (r *ReconcilerBase) createPVCPrime(pvc *corev1.PersistentVolumeClaim, sourc
}

if err := r.client.Create(context.TODO(), pvcPrime); err != nil {
+ if strings.Contains(err.Error(), "exceeded quota") {
+ patchedDV.UpdateDVQuotaNotExceededConditionByPVC(r.client, pvc, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", err.Error()), patchedDV.QuotaExceededReason)
+ innerErr := patchedDV.UpdateDVQuotaNotExceededConditionByPVC(r.client, pvc, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", err.Error()), patchedDV.QuotaExceededReason)
+ if innerErr != nil {
+ return nil, innerErr
+ }
+ }
return nil, err
}
r.recorder.Eventf(pvc, corev1.EventTypeNormal, createdPVCPrimeSuccessfully, messageCreatedPVCPrimeSuccessfully)
diff --git a/pkg/controller/upload-controller.go b/pkg/controller/upload-controller.go
index 4c153257a..aaf4f8198 100644
index 4c153257a..e21e40312 100644
--- a/pkg/controller/upload-controller.go
+++ b/pkg/controller/upload-controller.go
@@ -51,6 +51,7 @@ import (
Expand All @@ -113,26 +102,30 @@ index 4c153257a..aaf4f8198 100644
"kubevirt.io/containerized-data-importer/pkg/util"
"kubevirt.io/containerized-data-importer/pkg/util/cert/fetcher"
"kubevirt.io/containerized-data-importer/pkg/util/cert/generator"
@@ -473,6 +474,9 @@ func (r *UploadReconciler) getOrCreateScratchPvc(pvc *corev1.PersistentVolumeCla
@@ -473,6 +474,12 @@ func (r *UploadReconciler) getOrCreateScratchPvc(pvc *corev1.PersistentVolumeCla
// Scratch PVC doesn't exist yet, create it.
scratchPvc, err = createScratchPersistentVolumeClaim(r.client, pvc, pod, name, storageClassName, map[string]string{}, r.recorder)
if err != nil {
+ if strings.Contains(err.Error(), "exceeded quota") {
+ patchedDV.UpdateDVQuotaNotExceededConditionByPVC(r.client, pvc, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", err.Error()), patchedDV.QuotaExceededReason)
+ innerErr := patchedDV.UpdateDVQuotaNotExceededConditionByPVC(r.client, pvc, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", err.Error()), patchedDV.QuotaExceededReason)
+ if innerErr != nil {
+ return nil, innerErr
+ }
+ }
return nil, err
}
} else {
diff --git a/pkg/patcheddatavolume/patched_datavolume.go b/pkg/patcheddatavolume/patched_datavolume.go
new file mode 100644
index 000000000..7b00253e5
index 000000000..28ce3c406
--- /dev/null
+++ b/pkg/patcheddatavolume/patched_datavolume.go
@@ -0,0 +1,101 @@
@@ -0,0 +1,120 @@
+package patcheddatavolume
+
+import (
+ "context"
+ "fmt"
+
+ corev1 "k8s.io/api/core/v1"
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -157,6 +150,54 @@ index 000000000..7b00253e5
+ return nil
+}
+
+func UpdateDVQuotaNotExceededCondition(conditions []cdiv1.DataVolumeCondition) []cdiv1.DataVolumeCondition {
+ CreateDVQuotaIsNotExceededConditionIfNotExists(&conditions)
+ readyCondition := FindConditionByType(cdiv1.DataVolumeReady, conditions)
+ boundCondition := FindConditionByType(cdiv1.DataVolumeBound, conditions)
+ runningCondition := FindConditionByType(cdiv1.DataVolumeRunning, conditions)
+
+ switch {
+ case readyCondition != nil && readyCondition.Reason == common.ErrExceededQuota:
+ conditions = updateCondition(conditions, QoutaNotExceededConditionType, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", readyCondition.Message), QuotaExceededReason)
+ case boundCondition != nil && boundCondition.Reason == common.ErrExceededQuota:
+ conditions = updateCondition(conditions, QoutaNotExceededConditionType, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", readyCondition.Message), QuotaExceededReason)
+ case runningCondition != nil:
+ if runningCondition.Reason == common.ErrExceededQuota {
+ conditions = updateCondition(conditions, QoutaNotExceededConditionType, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", readyCondition.Message), QuotaExceededReason)
+ } else if runningCondition.Status == corev1.ConditionTrue {
+ conditions = updateCondition(conditions, QoutaNotExceededConditionType, corev1.ConditionTrue, "", QuotaNotExceededReason)
+ }
+ }
+
+ return conditions
+}
+
+func UpdateDVQuotaNotExceededConditionByPVC(clientObject client.Client, pvc *corev1.PersistentVolumeClaim, status corev1.ConditionStatus, message, reason string) error {
+ dv := getDVByPVC(clientObject, pvc, common.AnnCreatedForDataVolume)
+ if dv == nil {
+ return nil
+ }
+
+ dv.Status.Conditions = updateCondition(dv.Status.Conditions, QoutaNotExceededConditionType, status, message, reason)
+ return clientObject.Status().Update(context.TODO(), dv)
+}
+
+func CreateDVQuotaIsNotExceededConditionIfNotExists(conditions *[]cdiv1.DataVolumeCondition) {
+ if conditions == nil {
+ return
+ }
+
+ condition := FindConditionByType(QoutaNotExceededConditionType, *conditions)
+ if condition == nil {
+ *conditions = append(*conditions, cdiv1.DataVolumeCondition{
+ Type: QoutaNotExceededConditionType,
+ Status: corev1.ConditionTrue,
+ Reason: QuotaNotExceededReason,
+ Message: "",
+ })
+ }
+}
+
+func updateCondition(conditions []cdiv1.DataVolumeCondition, conditionType cdiv1.DataVolumeConditionType, status corev1.ConditionStatus, message, reason string) []cdiv1.DataVolumeCondition {
+ condition := FindConditionByType(conditionType, conditions)
+ if condition == nil {
Expand Down Expand Up @@ -200,33 +241,3 @@ index 000000000..7b00253e5
+
+ return nil
+}
+
+func UpdateDVQuotaNotExceededCondition(conditions []cdiv1.DataVolumeCondition, status corev1.ConditionStatus, message, reason string) []cdiv1.DataVolumeCondition {
+ return updateCondition(conditions, QoutaNotExceededConditionType, status, message, reason)
+}
+
+func UpdateDVQuotaNotExceededConditionByPVC(clientObject client.Client, pvc *corev1.PersistentVolumeClaim, status corev1.ConditionStatus, message, reason string) {
+ dv := getDVByPVC(clientObject, pvc, common.AnnCreatedForDataVolume)
+ if dv == nil {
+ return
+ }
+
+ dv.Status.Conditions = updateCondition(dv.Status.Conditions, QoutaNotExceededConditionType, status, message, reason)
+ _ = clientObject.Status().Update(context.TODO(), dv)
+}
+
+func CreateDVQuotaIsNotExceededConditionIfNotExists(conditions *[]cdiv1.DataVolumeCondition) {
+ if conditions == nil {
+ return
+ }
+
+ condition := FindConditionByType(QoutaNotExceededConditionType, *conditions)
+ if condition == nil {
+ *conditions = append(*conditions, cdiv1.DataVolumeCondition{
+ Type: QoutaNotExceededConditionType,
+ Status: corev1.ConditionTrue,
+ Reason: QuotaNotExceededReason,
+ Message: "",
+ })
+ }
+}

0 comments on commit 89d0c67

Please sign in to comment.