azure_util.go 3.9 KB

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