PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/pkg/volume/host_path/host_path.go

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