azure_util.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 azure_file
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. v1 "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/kubernetes/pkg/volume"
  23. )
  24. const (
  25. fileMode = "file_mode"
  26. dirMode = "dir_mode"
  27. gid = "gid"
  28. vers = "vers"
  29. defaultFileMode = "0777"
  30. defaultDirMode = "0777"
  31. defaultVers = "3.0"
  32. )
  33. // Abstract interface to azure file operations.
  34. type azureUtil interface {
  35. GetAzureCredentials(host volume.VolumeHost, nameSpace, secretName string) (string, string, error)
  36. SetAzureCredentials(host volume.VolumeHost, nameSpace, accountName, accountKey string) (string, error)
  37. }
  38. type azureSvc struct{}
  39. func (s *azureSvc) GetAzureCredentials(host volume.VolumeHost, nameSpace, secretName string) (string, string, error) {
  40. var accountKey, accountName string
  41. kubeClient := host.GetKubeClient()
  42. if kubeClient == nil {
  43. return "", "", fmt.Errorf("Cannot get kube client")
  44. }
  45. keys, err := kubeClient.CoreV1().Secrets(nameSpace).Get(context.TODO(), secretName, metav1.GetOptions{})
  46. if err != nil {
  47. return "", "", fmt.Errorf("Couldn't get secret %v/%v", nameSpace, secretName)
  48. }
  49. for name, data := range keys.Data {
  50. if name == "azurestorageaccountname" {
  51. accountName = string(data)
  52. }
  53. if name == "azurestorageaccountkey" {
  54. accountKey = string(data)
  55. }
  56. }
  57. if accountName == "" || accountKey == "" {
  58. return "", "", fmt.Errorf("Invalid %v/%v, couldn't extract azurestorageaccountname or azurestorageaccountkey", nameSpace, secretName)
  59. }
  60. accountName = strings.TrimSpace(accountName)
  61. return accountName, accountKey, nil
  62. }
  63. func (s *azureSvc) SetAzureCredentials(host volume.VolumeHost, nameSpace, accountName, accountKey string) (string, error) {
  64. kubeClient := host.GetKubeClient()
  65. if kubeClient == nil {
  66. return "", fmt.Errorf("Cannot get kube client")
  67. }
  68. secretName := "azure-storage-account-" + accountName + "-secret"
  69. secret := &v1.Secret{
  70. ObjectMeta: metav1.ObjectMeta{
  71. Namespace: nameSpace,
  72. Name: secretName,
  73. },
  74. Data: map[string][]byte{
  75. "azurestorageaccountname": []byte(accountName),
  76. "azurestorageaccountkey": []byte(accountKey),
  77. },
  78. Type: "Opaque",
  79. }
  80. _, err := kubeClient.CoreV1().Secrets(nameSpace).Create(context.TODO(), secret, metav1.CreateOptions{})
  81. if errors.IsAlreadyExists(err) {
  82. err = nil
  83. }
  84. if err != nil {
  85. return "", fmt.Errorf("Couldn't create secret %v", err)
  86. }
  87. return secretName, err
  88. }
  89. // check whether mountOptions contain file_mode, dir_mode, vers, gid, if not, append default mode
  90. func appendDefaultMountOptions(mountOptions []string, fsGroup *int64) []string {
  91. fileModeFlag := false
  92. dirModeFlag := false
  93. versFlag := false
  94. gidFlag := false
  95. for _, mountOption := range mountOptions {
  96. if strings.HasPrefix(mountOption, fileMode) {
  97. fileModeFlag = true
  98. }
  99. if strings.HasPrefix(mountOption, dirMode) {
  100. dirModeFlag = true
  101. }
  102. if strings.HasPrefix(mountOption, vers) {
  103. versFlag = true
  104. }
  105. if strings.HasPrefix(mountOption, gid) {
  106. gidFlag = true
  107. }
  108. }
  109. allMountOptions := mountOptions
  110. if !fileModeFlag {
  111. allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", fileMode, defaultFileMode))
  112. }
  113. if !dirModeFlag {
  114. allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", dirMode, defaultDirMode))
  115. }
  116. if !versFlag {
  117. allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", vers, defaultVers))
  118. }
  119. if !gidFlag && fsGroup != nil {
  120. allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%d", gid, *fsGroup))
  121. }
  122. return allMountOptions
  123. }