azure_provision.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_file
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2018-07-01/storage"
  18. v1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. cloudprovider "k8s.io/cloud-provider"
  22. volumehelpers "k8s.io/cloud-provider/volume/helpers"
  23. "k8s.io/klog"
  24. "k8s.io/kubernetes/pkg/volume"
  25. "k8s.io/kubernetes/pkg/volume/util"
  26. "k8s.io/legacy-cloud-providers/azure"
  27. utilstrings "k8s.io/utils/strings"
  28. )
  29. var _ volume.DeletableVolumePlugin = &azureFilePlugin{}
  30. var _ volume.ProvisionableVolumePlugin = &azureFilePlugin{}
  31. // Abstract interface to file share operations.
  32. // azure cloud provider should implement it
  33. type azureCloudProvider interface {
  34. // create a file share
  35. CreateFileShare(shareName, accountName, accountType, accountKind, resourceGroup, location string, requestGiB int) (string, string, error)
  36. // delete a file share
  37. DeleteFileShare(accountName, accountKey, shareName string) error
  38. // resize a file share
  39. ResizeFileShare(accountName, accountKey, name string, sizeGiB int) error
  40. }
  41. type azureFileDeleter struct {
  42. *azureFile
  43. accountName, accountKey, shareName string
  44. azureProvider azureCloudProvider
  45. }
  46. func (plugin *azureFilePlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  47. azure, err := getAzureCloudProvider(plugin.host.GetCloudProvider())
  48. if err != nil {
  49. klog.V(4).Infof("failed to get azure provider")
  50. return nil, err
  51. }
  52. return plugin.newDeleterInternal(spec, &azureSvc{}, azure)
  53. }
  54. func (plugin *azureFilePlugin) newDeleterInternal(spec *volume.Spec, util azureUtil, azure azureCloudProvider) (volume.Deleter, error) {
  55. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.AzureFile == nil {
  56. return nil, fmt.Errorf("invalid PV spec")
  57. }
  58. secretName, secretNamespace, err := getSecretNameAndNamespace(spec, spec.PersistentVolume.Spec.ClaimRef.Namespace)
  59. if err != nil {
  60. return nil, err
  61. }
  62. shareName := spec.PersistentVolume.Spec.AzureFile.ShareName
  63. if accountName, accountKey, err := util.GetAzureCredentials(plugin.host, secretNamespace, secretName); err != nil {
  64. return nil, err
  65. } else {
  66. return &azureFileDeleter{
  67. azureFile: &azureFile{
  68. volName: spec.Name(),
  69. plugin: plugin,
  70. },
  71. shareName: shareName,
  72. accountName: accountName,
  73. accountKey: accountKey,
  74. azureProvider: azure,
  75. }, nil
  76. }
  77. }
  78. func (plugin *azureFilePlugin) NewProvisioner(options volume.VolumeOptions) (volume.Provisioner, error) {
  79. azure, err := getAzureCloudProvider(plugin.host.GetCloudProvider())
  80. if err != nil {
  81. klog.V(4).Infof("failed to get azure provider")
  82. return nil, err
  83. }
  84. if len(options.PVC.Spec.AccessModes) == 0 {
  85. options.PVC.Spec.AccessModes = plugin.GetAccessModes()
  86. }
  87. return plugin.newProvisionerInternal(options, azure)
  88. }
  89. func (plugin *azureFilePlugin) newProvisionerInternal(options volume.VolumeOptions, azure azureCloudProvider) (volume.Provisioner, error) {
  90. return &azureFileProvisioner{
  91. azureFile: &azureFile{
  92. plugin: plugin,
  93. },
  94. azureProvider: azure,
  95. util: &azureSvc{},
  96. options: options,
  97. }, nil
  98. }
  99. var _ volume.Deleter = &azureFileDeleter{}
  100. func (f *azureFileDeleter) GetPath() string {
  101. name := azureFilePluginName
  102. return f.plugin.host.GetPodVolumeDir(f.podUID, utilstrings.EscapeQualifiedName(name), f.volName)
  103. }
  104. func (f *azureFileDeleter) Delete() error {
  105. klog.V(4).Infof("deleting volume %s", f.shareName)
  106. return f.azureProvider.DeleteFileShare(f.accountName, f.accountKey, f.shareName)
  107. }
  108. type azureFileProvisioner struct {
  109. *azureFile
  110. azureProvider azureCloudProvider
  111. util azureUtil
  112. options volume.VolumeOptions
  113. }
  114. var _ volume.Provisioner = &azureFileProvisioner{}
  115. func (a *azureFileProvisioner) Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error) {
  116. if !util.AccessModesContainedInAll(a.plugin.GetAccessModes(), a.options.PVC.Spec.AccessModes) {
  117. return nil, fmt.Errorf("invalid AccessModes %v: only AccessModes %v are supported", a.options.PVC.Spec.AccessModes, a.plugin.GetAccessModes())
  118. }
  119. if util.CheckPersistentVolumeClaimModeBlock(a.options.PVC) {
  120. return nil, fmt.Errorf("%s does not support block volume provisioning", a.plugin.GetPluginName())
  121. }
  122. var sku, resourceGroup, location, account, shareName string
  123. capacity := a.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
  124. requestGiB := int(volumehelpers.RoundUpToGiB(capacity))
  125. secretNamespace := a.options.PVC.Namespace
  126. // Apply ProvisionerParameters (case-insensitive). We leave validation of
  127. // the values to the cloud provider.
  128. for k, v := range a.options.Parameters {
  129. switch strings.ToLower(k) {
  130. case "skuname":
  131. sku = v
  132. case "location":
  133. location = v
  134. case "storageaccount":
  135. account = v
  136. case "secretnamespace":
  137. secretNamespace = v
  138. case "resourcegroup":
  139. resourceGroup = v
  140. case "sharename":
  141. shareName = v
  142. default:
  143. return nil, fmt.Errorf("invalid option %q for volume plugin %s", k, a.plugin.GetPluginName())
  144. }
  145. }
  146. // TODO: implement c.options.ProvisionerSelector parsing
  147. if a.options.PVC.Spec.Selector != nil {
  148. return nil, fmt.Errorf("claim.Spec.Selector is not supported for dynamic provisioning on Azure file")
  149. }
  150. if shareName == "" {
  151. // File share name has a length limit of 63, and it cannot contain two consecutive '-'s.
  152. name := util.GenerateVolumeName(a.options.ClusterName, a.options.PVName, 63)
  153. shareName = strings.Replace(name, "--", "-", -1)
  154. }
  155. // when use azure file premium, account kind should be specified as FileStorage
  156. accountKind := string(storage.StorageV2)
  157. if strings.HasPrefix(strings.ToLower(sku), "premium") {
  158. accountKind = string(storage.FileStorage)
  159. }
  160. account, key, err := a.azureProvider.CreateFileShare(shareName, account, sku, accountKind, resourceGroup, location, requestGiB)
  161. if err != nil {
  162. return nil, err
  163. }
  164. // create a secret for storage account and key
  165. secretName, err := a.util.SetAzureCredentials(a.plugin.host, secretNamespace, account, key)
  166. if err != nil {
  167. return nil, err
  168. }
  169. // create PV
  170. pv := &v1.PersistentVolume{
  171. ObjectMeta: metav1.ObjectMeta{
  172. Name: a.options.PVName,
  173. Labels: map[string]string{},
  174. Annotations: map[string]string{
  175. util.VolumeDynamicallyCreatedByKey: "azure-file-dynamic-provisioner",
  176. },
  177. },
  178. Spec: v1.PersistentVolumeSpec{
  179. PersistentVolumeReclaimPolicy: a.options.PersistentVolumeReclaimPolicy,
  180. AccessModes: a.options.PVC.Spec.AccessModes,
  181. Capacity: v1.ResourceList{
  182. v1.ResourceName(v1.ResourceStorage): resource.MustParse(fmt.Sprintf("%dGi", requestGiB)),
  183. },
  184. PersistentVolumeSource: v1.PersistentVolumeSource{
  185. AzureFile: &v1.AzureFilePersistentVolumeSource{
  186. SecretName: secretName,
  187. ShareName: shareName,
  188. SecretNamespace: &secretNamespace,
  189. },
  190. },
  191. MountOptions: a.options.MountOptions,
  192. },
  193. }
  194. return pv, nil
  195. }
  196. // Return cloud provider
  197. func getAzureCloudProvider(cloudProvider cloudprovider.Interface) (azureCloudProvider, error) {
  198. azureCloudProvider, ok := cloudProvider.(*azure.Cloud)
  199. if !ok || azureCloudProvider == nil {
  200. return nil, fmt.Errorf("Failed to get Azure Cloud Provider. GetCloudProvider returned %v instead", cloudProvider)
  201. }
  202. return azureCloudProvider, nil
  203. }