From d22ffb0402f676419bb38ccb493a886d42db2fb5 Mon Sep 17 00:00:00 2001 From: Paul Morie Date: Mon, 22 Aug 2016 14:45:46 -0400 Subject: [PATCH] Add validation preventing recycle of / in a hostPath PV --- pkg/api/validation/validation.go | 6 ++++++ pkg/api/validation/validation_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index b754f39c5c..9f40758783 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -1087,6 +1087,12 @@ func ValidatePersistentVolume(pv *api.PersistentVolume) field.ErrorList { if numVolumes == 0 { allErrs = append(allErrs, field.Required(specPath, "must specify a volume type")) } + + // do not allow hostPath mounts of '/' to have a 'recycle' reclaim policy + if pv.Spec.HostPath != nil && path.Clean(pv.Spec.HostPath.Path) == "/" && pv.Spec.PersistentVolumeReclaimPolicy == api.PersistentVolumeReclaimRecycle { + allErrs = append(allErrs, field.Forbidden(specPath.Child("persistentVolumeReclaimPolicy"), "may not be 'recycle' for a hostPath mount of '/'")) + } + return allErrs } diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index 763f5b8f37..415f896ca9 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -549,6 +549,32 @@ func TestValidatePersistentVolumes(t *testing.T) { }, }), }, + "host mount of / with recycle reclaim policy": { + isExpectedFailure: true, + volume: testVolume("bad-recycle-do-not-want", "", api.PersistentVolumeSpec{ + Capacity: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce}, + PersistentVolumeSource: api.PersistentVolumeSource{ + HostPath: &api.HostPathVolumeSource{Path: "/"}, + }, + PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimRecycle, + }), + }, + "host mount of / with recycle reclaim policy 2": { + isExpectedFailure: true, + volume: testVolume("bad-recycle-do-not-want", "", api.PersistentVolumeSpec{ + Capacity: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce}, + PersistentVolumeSource: api.PersistentVolumeSource{ + HostPath: &api.HostPathVolumeSource{Path: "/a/.."}, + }, + PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimRecycle, + }), + }, } for name, scenario := range scenarios {