subpath.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2019 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 subpath
  14. import "os"
  15. // Interface defines the set of methods all subpathers must implement
  16. type Interface interface {
  17. // CleanSubPaths removes any bind-mounts created by PrepareSafeSubpath in given
  18. // pod volume directory.
  19. CleanSubPaths(poodDir string, volumeName string) error
  20. // PrepareSafeSubpath does everything that's necessary to prepare a subPath
  21. // that's 1) inside given volumePath and 2) immutable after this call.
  22. //
  23. // newHostPath - location of prepared subPath. It should be used instead of
  24. // hostName when running the container.
  25. // cleanupAction - action to run when the container is running or it failed to start.
  26. //
  27. // CleanupAction must be called immediately after the container with given
  28. // subpath starts. On the other hand, Interface.CleanSubPaths must be called
  29. // when the pod finishes.
  30. PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error)
  31. // SafeMakeDir creates subdir within given base. It makes sure that the
  32. // created directory does not escape given base directory mis-using
  33. // symlinks. Note that the function makes sure that it creates the directory
  34. // somewhere under the base, nothing else. E.g. if the directory already
  35. // exists, it may exist outside of the base due to symlinks.
  36. // This method should be used if the directory to create is inside volume
  37. // that's under user control. User must not be able to use symlinks to
  38. // escape the volume to create directories somewhere else.
  39. SafeMakeDir(subdir string, base string, perm os.FileMode) error
  40. }
  41. // Subpath defines the attributes of a subpath
  42. type Subpath struct {
  43. // index of the VolumeMount for this container
  44. VolumeMountIndex int
  45. // Full path to the subpath directory on the host
  46. Path string
  47. // name of the volume that is a valid directory name.
  48. VolumeName string
  49. // Full path to the volume path
  50. VolumePath string
  51. // Path to the pod's directory, including pod UID
  52. PodDir string
  53. // Name of the container
  54. ContainerName string
  55. }
  56. // Compile time-check for all implementers of subpath interface
  57. var _ Interface = &subpath{}
  58. var _ Interface = &FakeSubpath{}
  59. // FakeSubpath is a subpather implementation for testing
  60. type FakeSubpath struct{}
  61. // PrepareSafeSubpath is a fake implementation of PrepareSafeSubpath. Always returns
  62. // newHostPath == subPath.Path
  63. func (fs *FakeSubpath) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) {
  64. return subPath.Path, nil, nil
  65. }
  66. // CleanSubPaths is a fake implementation of CleanSubPaths. It is a noop
  67. func (fs *FakeSubpath) CleanSubPaths(podDir string, volumeName string) error {
  68. return nil
  69. }
  70. // SafeMakeDir is a fake implementation of SafeMakeDir. It is a noop
  71. func (fs *FakeSubpath) SafeMakeDir(pathname string, base string, perm os.FileMode) error {
  72. return nil
  73. }