PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/pkg/volume/host_path/host_path.go

https://gitlab.com/leetchang/kubernetes
Go | 298 lines | 216 code | 41 blank | 41 comment | 24 complexity | bc5c429d0e2c0eb78e70b8a5994e3e57 MD5 | raw file
  1. /*
  2. Copyright 2014 The Kubernetes Authors All rights reserved.
  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 host_path
  14. import (
  15. "fmt"
  16. "os"
  17. "regexp"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/resource"
  20. "k8s.io/kubernetes/pkg/types"
  21. "k8s.io/kubernetes/pkg/util"
  22. "k8s.io/kubernetes/pkg/volume"
  23. )
  24. // This is the primary entrypoint for volume plugins.
  25. // The volumeConfig arg provides the ability to configure volume behavior. It is implemented as a pointer to allow nils.
  26. // The hostPathPlugin is used to store the volumeConfig and give it, when needed, to the func that creates HostPath Recyclers.
  27. // Tests that exercise recycling should not use this func but instead use ProbeRecyclablePlugins() to override default behavior.
  28. func ProbeVolumePlugins(volumeConfig volume.VolumeConfig) []volume.VolumePlugin {
  29. return []volume.VolumePlugin{
  30. &hostPathPlugin{
  31. host: nil,
  32. newRecyclerFunc: newRecycler,
  33. newDeleterFunc: newDeleter,
  34. newCreaterFunc: newCreater,
  35. config: volumeConfig,
  36. },
  37. }
  38. }
  39. func ProbeRecyclableVolumePlugins(recyclerFunc func(spec *volume.Spec, host volume.VolumeHost, volumeConfig volume.VolumeConfig) (volume.Recycler, error), volumeConfig volume.VolumeConfig) []volume.VolumePlugin {
  40. return []volume.VolumePlugin{
  41. &hostPathPlugin{
  42. host: nil,
  43. newRecyclerFunc: recyclerFunc,
  44. newCreaterFunc: newCreater,
  45. config: volumeConfig,
  46. },
  47. }
  48. }
  49. type hostPathPlugin struct {
  50. host volume.VolumeHost
  51. // decouple creating Recyclers/Deleters/Creaters by deferring to a function. Allows for easier testing.
  52. newRecyclerFunc func(spec *volume.Spec, host volume.VolumeHost, volumeConfig volume.VolumeConfig) (volume.Recycler, error)
  53. newDeleterFunc func(spec *volume.Spec, host volume.VolumeHost) (volume.Deleter, error)
  54. newCreaterFunc func(options volume.VolumeOptions, host volume.VolumeHost) (volume.Creater, error)
  55. config volume.VolumeConfig
  56. }
  57. var _ volume.VolumePlugin = &hostPathPlugin{}
  58. var _ volume.PersistentVolumePlugin = &hostPathPlugin{}
  59. var _ volume.RecyclableVolumePlugin = &hostPathPlugin{}
  60. var _ volume.DeletableVolumePlugin = &hostPathPlugin{}
  61. var _ volume.CreatableVolumePlugin = &hostPathPlugin{}
  62. const (
  63. hostPathPluginName = "kubernetes.io/host-path"
  64. )
  65. func (plugin *hostPathPlugin) Init(host volume.VolumeHost) {
  66. plugin.host = host
  67. }
  68. func (plugin *hostPathPlugin) Name() string {
  69. return hostPathPluginName
  70. }
  71. func (plugin *hostPathPlugin) CanSupport(spec *volume.Spec) bool {
  72. return (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.HostPath != nil) ||
  73. (spec.Volume != nil && spec.Volume.HostPath != nil)
  74. }
  75. func (plugin *hostPathPlugin) GetAccessModes() []api.PersistentVolumeAccessMode {
  76. return []api.PersistentVolumeAccessMode{
  77. api.ReadWriteOnce,
  78. }
  79. }
  80. func (plugin *hostPathPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume.VolumeOptions) (volume.Builder, error) {
  81. if spec.Volume != nil && spec.Volume.HostPath != nil {
  82. return &hostPathBuilder{
  83. hostPath: &hostPath{path: spec.Volume.HostPath.Path},
  84. readOnly: false,
  85. }, nil
  86. } else {
  87. return &hostPathBuilder{
  88. hostPath: &hostPath{path: spec.PersistentVolume.Spec.HostPath.Path},
  89. readOnly: spec.ReadOnly,
  90. }, nil
  91. }
  92. }
  93. func (plugin *hostPathPlugin) NewCleaner(volName string, podUID types.UID) (volume.Cleaner, error) {
  94. return &hostPathCleaner{&hostPath{""}}, nil
  95. }
  96. func (plugin *hostPathPlugin) NewRecycler(spec *volume.Spec) (volume.Recycler, error) {
  97. return plugin.newRecyclerFunc(spec, plugin.host, plugin.config)
  98. }
  99. func (plugin *hostPathPlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  100. return plugin.newDeleterFunc(spec, plugin.host)
  101. }
  102. func (plugin *hostPathPlugin) NewCreater(options volume.VolumeOptions) (volume.Creater, error) {
  103. if len(options.AccessModes) == 0 {
  104. options.AccessModes = plugin.GetAccessModes()
  105. }
  106. return plugin.newCreaterFunc(options, plugin.host)
  107. }
  108. func newRecycler(spec *volume.Spec, host volume.VolumeHost, config volume.VolumeConfig) (volume.Recycler, error) {
  109. if spec.PersistentVolume == nil || spec.PersistentVolume.Spec.HostPath == nil {
  110. return nil, fmt.Errorf("spec.PersistentVolumeSource.HostPath is nil")
  111. }
  112. return &hostPathRecycler{
  113. name: spec.Name(),
  114. path: spec.PersistentVolume.Spec.HostPath.Path,
  115. host: host,
  116. config: config,
  117. timeout: volume.CalculateTimeoutForVolume(config.RecyclerMinimumTimeout, config.RecyclerTimeoutIncrement, spec.PersistentVolume),
  118. }, nil
  119. }
  120. func newDeleter(spec *volume.Spec, host volume.VolumeHost) (volume.Deleter, error) {
  121. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.HostPath == nil {
  122. return nil, fmt.Errorf("spec.PersistentVolumeSource.HostPath is nil")
  123. }
  124. return &hostPathDeleter{spec.Name(), spec.PersistentVolume.Spec.HostPath.Path, host}, nil
  125. }
  126. func newCreater(options volume.VolumeOptions, host volume.VolumeHost) (volume.Creater, error) {
  127. return &hostPathCreater{options: options, host: host}, nil
  128. }
  129. // HostPath volumes represent a bare host file or directory mount.
  130. // The direct at the specified path will be directly exposed to the container.
  131. type hostPath struct {
  132. path string
  133. }
  134. func (hp *hostPath) GetPath() string {
  135. return hp.path
  136. }
  137. type hostPathBuilder struct {
  138. *hostPath
  139. readOnly bool
  140. }
  141. var _ volume.Builder = &hostPathBuilder{}
  142. func (b *hostPathBuilder) GetAttributes() volume.Attributes {
  143. return volume.Attributes{
  144. ReadOnly: b.readOnly,
  145. Managed: false,
  146. SupportsOwnershipManagement: false,
  147. SupportsSELinux: false,
  148. }
  149. }
  150. // SetUp does nothing.
  151. func (b *hostPathBuilder) SetUp() error {
  152. return nil
  153. }
  154. // SetUpAt does not make sense for host paths - probably programmer error.
  155. func (b *hostPathBuilder) SetUpAt(dir string) error {
  156. return fmt.Errorf("SetUpAt() does not make sense for host paths")
  157. }
  158. func (b *hostPathBuilder) GetPath() string {
  159. return b.path
  160. }
  161. type hostPathCleaner struct {
  162. *hostPath
  163. }
  164. var _ volume.Cleaner = &hostPathCleaner{}
  165. // TearDown does nothing.
  166. func (c *hostPathCleaner) TearDown() error {
  167. return nil
  168. }
  169. // TearDownAt does not make sense for host paths - probably programmer error.
  170. func (c *hostPathCleaner) TearDownAt(dir string) error {
  171. return fmt.Errorf("TearDownAt() does not make sense for host paths")
  172. }
  173. // hostPathRecycler implements a dynamic provisioning Recycler for the HostPath plugin
  174. // This implementation is meant for testing only and only works in a single node cluster
  175. type hostPathRecycler struct {
  176. name string
  177. path string
  178. host volume.VolumeHost
  179. config volume.VolumeConfig
  180. timeout int64
  181. }
  182. func (r *hostPathRecycler) GetPath() string {
  183. return r.path
  184. }
  185. // Recycle recycles/scrubs clean a HostPath volume.
  186. // Recycle blocks until the pod has completed or any error occurs.
  187. // HostPath recycling only works in single node clusters and is meant for testing purposes only.
  188. func (r *hostPathRecycler) Recycle() error {
  189. pod := r.config.RecyclerPodTemplate
  190. // overrides
  191. pod.Spec.ActiveDeadlineSeconds = &r.timeout
  192. pod.GenerateName = "pv-recycler-hostpath-"
  193. pod.Spec.Volumes[0].VolumeSource = api.VolumeSource{
  194. HostPath: &api.HostPathVolumeSource{
  195. Path: r.path,
  196. },
  197. }
  198. return volume.RecycleVolumeByWatchingPodUntilCompletion(pod, r.host.GetKubeClient())
  199. }
  200. // hostPathCreater implements a dynamic provisioning Creater for the HostPath plugin
  201. // This implementation is meant for testing only and only works in a single node cluster.
  202. type hostPathCreater struct {
  203. host volume.VolumeHost
  204. options volume.VolumeOptions
  205. }
  206. // Create for hostPath simply creates a local /tmp/hostpath_pv/%s directory as a new PersistentVolume.
  207. // This Creater is meant for development and testing only and WILL NOT WORK in a multi-node cluster.
  208. func (r *hostPathCreater) Create() (*api.PersistentVolume, error) {
  209. fullpath := fmt.Sprintf("/tmp/hostpath_pv/%s", util.NewUUID())
  210. err := os.MkdirAll(fullpath, 0750)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return &api.PersistentVolume{
  215. ObjectMeta: api.ObjectMeta{
  216. GenerateName: "pv-hostpath-",
  217. Labels: map[string]string{
  218. "createdby": "hostpath dynamic provisioner",
  219. },
  220. },
  221. Spec: api.PersistentVolumeSpec{
  222. PersistentVolumeReclaimPolicy: r.options.PersistentVolumeReclaimPolicy,
  223. AccessModes: r.options.AccessModes,
  224. Capacity: api.ResourceList{
  225. api.ResourceName(api.ResourceStorage): resource.MustParse(fmt.Sprintf("%dMi", r.options.CapacityMB)),
  226. },
  227. PersistentVolumeSource: api.PersistentVolumeSource{
  228. HostPath: &api.HostPathVolumeSource{
  229. Path: fullpath,
  230. },
  231. },
  232. },
  233. }, nil
  234. }
  235. // hostPathDeleter deletes a hostPath PV from the cluster.
  236. // This deleter only works on a single host cluster and is for testing purposes only.
  237. type hostPathDeleter struct {
  238. name string
  239. path string
  240. host volume.VolumeHost
  241. }
  242. func (r *hostPathDeleter) GetPath() string {
  243. return r.path
  244. }
  245. // Delete for hostPath removes the local directory so long as it is beneath /tmp/*.
  246. // THIS IS FOR TESTING AND LOCAL DEVELOPMENT ONLY! This message should scare you away from using
  247. // this deleter for anything other than development and testing.
  248. func (r *hostPathDeleter) Delete() error {
  249. regexp := regexp.MustCompile("/tmp/.+")
  250. if !regexp.MatchString(r.GetPath()) {
  251. return fmt.Errorf("host_path deleter only supports /tmp/.+ but received provided %s", r.GetPath())
  252. }
  253. return os.RemoveAll(r.GetPath())
  254. }