azure_common.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright 2017 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 azure_dd
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "regexp"
  20. libstrings "strings"
  21. "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
  22. v1 "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/util/sets"
  25. api "k8s.io/kubernetes/pkg/apis/core"
  26. "k8s.io/kubernetes/pkg/volume"
  27. "k8s.io/kubernetes/pkg/volume/util"
  28. "k8s.io/legacy-cloud-providers/azure"
  29. utilstrings "k8s.io/utils/strings"
  30. )
  31. const (
  32. defaultStorageAccountType = compute.StandardLRS
  33. defaultAzureDiskKind = v1.AzureManagedDisk
  34. defaultAzureDataDiskCachingMode = v1.AzureDataDiskCachingReadOnly
  35. )
  36. type dataDisk struct {
  37. volume.MetricsProvider
  38. volumeName string
  39. diskName string
  40. podUID types.UID
  41. plugin *azureDataDiskPlugin
  42. }
  43. var (
  44. supportedCachingModes = sets.NewString(
  45. string(api.AzureDataDiskCachingNone),
  46. string(api.AzureDataDiskCachingReadOnly),
  47. string(api.AzureDataDiskCachingReadWrite))
  48. supportedDiskKinds = sets.NewString(
  49. string(api.AzureSharedBlobDisk),
  50. string(api.AzureDedicatedBlobDisk),
  51. string(api.AzureManagedDisk))
  52. // only for Windows node
  53. winDiskNumRE = regexp.MustCompile(`/dev/disk(.+)`)
  54. winDiskNumFormat = "/dev/disk%d"
  55. )
  56. func getPath(uid types.UID, volName string, host volume.VolumeHost) string {
  57. return host.GetPodVolumeDir(uid, utilstrings.EscapeQualifiedName(azureDataDiskPluginName), volName)
  58. }
  59. // creates a unique path for disks (even if they share the same *.vhd name)
  60. func makeGlobalPDPath(host volume.VolumeHost, diskUri string, isManaged bool) (string, error) {
  61. diskUri = libstrings.ToLower(diskUri) // always lower uri because users may enter it in caps.
  62. uniqueDiskNameTemplate := "%s%s"
  63. hashedDiskUri := azure.MakeCRC32(diskUri)
  64. prefix := "b"
  65. if isManaged {
  66. prefix = "m"
  67. }
  68. // "{m for managed b for blob}{hashed diskUri or DiskId depending on disk kind }"
  69. diskName := fmt.Sprintf(uniqueDiskNameTemplate, prefix, hashedDiskUri)
  70. pdPath := filepath.Join(host.GetPluginDir(azureDataDiskPluginName), util.MountsInGlobalPDPath, diskName)
  71. return pdPath, nil
  72. }
  73. func makeDataDisk(volumeName string, podUID types.UID, diskName string, host volume.VolumeHost, plugin *azureDataDiskPlugin) *dataDisk {
  74. var metricProvider volume.MetricsProvider
  75. if podUID != "" {
  76. metricProvider = volume.NewMetricsStatFS(getPath(podUID, volumeName, host))
  77. }
  78. return &dataDisk{
  79. MetricsProvider: metricProvider,
  80. volumeName: volumeName,
  81. diskName: diskName,
  82. podUID: podUID,
  83. plugin: plugin,
  84. }
  85. }
  86. func getVolumeSource(spec *volume.Spec) (volumeSource *v1.AzureDiskVolumeSource, readOnly bool, err error) {
  87. if spec.Volume != nil && spec.Volume.AzureDisk != nil {
  88. return spec.Volume.AzureDisk, spec.Volume.AzureDisk.ReadOnly != nil && *spec.Volume.AzureDisk.ReadOnly, nil
  89. }
  90. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.AzureDisk != nil {
  91. return spec.PersistentVolume.Spec.AzureDisk, spec.ReadOnly, nil
  92. }
  93. return nil, false, fmt.Errorf("azureDisk - Spec does not reference an Azure disk volume type")
  94. }
  95. func normalizeKind(kind string) (v1.AzureDataDiskKind, error) {
  96. if kind == "" {
  97. return defaultAzureDiskKind, nil
  98. }
  99. if !supportedDiskKinds.Has(kind) {
  100. return "", fmt.Errorf("azureDisk - %s is not supported disk kind. Supported values are %s", kind, supportedDiskKinds.List())
  101. }
  102. return v1.AzureDataDiskKind(kind), nil
  103. }
  104. func normalizeStorageAccountType(storageAccountType string) (compute.DiskStorageAccountTypes, error) {
  105. if storageAccountType == "" {
  106. return defaultStorageAccountType, nil
  107. }
  108. sku := compute.DiskStorageAccountTypes(storageAccountType)
  109. supportedSkuNames := compute.PossibleDiskStorageAccountTypesValues()
  110. for _, s := range supportedSkuNames {
  111. if sku == s {
  112. return sku, nil
  113. }
  114. }
  115. return "", fmt.Errorf("azureDisk - %s is not supported sku/storageaccounttype. Supported values are %s", storageAccountType, supportedSkuNames)
  116. }
  117. func normalizeCachingMode(cachingMode v1.AzureDataDiskCachingMode) (v1.AzureDataDiskCachingMode, error) {
  118. if cachingMode == "" {
  119. return defaultAzureDataDiskCachingMode, nil
  120. }
  121. if !supportedCachingModes.Has(string(cachingMode)) {
  122. return "", fmt.Errorf("azureDisk - %s is not supported cachingmode. Supported values are %s", cachingMode, supportedCachingModes.List())
  123. }
  124. return cachingMode, nil
  125. }
  126. type ioHandler interface {
  127. ReadDir(dirname string) ([]os.FileInfo, error)
  128. WriteFile(filename string, data []byte, perm os.FileMode) error
  129. Readlink(name string) (string, error)
  130. ReadFile(filename string) ([]byte, error)
  131. }
  132. //TODO: check if priming the iscsi interface is actually needed
  133. type osIOHandler struct{}
  134. func (handler *osIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
  135. return ioutil.ReadDir(dirname)
  136. }
  137. func (handler *osIOHandler) WriteFile(filename string, data []byte, perm os.FileMode) error {
  138. return ioutil.WriteFile(filename, data, perm)
  139. }
  140. func (handler *osIOHandler) Readlink(name string) (string, error) {
  141. return os.Readlink(name)
  142. }
  143. func (handler *osIOHandler) ReadFile(filename string) ([]byte, error) {
  144. return ioutil.ReadFile(filename)
  145. }
  146. func getDiskController(host volume.VolumeHost) (DiskController, error) {
  147. cloudProvider := host.GetCloudProvider()
  148. az, ok := cloudProvider.(*azure.Cloud)
  149. if !ok || az == nil {
  150. return nil, fmt.Errorf("AzureDisk - failed to get Azure Cloud Provider. GetCloudProvider returned %v instead", cloudProvider)
  151. }
  152. return az, nil
  153. }
  154. func getCloud(host volume.VolumeHost) (*azure.Cloud, error) {
  155. cloudProvider := host.GetCloudProvider()
  156. az, ok := cloudProvider.(*azure.Cloud)
  157. if !ok || az == nil {
  158. return nil, fmt.Errorf("AzureDisk - failed to get Azure Cloud Provider. GetCloudProvider returned %v instead", cloudProvider)
  159. }
  160. return az, nil
  161. }
  162. func strFirstLetterToUpper(str string) string {
  163. if len(str) < 2 {
  164. return str
  165. }
  166. return libstrings.ToUpper(string(str[0])) + str[1:]
  167. }
  168. // getDiskNum : extract the disk num from a device path,
  169. // deviceInfo format could be like this: e.g. /dev/disk2
  170. func getDiskNum(deviceInfo string) (string, error) {
  171. matches := winDiskNumRE.FindStringSubmatch(deviceInfo)
  172. if len(matches) == 2 {
  173. return matches[1], nil
  174. }
  175. return "", fmt.Errorf("cannot parse deviceInfo: %s, correct format: /dev/disk?", deviceInfo)
  176. }