azure_common.go 6.7 KB

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