PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/pkg/volume/host_path/host_path.go

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