nested_volumes.go 3.4 KB

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