gce_pd_block.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright 2018 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 gcepd
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "strconv"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. "k8s.io/klog"
  21. "k8s.io/kubernetes/pkg/util/mount"
  22. "k8s.io/kubernetes/pkg/volume"
  23. "k8s.io/kubernetes/pkg/volume/util"
  24. "k8s.io/kubernetes/pkg/volume/util/volumepathhandler"
  25. utilstrings "k8s.io/utils/strings"
  26. )
  27. var _ volume.VolumePlugin = &gcePersistentDiskPlugin{}
  28. var _ volume.PersistentVolumePlugin = &gcePersistentDiskPlugin{}
  29. var _ volume.BlockVolumePlugin = &gcePersistentDiskPlugin{}
  30. var _ volume.DeletableVolumePlugin = &gcePersistentDiskPlugin{}
  31. var _ volume.ProvisionableVolumePlugin = &gcePersistentDiskPlugin{}
  32. var _ volume.ExpandableVolumePlugin = &gcePersistentDiskPlugin{}
  33. func (plugin *gcePersistentDiskPlugin) ConstructBlockVolumeSpec(podUID types.UID, volumeName, mapPath string) (*volume.Spec, error) {
  34. pluginDir := plugin.host.GetVolumeDevicePluginDir(gcePersistentDiskPluginName)
  35. blkutil := volumepathhandler.NewBlockVolumePathHandler()
  36. globalMapPathUUID, err := blkutil.FindGlobalMapPathUUIDFromPod(pluginDir, mapPath, podUID)
  37. if err != nil {
  38. return nil, err
  39. }
  40. klog.V(5).Infof("globalMapPathUUID: %v, err: %v", globalMapPathUUID, err)
  41. globalMapPath := filepath.Dir(globalMapPathUUID)
  42. if len(globalMapPath) <= 1 {
  43. return nil, fmt.Errorf("failed to get volume plugin information from globalMapPathUUID: %v", globalMapPathUUID)
  44. }
  45. return getVolumeSpecFromGlobalMapPath(globalMapPath)
  46. }
  47. func getVolumeSpecFromGlobalMapPath(globalMapPath string) (*volume.Spec, error) {
  48. // Get volume spec information from globalMapPath
  49. // globalMapPath example:
  50. // plugins/kubernetes.io/{PluginName}/{DefaultKubeletVolumeDevicesDirName}/{volumeID}
  51. // plugins/kubernetes.io/gce-pd/volumeDevices/vol-XXXXXX
  52. pdName := filepath.Base(globalMapPath)
  53. if len(pdName) <= 1 {
  54. return nil, fmt.Errorf("failed to get pd name from global path=%s", globalMapPath)
  55. }
  56. block := v1.PersistentVolumeBlock
  57. gceVolume := &v1.PersistentVolume{
  58. Spec: v1.PersistentVolumeSpec{
  59. PersistentVolumeSource: v1.PersistentVolumeSource{
  60. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  61. PDName: pdName,
  62. },
  63. },
  64. VolumeMode: &block,
  65. },
  66. }
  67. return volume.NewSpecFromPersistentVolume(gceVolume, true), nil
  68. }
  69. // NewBlockVolumeMapper creates a new volume.BlockVolumeMapper from an API specification.
  70. func (plugin *gcePersistentDiskPlugin) NewBlockVolumeMapper(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.BlockVolumeMapper, error) {
  71. // If this is 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, &GCEDiskUtil{}, plugin.host.GetMounter(plugin.GetPluginName()))
  78. }
  79. func (plugin *gcePersistentDiskPlugin) newBlockVolumeMapperInternal(spec *volume.Spec, podUID types.UID, manager pdManager, mounter mount.Interface) (volume.BlockVolumeMapper, error) {
  80. volumeSource, readOnly, err := getVolumeSource(spec)
  81. if err != nil {
  82. return nil, err
  83. }
  84. pdName := volumeSource.PDName
  85. partition := ""
  86. if volumeSource.Partition != 0 {
  87. partition = strconv.Itoa(int(volumeSource.Partition))
  88. }
  89. return &gcePersistentDiskMapper{
  90. gcePersistentDisk: &gcePersistentDisk{
  91. volName: spec.Name(),
  92. podUID: podUID,
  93. pdName: pdName,
  94. partition: partition,
  95. manager: manager,
  96. mounter: mounter,
  97. plugin: plugin,
  98. },
  99. readOnly: readOnly}, nil
  100. }
  101. func (plugin *gcePersistentDiskPlugin) NewBlockVolumeUnmapper(volName string, podUID types.UID) (volume.BlockVolumeUnmapper, error) {
  102. return plugin.newUnmapperInternal(volName, podUID, &GCEDiskUtil{})
  103. }
  104. func (plugin *gcePersistentDiskPlugin) newUnmapperInternal(volName string, podUID types.UID, manager pdManager) (volume.BlockVolumeUnmapper, error) {
  105. return &gcePersistentDiskUnmapper{
  106. gcePersistentDisk: &gcePersistentDisk{
  107. volName: volName,
  108. podUID: podUID,
  109. pdName: volName,
  110. manager: manager,
  111. plugin: plugin,
  112. }}, nil
  113. }
  114. func (c *gcePersistentDiskUnmapper) TearDownDevice(mapPath, devicePath string) error {
  115. return nil
  116. }
  117. type gcePersistentDiskUnmapper struct {
  118. *gcePersistentDisk
  119. }
  120. var _ volume.BlockVolumeUnmapper = &gcePersistentDiskUnmapper{}
  121. type gcePersistentDiskMapper struct {
  122. *gcePersistentDisk
  123. readOnly bool
  124. }
  125. var _ volume.BlockVolumeMapper = &gcePersistentDiskMapper{}
  126. func (b *gcePersistentDiskMapper) SetUpDevice() (string, error) {
  127. return "", nil
  128. }
  129. func (b *gcePersistentDiskMapper) MapDevice(devicePath, globalMapPath, volumeMapPath, volumeMapName string, podUID types.UID) error {
  130. return util.MapBlockVolume(devicePath, globalMapPath, volumeMapPath, volumeMapName, podUID)
  131. }
  132. // GetGlobalMapPath returns global map path and error
  133. // path: plugins/kubernetes.io/{PluginName}/volumeDevices/pdName
  134. func (pd *gcePersistentDisk) GetGlobalMapPath(spec *volume.Spec) (string, error) {
  135. volumeSource, _, err := getVolumeSource(spec)
  136. if err != nil {
  137. return "", err
  138. }
  139. return filepath.Join(pd.plugin.host.GetVolumeDevicePluginDir(gcePersistentDiskPluginName), string(volumeSource.PDName)), nil
  140. }
  141. // GetPodDeviceMapPath returns pod device map path and volume name
  142. // path: pods/{podUid}/volumeDevices/kubernetes.io~aws
  143. func (pd *gcePersistentDisk) GetPodDeviceMapPath() (string, string) {
  144. name := gcePersistentDiskPluginName
  145. return pd.plugin.host.GetPodVolumeDeviceDir(pd.podUID, utilstrings.EscapeQualifiedName(name)), pd.volName
  146. }