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