PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/pkg/volume/host_path/host_path.go

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