From 3a22b5f5e090f90cbc8a5d2bab79af2e7de97f6e Mon Sep 17 00:00:00 2001 From: NickrenREN Date: Mon, 6 Nov 2017 21:23:27 +0800 Subject: [PATCH] Make sure that storage request of pvc is not zero --- pkg/apis/core/validation/validation.go | 4 ++++ pkg/apis/core/validation/validation_test.go | 22 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index c3878644cd5..b2b82c17f70 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -62,6 +62,7 @@ const isNegativeErrorMsg string = apimachineryvalidation.IsNegativeErrorMsg const isInvalidQuotaResource string = `must be a standard resource for quota` const fieldImmutableErrorMsg string = genericvalidation.FieldImmutableErrorMsg const isNotIntegerErrorMsg string = `must be an integer` +const isZeroErrorMsg string = `must be greater than zero` var pdPartitionErrorMsg string = validation.InclusiveRangeError(1, 255) var volumeModeErrorMsg string = "must be a number between 0 and 0777 (octal), both inclusive" @@ -1601,6 +1602,9 @@ func ValidatePersistentVolumeClaimSpec(spec *core.PersistentVolumeClaimSpec, fld allErrs = append(allErrs, field.Required(fldPath.Child("resources").Key(string(core.ResourceStorage)), "")) } else { allErrs = append(allErrs, ValidateResourceQuantityValue(string(core.ResourceStorage), storageValue, fldPath.Child("resources").Key(string(core.ResourceStorage)))...) + if storageValue.Value() == int64(0) { + allErrs = append(allErrs, field.Invalid(fldPath, storageValue, isZeroErrorMsg)) + } } if spec.StorageClassName != nil && len(*spec.StorageClassName) > 0 { diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index 07d03fe56df..fe43526feb1 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -765,6 +765,28 @@ func TestValidatePersistentVolumeClaim(t *testing.T) { }, }), }, + "zero-storage-request": { + isExpectedFailure: true, + claim: testVolumeClaim("foo", "ns", core.PersistentVolumeClaimSpec{ + Selector: &metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "key2", + Operator: "Exists", + }, + }, + }, + AccessModes: []core.PersistentVolumeAccessMode{ + core.ReadWriteOnce, + core.ReadOnlyMany, + }, + Resources: core.ResourceRequirements{ + Requests: core.ResourceList{ + core.ResourceName(api.ResourceStorage): resource.MustParse("0G"), + }, + }, + }), + }, "invalid-storage-class-name": { isExpectedFailure: true, claim: testVolumeClaim("foo", "ns", core.PersistentVolumeClaimSpec{