vsphere_volume_block.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // +build !providerless
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package vsphere_volume
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "k8s.io/klog"
  20. "k8s.io/utils/mount"
  21. utilstrings "k8s.io/utils/strings"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "k8s.io/kubernetes/pkg/volume"
  26. "k8s.io/kubernetes/pkg/volume/util/volumepathhandler"
  27. )
  28. var _ volume.BlockVolumePlugin = &vsphereVolumePlugin{}
  29. func (plugin *vsphereVolumePlugin) ConstructBlockVolumeSpec(podUID types.UID, volumeName, mapPath string) (*volume.Spec, error) {
  30. pluginDir := plugin.host.GetPluginDir(plugin.GetPluginName())
  31. blkUtil := volumepathhandler.NewBlockVolumePathHandler()
  32. globalMapPathUUID, err := blkUtil.FindGlobalMapPathUUIDFromPod(pluginDir, mapPath, podUID)
  33. if err != nil {
  34. klog.Errorf("Failed to find GlobalMapPathUUID from Pod: %s with error: %+v", podUID, err)
  35. return nil, err
  36. }
  37. klog.V(5).Infof("globalMapPathUUID: %v", globalMapPathUUID)
  38. globalMapPath := filepath.Dir(globalMapPathUUID)
  39. if len(globalMapPath) <= 1 {
  40. return nil, fmt.Errorf("failed to get volume plugin information from globalMapPathUUID: %v", globalMapPathUUID)
  41. }
  42. return getVolumeSpecFromGlobalMapPath(volumeName, globalMapPath)
  43. }
  44. func getVolumeSpecFromGlobalMapPath(volumeName, globalMapPath string) (*volume.Spec, error) {
  45. // Construct volume spec from globalMapPath
  46. // globalMapPath example:
  47. // plugins/kubernetes.io/{PluginName}/{DefaultKubeletVolumeDevicesDirName}/{volumeID}
  48. // plugins/kubernetes.io/vsphere-volume/volumeDevices/[datastore1]\\040volumes/myDisk
  49. volPath := filepath.Base(globalMapPath)
  50. volPath = strings.Replace(volPath, "\\040", "", -1)
  51. if len(volPath) <= 1 {
  52. return nil, fmt.Errorf("failed to get volume path from global path=%s", globalMapPath)
  53. }
  54. block := v1.PersistentVolumeBlock
  55. vsphereVolume := &v1.PersistentVolume{
  56. ObjectMeta: metav1.ObjectMeta{
  57. Name: volumeName,
  58. },
  59. Spec: v1.PersistentVolumeSpec{
  60. PersistentVolumeSource: v1.PersistentVolumeSource{
  61. VsphereVolume: &v1.VsphereVirtualDiskVolumeSource{
  62. VolumePath: volPath,
  63. },
  64. },
  65. VolumeMode: &block,
  66. },
  67. }
  68. return volume.NewSpecFromPersistentVolume(vsphereVolume, true), nil
  69. }
  70. func (plugin *vsphereVolumePlugin) NewBlockVolumeMapper(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.BlockVolumeMapper, error) {
  71. // If this called via GenerateUnmapDeviceFunc(), pod is nil.
  72. // Pass empty string as dummy uid since uid isn't used in the case.
  73. var uid types.UID
  74. if pod != nil {
  75. uid = pod.UID
  76. }
  77. return plugin.newBlockVolumeMapperInternal(spec, uid, &VsphereDiskUtil{}, plugin.host.GetMounter(plugin.GetPluginName()))
  78. }
  79. func (plugin *vsphereVolumePlugin) newBlockVolumeMapperInternal(spec *volume.Spec, podUID types.UID, manager vdManager, mounter mount.Interface) (volume.BlockVolumeMapper, error) {
  80. volumeSource, _, err := getVolumeSource(spec)
  81. if err != nil {
  82. klog.Errorf("Failed to get Volume source from volume Spec: %+v with error: %+v", *spec, err)
  83. return nil, err
  84. }
  85. volPath := volumeSource.VolumePath
  86. return &vsphereBlockVolumeMapper{
  87. vsphereVolume: &vsphereVolume{
  88. volName: spec.Name(),
  89. podUID: podUID,
  90. volPath: volPath,
  91. manager: manager,
  92. mounter: mounter,
  93. plugin: plugin,
  94. MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, spec.Name(), plugin.host)),
  95. },
  96. }, nil
  97. }
  98. func (plugin *vsphereVolumePlugin) NewBlockVolumeUnmapper(volName string, podUID types.UID) (volume.BlockVolumeUnmapper, error) {
  99. return plugin.newUnmapperInternal(volName, podUID, &VsphereDiskUtil{})
  100. }
  101. func (plugin *vsphereVolumePlugin) newUnmapperInternal(volName string, podUID types.UID, manager vdManager) (volume.BlockVolumeUnmapper, error) {
  102. return &vsphereBlockVolumeUnmapper{
  103. vsphereVolume: &vsphereVolume{
  104. volName: volName,
  105. podUID: podUID,
  106. volPath: volName,
  107. manager: manager,
  108. plugin: plugin,
  109. },
  110. }, nil
  111. }
  112. var _ volume.BlockVolumeMapper = &vsphereBlockVolumeMapper{}
  113. type vsphereBlockVolumeMapper struct {
  114. *vsphereVolume
  115. }
  116. var _ volume.BlockVolumeUnmapper = &vsphereBlockVolumeUnmapper{}
  117. type vsphereBlockVolumeUnmapper struct {
  118. *vsphereVolume
  119. }
  120. // GetGlobalMapPath returns global map path and error
  121. // path: plugins/kubernetes.io/{PluginName}/volumeDevices/volumePath
  122. func (v *vsphereVolume) GetGlobalMapPath(spec *volume.Spec) (string, error) {
  123. volumeSource, _, err := getVolumeSource(spec)
  124. if err != nil {
  125. return "", err
  126. }
  127. return filepath.Join(v.plugin.host.GetVolumeDevicePluginDir(vsphereVolumePluginName), string(volumeSource.VolumePath)), nil
  128. }
  129. func (v *vsphereVolume) GetPodDeviceMapPath() (string, string) {
  130. return v.plugin.host.GetPodVolumeDeviceDir(v.podUID, utilstrings.EscapeQualifiedName(vsphereVolumePluginName)), v.volName
  131. }