-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vd): add info represents quota exceed state (#586)
Signed-off-by: Valeriy Khorunzhin <[email protected]>
- Loading branch information
Showing
11 changed files
with
362 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
images/cdi-artifact/patches/022-add-datavolume-quouta-not-exceeded-condition.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
diff --git a/pkg/controller/datavolume/controller-base.go b/pkg/controller/datavolume/controller-base.go | ||
index acd09cb94..2fb859150 100644 | ||
--- a/pkg/controller/datavolume/controller-base.go | ||
+++ b/pkg/controller/datavolume/controller-base.go | ||
@@ -55,6 +55,7 @@ import ( | ||
cloneMetrics "kubevirt.io/containerized-data-importer/pkg/monitoring/metrics/cdi-cloner" | ||
metrics "kubevirt.io/containerized-data-importer/pkg/monitoring/metrics/cdi-controller" | ||
importMetrics "kubevirt.io/containerized-data-importer/pkg/monitoring/metrics/cdi-importer" | ||
+ patchedDV "kubevirt.io/containerized-data-importer/pkg/patcheddatavolume" | ||
"kubevirt.io/containerized-data-importer/pkg/token" | ||
"kubevirt.io/containerized-data-importer/pkg/util" | ||
) | ||
@@ -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) | ||
+ 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..972f8ab5f 100644 | ||
--- a/pkg/controller/import-controller.go | ||
+++ b/pkg/controller/import-controller.go | ||
@@ -34,6 +34,7 @@ import ( | ||
"kubevirt.io/containerized-data-importer/pkg/common" | ||
cc "kubevirt.io/containerized-data-importer/pkg/controller/common" | ||
featuregates "kubevirt.io/containerized-data-importer/pkg/feature-gates" | ||
+ patchedDV "kubevirt.io/containerized-data-importer/pkg/patcheddatavolume" | ||
"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,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") { | ||
+ 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..8fcda592c 100644 | ||
--- a/pkg/controller/populators/populator-base.go | ||
+++ b/pkg/controller/populators/populator-base.go | ||
@@ -18,7 +18,9 @@ package populators | ||
|
||
import ( | ||
"context" | ||
+ "fmt" | ||
"reflect" | ||
+ "strings" | ||
|
||
"github.com/go-logr/logr" | ||
|
||
@@ -40,6 +42,7 @@ import ( | ||
"kubevirt.io/containerized-data-importer/pkg/common" | ||
cc "kubevirt.io/containerized-data-importer/pkg/controller/common" | ||
featuregates "kubevirt.io/containerized-data-importer/pkg/feature-gates" | ||
+ patchedDV "kubevirt.io/containerized-data-importer/pkg/patcheddatavolume" | ||
"kubevirt.io/containerized-data-importer/pkg/util" | ||
) | ||
|
||
@@ -182,6 +185,11 @@ func (r *ReconcilerBase) createPVCPrime(pvc *corev1.PersistentVolumeClaim, sourc | ||
annotations[cc.AnnPodRetainAfterCompletion] = pvc.Annotations[cc.AnnPodRetainAfterCompletion] | ||
} | ||
|
||
+ dvUid, ok := pvc.Annotations[cc.AnnCreatedForDataVolume] | ||
+ if ok { | ||
+ annotations[cc.AnnCreatedForDataVolume] = dvUid | ||
+ } | ||
+ | ||
// Assemble PVC' spec | ||
pvcPrime := &corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
@@ -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") { | ||
+ 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..e21e40312 100644 | ||
--- a/pkg/controller/upload-controller.go | ||
+++ b/pkg/controller/upload-controller.go | ||
@@ -51,6 +51,7 @@ import ( | ||
cc "kubevirt.io/containerized-data-importer/pkg/controller/common" | ||
featuregates "kubevirt.io/containerized-data-importer/pkg/feature-gates" | ||
"kubevirt.io/containerized-data-importer/pkg/operator" | ||
+ patchedDV "kubevirt.io/containerized-data-importer/pkg/patcheddatavolume" | ||
"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,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") { | ||
+ 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..29fc38c97 | ||
--- /dev/null | ||
+++ b/pkg/patcheddatavolume/patched_datavolume.go | ||
@@ -0,0 +1,124 @@ | ||
+package patcheddatavolume | ||
+ | ||
+import ( | ||
+ "context" | ||
+ "fmt" | ||
+ "strings" | ||
+ | ||
+ corev1 "k8s.io/api/core/v1" | ||
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
+ cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1" | ||
+ "kubevirt.io/containerized-data-importer/pkg/controller/common" | ||
+ "sigs.k8s.io/controller-runtime/pkg/client" | ||
+) | ||
+ | ||
+const ( | ||
+ QoutaNotExceededConditionType cdiv1.DataVolumeConditionType = "QuotaNotExceeded" | ||
+ | ||
+ QuotaNotExceededReason string = "QuotaNotExceeded" | ||
+ QuotaExceededReason string = "QuotaExceeded" | ||
+ | ||
+ RunningConditionErrorReason string = "Error" | ||
+) | ||
+ | ||
+func FindConditionByType(conditionType cdiv1.DataVolumeConditionType, conditions []cdiv1.DataVolumeCondition) *cdiv1.DataVolumeCondition { | ||
+ for i, condition := range conditions { | ||
+ if condition.Type == conditionType { | ||
+ return &conditions[i] | ||
+ } | ||
+ } | ||
+ 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", boundCondition.Message), QuotaExceededReason) | ||
+ case runningCondition != nil: | ||
+ if runningCondition.Reason == common.ErrExceededQuota || | ||
+ runningCondition.Reason == RunningConditionErrorReason && strings.Contains(runningCondition.Message, "exceeded quota") { | ||
+ conditions = updateCondition(conditions, QoutaNotExceededConditionType, corev1.ConditionFalse, fmt.Sprintf("Exceeded quota: %q", runningCondition.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 { | ||
+ conditions = append(conditions, cdiv1.DataVolumeCondition{ | ||
+ Type: conditionType, | ||
+ }) | ||
+ condition = &conditions[len(conditions)-1] | ||
+ } | ||
+ if condition.Status != status { | ||
+ condition.LastTransitionTime = metav1.Now() | ||
+ condition.Message = message | ||
+ condition.Reason = reason | ||
+ condition.LastHeartbeatTime = condition.LastTransitionTime | ||
+ } else if condition.Message != message || condition.Reason != reason { | ||
+ condition.Message = message | ||
+ condition.Reason = reason | ||
+ condition.LastHeartbeatTime = metav1.Now() | ||
+ } | ||
+ condition.Status = status | ||
+ return conditions | ||
+} | ||
+ | ||
+func getDVByPVC(clientObject client.Client, pvc *corev1.PersistentVolumeClaim, ann string) *cdiv1.DataVolume { | ||
+ uid, ok := pvc.Annotations[ann] | ||
+ if !ok { | ||
+ return nil | ||
+ } | ||
+ | ||
+ var dvList cdiv1.DataVolumeList | ||
+ | ||
+ err := clientObject.List(context.TODO(), &dvList, client.InNamespace(pvc.Namespace)) | ||
+ if err != nil { | ||
+ return nil | ||
+ } | ||
+ | ||
+ for _, dv := range dvList.Items { | ||
+ if string(dv.UID) == uid { | ||
+ return &dv | ||
+ } | ||
+ } | ||
+ | ||
+ return nil | ||
+} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.