Merge pull request #30304 from screeley44/k8-validate-pv

Automatic merge from submit-queue

add validation for PV spec to ensure correct values are used for ReclaimPolicy on initial create

k8 currently allows invalid values for ReclaimPolicy (i.e. "scotto") - this allows the PV to be created and even bound, however, when the pvc or pod is deleted and the recycler is triggered, an error is thrown

```
Events:
  FirstSeen	LastSeen	Count	From				SubobjectPath	Type		Reason				Message
  ---------	--------	-----	----				-------------	--------	------				-------
  36s		36s		1	{persistentvolume-controller }			Warning		VolumeUnknownReclaimPolicy	Volume has unrecognized PersistentVolumeReclaimPolicy
```

New behavior will not allow the user to create the PV:

```
[root@k8dev nfs]# kubectl create -f nfs-pv-bad.yaml 
The PersistentVolume "pv-gce" is invalid: spec.persistentVolumeReclaimPolicy: Unsupported value: "scotto": supported values: Delete, Recycle, Retain
```
This commit is contained in:
Kubernetes Submit Queue 2016-08-26 16:24:25 -07:00 committed by GitHub
commit 4dc5d44879

View File

@ -994,6 +994,8 @@ var ValidatePersistentVolumeName = NameIsDNSSubdomain
var supportedAccessModes = sets.NewString(string(api.ReadWriteOnce), string(api.ReadOnlyMany), string(api.ReadWriteMany))
var supportedReclaimPolicy = sets.NewString(string(api.PersistentVolumeReclaimDelete), string(api.PersistentVolumeReclaimRecycle), string(api.PersistentVolumeReclaimRetain))
func ValidatePersistentVolume(pv *api.PersistentVolume) field.ErrorList {
allErrs := ValidateObjectMeta(&pv.ObjectMeta, false, ValidatePersistentVolumeName, field.NewPath("metadata"))
@ -1018,6 +1020,11 @@ func ValidatePersistentVolume(pv *api.PersistentVolume) field.ErrorList {
for r, qty := range pv.Spec.Capacity {
allErrs = append(allErrs, validateBasicResource(qty, capPath.Key(string(r)))...)
}
if len(string(pv.Spec.PersistentVolumeReclaimPolicy)) > 0 {
if !supportedReclaimPolicy.Has(string(pv.Spec.PersistentVolumeReclaimPolicy)) {
allErrs = append(allErrs, field.NotSupported(specPath.Child("persistentVolumeReclaimPolicy"), pv.Spec.PersistentVolumeReclaimPolicy, supportedReclaimPolicy.List()))
}
}
numVolumes := 0
if pv.Spec.HostPath != nil {