PageRenderTime 85ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/compare/Godeps/_workspace/src/k8s.io/kubernetes/pkg/volume/host_path/host_path.go

https://gitlab.com/jasonbishop/contrib
Go | 311 lines | 229 code | 41 blank | 41 comment | 24 complexity | 8b9246c714e361b675e09d998f9d1c7d 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) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume.VolumeOptions) (volume.Builder, error) {
  81. if spec.Volume != nil && spec.Volume.HostPath != nil {
  82. path := spec.Volume.HostPath.Path
  83. return &hostPathBuilder{
  84. hostPath: &hostPath{path: path, MetricsProvider: volume.NewMetricsDu(path)},
  85. readOnly: false,
  86. }, nil
  87. } else {
  88. path := spec.PersistentVolume.Spec.HostPath.Path
  89. return &hostPathBuilder{
  90. hostPath: &hostPath{path: path, MetricsProvider: volume.NewMetricsDu(path)},
  91. readOnly: spec.ReadOnly,
  92. }, nil
  93. }
  94. }
  95. func (plugin *hostPathPlugin) NewCleaner(volName string, podUID types.UID) (volume.Cleaner, error) {
  96. return &hostPathCleaner{&hostPath{
  97. path: "",
  98. MetricsProvider: volume.NewMetricsDu(""),
  99. }}, nil
  100. }
  101. func (plugin *hostPathPlugin) NewRecycler(spec *volume.Spec) (volume.Recycler, error) {
  102. return plugin.newRecyclerFunc(spec, plugin.host, plugin.config)
  103. }
  104. func (plugin *hostPathPlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  105. return plugin.newDeleterFunc(spec, plugin.host)
  106. }
  107. func (plugin *hostPathPlugin) NewProvisioner(options volume.VolumeOptions) (volume.Provisioner, error) {
  108. if len(options.AccessModes) == 0 {
  109. options.AccessModes = plugin.GetAccessModes()
  110. }
  111. return plugin.newProvisionerFunc(options, plugin.host)
  112. }
  113. func newRecycler(spec *volume.Spec, host volume.VolumeHost, config volume.VolumeConfig) (volume.Recycler, error) {
  114. if spec.PersistentVolume == nil || spec.PersistentVolume.Spec.HostPath == nil {
  115. return nil, fmt.Errorf("spec.PersistentVolumeSource.HostPath is nil")
  116. }
  117. path := spec.PersistentVolume.Spec.HostPath.Path
  118. return &hostPathRecycler{
  119. name: spec.Name(),
  120. path: path,
  121. host: host,
  122. config: config,
  123. timeout: volume.CalculateTimeoutForVolume(config.RecyclerMinimumTimeout, config.RecyclerTimeoutIncrement, spec.PersistentVolume),
  124. MetricsProvider: volume.NewMetricsDu(path),
  125. }, nil
  126. }
  127. func newDeleter(spec *volume.Spec, host volume.VolumeHost) (volume.Deleter, error) {
  128. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.HostPath == nil {
  129. return nil, fmt.Errorf("spec.PersistentVolumeSource.HostPath is nil")
  130. }
  131. path := spec.PersistentVolume.Spec.HostPath.Path
  132. return &hostPathDeleter{spec.Name(), path, host, volume.NewMetricsDu(path)}, nil
  133. }
  134. func newProvisioner(options volume.VolumeOptions, host volume.VolumeHost) (volume.Provisioner, error) {
  135. return &hostPathProvisioner{options: options, host: host}, nil
  136. }
  137. // HostPath volumes represent a bare host file or directory mount.
  138. // The direct at the specified path will be directly exposed to the container.
  139. type hostPath struct {
  140. path string
  141. volume.MetricsProvider
  142. }
  143. func (hp *hostPath) GetPath() string {
  144. return hp.path
  145. }
  146. type hostPathBuilder struct {
  147. *hostPath
  148. readOnly bool
  149. }
  150. var _ volume.Builder = &hostPathBuilder{}
  151. func (b *hostPathBuilder) GetAttributes() volume.Attributes {
  152. return volume.Attributes{
  153. ReadOnly: b.readOnly,
  154. Managed: false,
  155. SupportsOwnershipManagement: false,
  156. SupportsSELinux: false,
  157. }
  158. }
  159. // SetUp does nothing.
  160. func (b *hostPathBuilder) SetUp() error {
  161. return nil
  162. }
  163. // SetUpAt does not make sense for host paths - probably programmer error.
  164. func (b *hostPathBuilder) SetUpAt(dir string) error {
  165. return fmt.Errorf("SetUpAt() does not make sense for host paths")
  166. }
  167. func (b *hostPathBuilder) GetPath() string {
  168. return b.path
  169. }
  170. type hostPathCleaner struct {
  171. *hostPath
  172. }
  173. var _ volume.Cleaner = &hostPathCleaner{}
  174. // TearDown does nothing.
  175. func (c *hostPathCleaner) TearDown() error {
  176. return nil
  177. }
  178. // TearDownAt does not make sense for host paths - probably programmer error.
  179. func (c *hostPathCleaner) TearDownAt(dir string) error {
  180. return fmt.Errorf("TearDownAt() does not make sense for host paths")
  181. }
  182. // hostPathRecycler implements a Recycler for the HostPath plugin
  183. // This implementation is meant for testing only and only works in a single node cluster
  184. type hostPathRecycler struct {
  185. name string
  186. path string
  187. host volume.VolumeHost
  188. config volume.VolumeConfig
  189. timeout int64
  190. volume.MetricsProvider
  191. }
  192. func (r *hostPathRecycler) GetPath() string {
  193. return r.path
  194. }
  195. // Recycle recycles/scrubs clean a HostPath volume.
  196. // Recycle blocks until the pod has completed or any error occurs.
  197. // HostPath recycling only works in single node clusters and is meant for testing purposes only.
  198. func (r *hostPathRecycler) Recycle() error {
  199. pod := r.config.RecyclerPodTemplate
  200. // overrides
  201. pod.Spec.ActiveDeadlineSeconds = &r.timeout
  202. pod.GenerateName = "pv-recycler-hostpath-"
  203. pod.Spec.Volumes[0].VolumeSource = api.VolumeSource{
  204. HostPath: &api.HostPathVolumeSource{
  205. Path: r.path,
  206. },
  207. }
  208. return volume.RecycleVolumeByWatchingPodUntilCompletion(pod, r.host.GetKubeClient())
  209. }
  210. // hostPathProvisioner implements a Provisioner for the HostPath plugin
  211. // This implementation is meant for testing only and only works in a single node cluster.
  212. type hostPathProvisioner struct {
  213. host volume.VolumeHost
  214. options volume.VolumeOptions
  215. }
  216. // Create for hostPath simply creates a local /tmp/hostpath_pv/%s directory as a new PersistentVolume.
  217. // This Provisioner is meant for development and testing only and WILL NOT WORK in a multi-node cluster.
  218. func (r *hostPathProvisioner) Provision(pv *api.PersistentVolume) error {
  219. if pv.Spec.HostPath == nil {
  220. return fmt.Errorf("pv.Spec.HostPath cannot be nil")
  221. }
  222. return os.MkdirAll(pv.Spec.HostPath.Path, 0750)
  223. }
  224. func (r *hostPathProvisioner) NewPersistentVolumeTemplate() (*api.PersistentVolume, error) {
  225. fullpath := fmt.Sprintf("/tmp/hostpath_pv/%s", util.NewUUID())
  226. return &api.PersistentVolume{
  227. ObjectMeta: api.ObjectMeta{
  228. GenerateName: "pv-hostpath-",
  229. Annotations: map[string]string{
  230. "kubernetes.io/createdby": "hostpath-dynamic-provisioner",
  231. },
  232. },
  233. Spec: api.PersistentVolumeSpec{
  234. PersistentVolumeReclaimPolicy: r.options.PersistentVolumeReclaimPolicy,
  235. AccessModes: r.options.AccessModes,
  236. Capacity: api.ResourceList{
  237. api.ResourceName(api.ResourceStorage): r.options.Capacity,
  238. },
  239. PersistentVolumeSource: api.PersistentVolumeSource{
  240. HostPath: &api.HostPathVolumeSource{
  241. Path: fullpath,
  242. },
  243. },
  244. },
  245. }, nil
  246. }
  247. // hostPathDeleter deletes a hostPath PV from the cluster.
  248. // This deleter only works on a single host cluster and is for testing purposes only.
  249. type hostPathDeleter struct {
  250. name string
  251. path string
  252. host volume.VolumeHost
  253. volume.MetricsProvider
  254. }
  255. func (r *hostPathDeleter) GetPath() string {
  256. return r.path
  257. }
  258. // Delete for hostPath removes the local directory so long as it is beneath /tmp/*.
  259. // THIS IS FOR TESTING AND LOCAL DEVELOPMENT ONLY! This message should scare you away from using
  260. // this deleter for anything other than development and testing.
  261. func (r *hostPathDeleter) Delete() error {
  262. regexp := regexp.MustCompile("/tmp/.+")
  263. if !regexp.MatchString(r.GetPath()) {
  264. return fmt.Errorf("host_path deleter only supports /tmp/.+ but received provided %s", r.GetPath())
  265. }
  266. return os.RemoveAll(r.GetPath())
  267. }