nested_volumes.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2018 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 util
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "sort"
  19. "strings"
  20. "k8s.io/api/core/v1"
  21. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  22. )
  23. // getNestedMountpoints returns a list of mountpoint directories that should be created
  24. // for the volume indicated by name.
  25. // note: the returned list is relative to baseDir
  26. func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) {
  27. var retval []string
  28. checkContainer := func(container *v1.Container) error {
  29. var allMountPoints []string // all mount points in this container
  30. var myMountPoints []string // mount points that match name
  31. for _, vol := range container.VolumeMounts {
  32. cleaned := filepath.Clean(vol.MountPath)
  33. allMountPoints = append(allMountPoints, cleaned)
  34. if vol.Name == name {
  35. myMountPoints = append(myMountPoints, cleaned)
  36. }
  37. }
  38. sort.Strings(allMountPoints)
  39. parentPrefix := ".." + string(os.PathSeparator)
  40. // Examine each place where this volume is mounted
  41. for _, myMountPoint := range myMountPoints {
  42. if strings.HasPrefix(myMountPoint, parentPrefix) {
  43. // Don't let a container trick us into creating directories outside of its rootfs
  44. return fmt.Errorf("Invalid container mount point %v", myMountPoint)
  45. }
  46. myMPSlash := myMountPoint + string(os.PathSeparator)
  47. // The previously found nested mountpoint (or "" if none found yet)
  48. prevNestedMP := ""
  49. // examine each mount point to see if it's nested beneath this volume
  50. // (but skip any that are double-nested beneath this volume)
  51. // For example, if this volume is mounted as /dir and other volumes are mounted
  52. // as /dir/nested and /dir/nested/other, only create /dir/nested.
  53. for _, mp := range allMountPoints {
  54. if !strings.HasPrefix(mp, myMPSlash) {
  55. continue // skip -- not nested beneath myMountPoint
  56. }
  57. if prevNestedMP != "" && strings.HasPrefix(mp, prevNestedMP) {
  58. continue // skip -- double nested beneath myMountPoint
  59. }
  60. // since this mount point is nested, remember it so that we can check that following ones aren't nested beneath this one
  61. prevNestedMP = mp + string(os.PathSeparator)
  62. retval = append(retval, mp[len(myMPSlash):])
  63. }
  64. }
  65. return nil
  66. }
  67. var retErr error
  68. podutil.VisitContainers(&pod.Spec, func(c *v1.Container) bool {
  69. retErr = checkContainer(c)
  70. if retErr != nil {
  71. return false
  72. }
  73. return true
  74. })
  75. if retErr != nil {
  76. return nil, retErr
  77. }
  78. return retval, nil
  79. }
  80. // MakeNestedMountpoints creates mount points in baseDir for volumes mounted beneath name
  81. func MakeNestedMountpoints(name, baseDir string, pod v1.Pod) error {
  82. dirs, err := getNestedMountpoints(name, baseDir, pod)
  83. if err != nil {
  84. return err
  85. }
  86. for _, dir := range dirs {
  87. err := os.MkdirAll(filepath.Join(baseDir, dir), 0755)
  88. if err != nil {
  89. return fmt.Errorf("Unable to create nested volume mountpoints: %v", err)
  90. }
  91. }
  92. return nil
  93. }