pv_validation.go 2.2 KB

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