k8s.io/kubernetes@v1.29.3/pkg/volume/validation/pv_validation.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package validation 18 19 import ( 20 "errors" 21 "path/filepath" 22 "strings" 23 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 api "k8s.io/kubernetes/pkg/apis/core" 26 ) 27 28 // ValidatePersistentVolume validates PV object for plugin specific validation 29 // We can put here validations which are specific to volume types. 30 func ValidatePersistentVolume(pv *api.PersistentVolume) field.ErrorList { 31 return checkMountOption(pv) 32 } 33 34 func checkMountOption(pv *api.PersistentVolume) field.ErrorList { 35 allErrs := field.ErrorList{} 36 // if PV is of these types we don't return errors 37 // since mount options is supported 38 if pv.Spec.GCEPersistentDisk != nil || 39 pv.Spec.AWSElasticBlockStore != nil || 40 pv.Spec.Glusterfs != nil || 41 pv.Spec.NFS != nil || 42 pv.Spec.RBD != nil || 43 pv.Spec.Quobyte != nil || 44 pv.Spec.ISCSI != nil || 45 pv.Spec.Cinder != nil || 46 pv.Spec.CephFS != nil || 47 pv.Spec.AzureFile != nil || 48 pv.Spec.VsphereVolume != nil || 49 pv.Spec.AzureDisk != nil || 50 pv.Spec.PhotonPersistentDisk != nil { 51 return allErrs 52 } 53 // any other type if mount option is present lets return error 54 if _, ok := pv.Annotations[api.MountOptionAnnotation]; ok { 55 metaField := field.NewPath("metadata") 56 allErrs = append(allErrs, field.Forbidden(metaField.Child("annotations", api.MountOptionAnnotation), "may not specify mount options for this volume type")) 57 } 58 return allErrs 59 } 60 61 // ValidatePathNoBacksteps will make sure the targetPath does not have any element which is ".." 62 func ValidatePathNoBacksteps(targetPath string) error { 63 parts := strings.Split(filepath.ToSlash(targetPath), "/") 64 for _, item := range parts { 65 if item == ".." { 66 return errors.New("must not contain '..'") 67 } 68 } 69 70 return nil 71 }