From 0b848bee4e89d89a1bedf4c38afd9188f2bc3dec Mon Sep 17 00:00:00 2001 From: Paco Xu Date: Fri, 21 Oct 2022 13:31:43 +0800 Subject: [PATCH] pvc storage request warning for fractional byte value - create or update --- pkg/api/persistentvolumeclaim/util.go | 20 +++++++++++++++++++ .../core/persistentvolumeclaim/strategy.go | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/api/persistentvolumeclaim/util.go b/pkg/api/persistentvolumeclaim/util.go index e8b56e74b7d..b8cacf83a4c 100644 --- a/pkg/api/persistentvolumeclaim/util.go +++ b/pkg/api/persistentvolumeclaim/util.go @@ -17,6 +17,10 @@ limitations under the License. package persistentvolumeclaim import ( + "fmt" + + "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/util/validation/field" utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/features" @@ -151,3 +155,19 @@ func allocatedResourcesInUse(oldPVC *core.PersistentVolumeClaim) bool { return false } + +func GetWarningsForPersistentVolumeClaim(pv *core.PersistentVolumeClaim) []string { + if pv == nil { + return nil + } + storageValue := pv.Spec.Resources.Requests[core.ResourceStorage] + return warningsForPersistentVolumeClaimResources(field.NewPath("spec").Child("Resources").Child("Requests").Key(core.ResourceStorage.String()), storageValue) +} + +func warningsForPersistentVolumeClaimResources(fieldPath *field.Path, storageValue resource.Quantity) []string { + var warnings []string + if storageValue.MilliValue()%int64(1000) != int64(0) { + warnings = append(warnings, fmt.Sprintf("%s: fractional byte value %q is invalid, must be an integer", fieldPath.String(), storageValue.String())) + } + return warnings +} diff --git a/pkg/registry/core/persistentvolumeclaim/strategy.go b/pkg/registry/core/persistentvolumeclaim/strategy.go index dad8db0915c..14bf61e2b33 100644 --- a/pkg/registry/core/persistentvolumeclaim/strategy.go +++ b/pkg/registry/core/persistentvolumeclaim/strategy.go @@ -86,7 +86,7 @@ func (persistentvolumeclaimStrategy) Validate(ctx context.Context, obj runtime.O // WarningsOnCreate returns warnings for the creation of the given object. func (persistentvolumeclaimStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { - return nil + return pvcutil.GetWarningsForPersistentVolumeClaim(obj.(*api.PersistentVolumeClaim)) } // Canonicalize normalizes the object after validation. @@ -128,7 +128,7 @@ func (persistentvolumeclaimStrategy) ValidateUpdate(ctx context.Context, obj, ol // WarningsOnUpdate returns warnings for the given update. func (persistentvolumeclaimStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { - return nil + return pvcutil.GetWarningsForPersistentVolumeClaim(obj.(*api.PersistentVolumeClaim)) } func (persistentvolumeclaimStrategy) AllowUnconditionalUpdate() bool {