azure_provision.go 7.7 KB

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