vsphere_volume_util.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. Copyright 2016 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 vsphere_volume
  14. import (
  15. "errors"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "k8s.io/api/core/v1"
  21. cloudprovider "k8s.io/cloud-provider"
  22. volumehelpers "k8s.io/cloud-provider/volume/helpers"
  23. "k8s.io/klog"
  24. "k8s.io/kubernetes/pkg/util/mount"
  25. "k8s.io/kubernetes/pkg/volume"
  26. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  27. "k8s.io/legacy-cloud-providers/vsphere"
  28. "k8s.io/legacy-cloud-providers/vsphere/vclib"
  29. )
  30. const (
  31. maxRetries = 10
  32. checkSleepDuration = time.Second
  33. diskByIDPath = "/dev/disk/by-id/"
  34. diskSCSIPrefix = "wwn-0x"
  35. diskformat = "diskformat"
  36. datastore = "datastore"
  37. StoragePolicyName = "storagepolicyname"
  38. HostFailuresToTolerateCapability = "hostfailurestotolerate"
  39. ForceProvisioningCapability = "forceprovisioning"
  40. CacheReservationCapability = "cachereservation"
  41. DiskStripesCapability = "diskstripes"
  42. ObjectSpaceReservationCapability = "objectspacereservation"
  43. IopsLimitCapability = "iopslimit"
  44. HostFailuresToTolerateCapabilityMin = 0
  45. HostFailuresToTolerateCapabilityMax = 3
  46. ForceProvisioningCapabilityMin = 0
  47. ForceProvisioningCapabilityMax = 1
  48. CacheReservationCapabilityMin = 0
  49. CacheReservationCapabilityMax = 100
  50. DiskStripesCapabilityMin = 1
  51. DiskStripesCapabilityMax = 12
  52. ObjectSpaceReservationCapabilityMin = 0
  53. ObjectSpaceReservationCapabilityMax = 100
  54. IopsLimitCapabilityMin = 0
  55. )
  56. var ErrProbeVolume = errors.New("Error scanning attached volumes")
  57. type VsphereDiskUtil struct{}
  58. type VolumeSpec struct {
  59. Path string
  60. Size int
  61. Fstype string
  62. StoragePolicyID string
  63. StoragePolicyName string
  64. Labels map[string]string
  65. }
  66. func verifyDevicePath(path string) (string, error) {
  67. if pathExists, err := mount.PathExists(path); err != nil {
  68. return "", fmt.Errorf("Error checking if path exists: %v", err)
  69. } else if pathExists {
  70. return path, nil
  71. }
  72. return "", nil
  73. }
  74. // CreateVolume creates a vSphere volume.
  75. func (util *VsphereDiskUtil) CreateVolume(v *vsphereVolumeProvisioner, selectedZone []string) (volSpec *VolumeSpec, err error) {
  76. var fstype string
  77. cloud, err := getCloudProvider(v.plugin.host.GetCloudProvider())
  78. if err != nil {
  79. return nil, err
  80. }
  81. capacity := v.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
  82. // vSphere works with KiB, but its minimum allocation unit is 1 MiB
  83. volSizeMiB, err := volumehelpers.RoundUpToMiBInt(capacity)
  84. if err != nil {
  85. return nil, err
  86. }
  87. volSizeKiB := volSizeMiB * 1024
  88. name := volumeutil.GenerateVolumeName(v.options.ClusterName, v.options.PVName, 255)
  89. volumeOptions := &vclib.VolumeOptions{
  90. CapacityKB: volSizeKiB,
  91. Tags: *v.options.CloudTags,
  92. Name: name,
  93. }
  94. volumeOptions.Zone = selectedZone
  95. // Apply Parameters (case-insensitive). We leave validation of
  96. // the values to the cloud provider.
  97. for parameter, value := range v.options.Parameters {
  98. switch strings.ToLower(parameter) {
  99. case diskformat:
  100. volumeOptions.DiskFormat = value
  101. case datastore:
  102. volumeOptions.Datastore = value
  103. case volume.VolumeParameterFSType:
  104. fstype = value
  105. klog.V(4).Infof("Setting fstype as %q", fstype)
  106. case StoragePolicyName:
  107. volumeOptions.StoragePolicyName = value
  108. klog.V(4).Infof("Setting StoragePolicyName as %q", volumeOptions.StoragePolicyName)
  109. case HostFailuresToTolerateCapability, ForceProvisioningCapability,
  110. CacheReservationCapability, DiskStripesCapability,
  111. ObjectSpaceReservationCapability, IopsLimitCapability:
  112. capabilityData, err := validateVSANCapability(strings.ToLower(parameter), value)
  113. if err != nil {
  114. return nil, err
  115. }
  116. volumeOptions.VSANStorageProfileData += capabilityData
  117. default:
  118. return nil, fmt.Errorf("invalid option %q for volume plugin %s", parameter, v.plugin.GetPluginName())
  119. }
  120. }
  121. if volumeOptions.VSANStorageProfileData != "" {
  122. if volumeOptions.StoragePolicyName != "" {
  123. return nil, fmt.Errorf("Cannot specify storage policy capabilities along with storage policy name. Please specify only one")
  124. }
  125. volumeOptions.VSANStorageProfileData = "(" + volumeOptions.VSANStorageProfileData + ")"
  126. }
  127. klog.V(4).Infof("VSANStorageProfileData in vsphere volume %q", volumeOptions.VSANStorageProfileData)
  128. // TODO: implement PVC.Selector parsing
  129. if v.options.PVC.Spec.Selector != nil {
  130. return nil, fmt.Errorf("claim.Spec.Selector is not supported for dynamic provisioning on vSphere")
  131. }
  132. vmDiskPath, err := cloud.CreateVolume(volumeOptions)
  133. if err != nil {
  134. return nil, err
  135. }
  136. labels, err := cloud.GetVolumeLabels(vmDiskPath)
  137. if err != nil {
  138. return nil, err
  139. }
  140. volSpec = &VolumeSpec{
  141. Path: vmDiskPath,
  142. Size: volSizeKiB,
  143. Fstype: fstype,
  144. StoragePolicyName: volumeOptions.StoragePolicyName,
  145. StoragePolicyID: volumeOptions.StoragePolicyID,
  146. Labels: labels,
  147. }
  148. klog.V(2).Infof("Successfully created vsphere volume %s", name)
  149. return volSpec, nil
  150. }
  151. // DeleteVolume deletes a vSphere volume.
  152. func (util *VsphereDiskUtil) DeleteVolume(vd *vsphereVolumeDeleter) error {
  153. cloud, err := getCloudProvider(vd.plugin.host.GetCloudProvider())
  154. if err != nil {
  155. return err
  156. }
  157. if err = cloud.DeleteVolume(vd.volPath); err != nil {
  158. klog.V(2).Infof("Error deleting vsphere volume %s: %v", vd.volPath, err)
  159. return err
  160. }
  161. klog.V(2).Infof("Successfully deleted vsphere volume %s", vd.volPath)
  162. return nil
  163. }
  164. func getVolPathfromVolumeName(deviceMountPath string) string {
  165. // Assumption: No file or folder is named starting with '[' in datastore
  166. volPath := deviceMountPath[strings.LastIndex(deviceMountPath, "["):]
  167. // space between datastore and vmdk name in volumePath is encoded as '\040' when returned by GetMountRefs().
  168. // volumePath eg: "[local] xxx.vmdk" provided to attach/mount
  169. // replacing \040 with space to match the actual volumePath
  170. return strings.Replace(volPath, "\\040", " ", -1)
  171. }
  172. func getCloudProvider(cloud cloudprovider.Interface) (*vsphere.VSphere, error) {
  173. if cloud == nil {
  174. klog.Errorf("Cloud provider not initialized properly")
  175. return nil, errors.New("Cloud provider not initialized properly")
  176. }
  177. vs, ok := cloud.(*vsphere.VSphere)
  178. if !ok || vs == nil {
  179. return nil, errors.New("Invalid cloud provider: expected vSphere")
  180. }
  181. return vs, nil
  182. }
  183. // Validate the capability requirement for the user specified policy attributes.
  184. func validateVSANCapability(capabilityName string, capabilityValue string) (string, error) {
  185. var capabilityData string
  186. capabilityIntVal, ok := verifyCapabilityValueIsInteger(capabilityValue)
  187. if !ok {
  188. return "", fmt.Errorf("Invalid value for %s. The capabilityValue: %s must be a valid integer value", capabilityName, capabilityValue)
  189. }
  190. switch strings.ToLower(capabilityName) {
  191. case HostFailuresToTolerateCapability:
  192. if capabilityIntVal >= HostFailuresToTolerateCapabilityMin && capabilityIntVal <= HostFailuresToTolerateCapabilityMax {
  193. capabilityData = " (\"hostFailuresToTolerate\" i" + capabilityValue + ")"
  194. } else {
  195. return "", fmt.Errorf(`Invalid value for hostFailuresToTolerate.
  196. The default value is %d, minimum value is %d and maximum value is %d.`,
  197. 1, HostFailuresToTolerateCapabilityMin, HostFailuresToTolerateCapabilityMax)
  198. }
  199. case ForceProvisioningCapability:
  200. if capabilityIntVal >= ForceProvisioningCapabilityMin && capabilityIntVal <= ForceProvisioningCapabilityMax {
  201. capabilityData = " (\"forceProvisioning\" i" + capabilityValue + ")"
  202. } else {
  203. return "", fmt.Errorf(`Invalid value for forceProvisioning.
  204. The value can be either %d or %d.`,
  205. ForceProvisioningCapabilityMin, ForceProvisioningCapabilityMax)
  206. }
  207. case CacheReservationCapability:
  208. if capabilityIntVal >= CacheReservationCapabilityMin && capabilityIntVal <= CacheReservationCapabilityMax {
  209. capabilityData = " (\"cacheReservation\" i" + strconv.Itoa(capabilityIntVal*10000) + ")"
  210. } else {
  211. return "", fmt.Errorf(`Invalid value for cacheReservation.
  212. The minimum percentage is %d and maximum percentage is %d.`,
  213. CacheReservationCapabilityMin, CacheReservationCapabilityMax)
  214. }
  215. case DiskStripesCapability:
  216. if capabilityIntVal >= DiskStripesCapabilityMin && capabilityIntVal <= DiskStripesCapabilityMax {
  217. capabilityData = " (\"stripeWidth\" i" + capabilityValue + ")"
  218. } else {
  219. return "", fmt.Errorf(`Invalid value for diskStripes.
  220. The minimum value is %d and maximum value is %d.`,
  221. DiskStripesCapabilityMin, DiskStripesCapabilityMax)
  222. }
  223. case ObjectSpaceReservationCapability:
  224. if capabilityIntVal >= ObjectSpaceReservationCapabilityMin && capabilityIntVal <= ObjectSpaceReservationCapabilityMax {
  225. capabilityData = " (\"proportionalCapacity\" i" + capabilityValue + ")"
  226. } else {
  227. return "", fmt.Errorf(`Invalid value for ObjectSpaceReservation.
  228. The minimum percentage is %d and maximum percentage is %d.`,
  229. ObjectSpaceReservationCapabilityMin, ObjectSpaceReservationCapabilityMax)
  230. }
  231. case IopsLimitCapability:
  232. if capabilityIntVal >= IopsLimitCapabilityMin {
  233. capabilityData = " (\"iopsLimit\" i" + capabilityValue + ")"
  234. } else {
  235. return "", fmt.Errorf(`Invalid value for iopsLimit.
  236. The value should be greater than %d.`, IopsLimitCapabilityMin)
  237. }
  238. }
  239. return capabilityData, nil
  240. }
  241. // Verify if the capability value is of type integer.
  242. func verifyCapabilityValueIsInteger(capabilityValue string) (int, bool) {
  243. i, err := strconv.Atoi(capabilityValue)
  244. if err != nil {
  245. return -1, false
  246. }
  247. return i, true
  248. }