aws_ebs_block.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 awsebs
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/klog"
  22. "k8s.io/kubernetes/pkg/util/mount"
  23. "k8s.io/kubernetes/pkg/volume"
  24. "k8s.io/kubernetes/pkg/volume/util"
  25. "k8s.io/kubernetes/pkg/volume/util/volumepathhandler"
  26. "k8s.io/legacy-cloud-providers/aws"
  27. utilstrings "k8s.io/utils/strings"
  28. )
  29. var _ volume.BlockVolumePlugin = &awsElasticBlockStorePlugin{}
  30. func (plugin *awsElasticBlockStorePlugin) ConstructBlockVolumeSpec(podUID types.UID, volumeName, mapPath string) (*volume.Spec, error) {
  31. pluginDir := plugin.host.GetVolumeDevicePluginDir(awsElasticBlockStorePluginName)
  32. blkutil := volumepathhandler.NewBlockVolumePathHandler()
  33. globalMapPathUUID, err := blkutil.FindGlobalMapPathUUIDFromPod(pluginDir, mapPath, podUID)
  34. if err != nil {
  35. return nil, err
  36. }
  37. klog.V(5).Infof("globalMapPathUUID: %s", 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(globalMapPath)
  43. }
  44. func getVolumeSpecFromGlobalMapPath(globalMapPath string) (*volume.Spec, error) {
  45. // Get volume spec information from globalMapPath
  46. // globalMapPath example:
  47. // plugins/kubernetes.io/{PluginName}/{DefaultKubeletVolumeDevicesDirName}/{volumeID}
  48. // plugins/kubernetes.io/aws-ebs/volumeDevices/vol-XXXXXX
  49. vID := filepath.Base(globalMapPath)
  50. if len(vID) <= 1 {
  51. return nil, fmt.Errorf("failed to get volumeID from global path=%s", globalMapPath)
  52. }
  53. if !strings.Contains(vID, "vol-") {
  54. return nil, fmt.Errorf("failed to get volumeID from global path=%s, invalid volumeID format = %s", globalMapPath, vID)
  55. }
  56. block := v1.PersistentVolumeBlock
  57. awsVolume := &v1.PersistentVolume{
  58. Spec: v1.PersistentVolumeSpec{
  59. PersistentVolumeSource: v1.PersistentVolumeSource{
  60. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
  61. VolumeID: vID,
  62. },
  63. },
  64. VolumeMode: &block,
  65. },
  66. }
  67. return volume.NewSpecFromPersistentVolume(awsVolume, true), nil
  68. }
  69. // NewBlockVolumeMapper creates a new volume.BlockVolumeMapper from an API specification.
  70. func (plugin *awsElasticBlockStorePlugin) 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, &AWSDiskUtil{}, plugin.host.GetMounter(plugin.GetPluginName()))
  78. }
  79. func (plugin *awsElasticBlockStorePlugin) newBlockVolumeMapperInternal(spec *volume.Spec, podUID types.UID, manager ebsManager, mounter mount.Interface) (volume.BlockVolumeMapper, error) {
  80. ebs, readOnly, err := getVolumeSource(spec)
  81. if err != nil {
  82. return nil, err
  83. }
  84. volumeID := aws.KubernetesVolumeID(ebs.VolumeID)
  85. partition := ""
  86. if ebs.Partition != 0 {
  87. partition = strconv.Itoa(int(ebs.Partition))
  88. }
  89. return &awsElasticBlockStoreMapper{
  90. awsElasticBlockStore: &awsElasticBlockStore{
  91. podUID: podUID,
  92. volName: spec.Name(),
  93. volumeID: volumeID,
  94. partition: partition,
  95. manager: manager,
  96. mounter: mounter,
  97. plugin: plugin,
  98. },
  99. readOnly: readOnly}, nil
  100. }
  101. func (plugin *awsElasticBlockStorePlugin) NewBlockVolumeUnmapper(volName string, podUID types.UID) (volume.BlockVolumeUnmapper, error) {
  102. return plugin.newUnmapperInternal(volName, podUID, &AWSDiskUtil{}, plugin.host.GetMounter(plugin.GetPluginName()))
  103. }
  104. func (plugin *awsElasticBlockStorePlugin) newUnmapperInternal(volName string, podUID types.UID, manager ebsManager, mounter mount.Interface) (volume.BlockVolumeUnmapper, error) {
  105. return &awsElasticBlockStoreUnmapper{
  106. awsElasticBlockStore: &awsElasticBlockStore{
  107. podUID: podUID,
  108. volName: volName,
  109. manager: manager,
  110. mounter: mounter,
  111. plugin: plugin,
  112. }}, nil
  113. }
  114. func (c *awsElasticBlockStoreUnmapper) TearDownDevice(mapPath, devicePath string) error {
  115. return nil
  116. }
  117. type awsElasticBlockStoreUnmapper struct {
  118. *awsElasticBlockStore
  119. }
  120. var _ volume.BlockVolumeUnmapper = &awsElasticBlockStoreUnmapper{}
  121. type awsElasticBlockStoreMapper struct {
  122. *awsElasticBlockStore
  123. readOnly bool
  124. }
  125. var _ volume.BlockVolumeMapper = &awsElasticBlockStoreMapper{}
  126. func (b *awsElasticBlockStoreMapper) SetUpDevice() (string, error) {
  127. return "", nil
  128. }
  129. func (b *awsElasticBlockStoreMapper) 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/volumeID
  134. // plugins/kubernetes.io/aws-ebs/volumeDevices/vol-XXXXXX
  135. func (ebs *awsElasticBlockStore) GetGlobalMapPath(spec *volume.Spec) (string, error) {
  136. volumeSource, _, err := getVolumeSource(spec)
  137. if err != nil {
  138. return "", err
  139. }
  140. return filepath.Join(ebs.plugin.host.GetVolumeDevicePluginDir(awsElasticBlockStorePluginName), string(volumeSource.VolumeID)), nil
  141. }
  142. // GetPodDeviceMapPath returns pod device map path and volume name
  143. // path: pods/{podUid}/volumeDevices/kubernetes.io~aws
  144. func (ebs *awsElasticBlockStore) GetPodDeviceMapPath() (string, string) {
  145. name := awsElasticBlockStorePluginName
  146. return ebs.plugin.host.GetPodVolumeDeviceDir(ebs.podUID, utilstrings.EscapeQualifiedName(name)), ebs.volName
  147. }