vsphere_volume_util.go 9.7 KB

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