csi_util.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright 2018 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 csi
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "time"
  20. api "k8s.io/api/core/v1"
  21. meta "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. utilfeature "k8s.io/apiserver/pkg/util/feature"
  23. "k8s.io/client-go/kubernetes"
  24. "k8s.io/klog"
  25. "k8s.io/kubernetes/pkg/features"
  26. "k8s.io/kubernetes/pkg/volume"
  27. utilstrings "k8s.io/utils/strings"
  28. )
  29. const (
  30. testInformerSyncPeriod = 100 * time.Millisecond
  31. testInformerSyncTimeout = 30 * time.Second
  32. )
  33. func getCredentialsFromSecret(k8s kubernetes.Interface, secretRef *api.SecretReference) (map[string]string, error) {
  34. credentials := map[string]string{}
  35. secret, err := k8s.CoreV1().Secrets(secretRef.Namespace).Get(secretRef.Name, meta.GetOptions{})
  36. if err != nil {
  37. klog.Errorf("failed to find the secret %s in the namespace %s with error: %v\n", secretRef.Name, secretRef.Namespace, err)
  38. return credentials, err
  39. }
  40. for key, value := range secret.Data {
  41. credentials[key] = string(value)
  42. }
  43. return credentials, nil
  44. }
  45. // saveVolumeData persists parameter data as json file at the provided location
  46. func saveVolumeData(dir string, fileName string, data map[string]string) error {
  47. dataFilePath := filepath.Join(dir, fileName)
  48. klog.V(4).Info(log("saving volume data file [%s]", dataFilePath))
  49. file, err := os.Create(dataFilePath)
  50. if err != nil {
  51. klog.Error(log("failed to save volume data file %s: %v", dataFilePath, err))
  52. return err
  53. }
  54. defer file.Close()
  55. if err := json.NewEncoder(file).Encode(data); err != nil {
  56. klog.Error(log("failed to save volume data file %s: %v", dataFilePath, err))
  57. return err
  58. }
  59. klog.V(4).Info(log("volume data file saved successfully [%s]", dataFilePath))
  60. return nil
  61. }
  62. // loadVolumeData loads volume info from specified json file/location
  63. func loadVolumeData(dir string, fileName string) (map[string]string, error) {
  64. // remove /mount at the end
  65. dataFileName := filepath.Join(dir, fileName)
  66. klog.V(4).Info(log("loading volume data file [%s]", dataFileName))
  67. file, err := os.Open(dataFileName)
  68. if err != nil {
  69. klog.Error(log("failed to open volume data file [%s]: %v", dataFileName, err))
  70. return nil, err
  71. }
  72. defer file.Close()
  73. data := map[string]string{}
  74. if err := json.NewDecoder(file).Decode(&data); err != nil {
  75. klog.Error(log("failed to parse volume data file [%s]: %v", dataFileName, err))
  76. return nil, err
  77. }
  78. return data, nil
  79. }
  80. func getCSISourceFromSpec(spec *volume.Spec) (*api.CSIPersistentVolumeSource, error) {
  81. return getPVSourceFromSpec(spec)
  82. }
  83. func getReadOnlyFromSpec(spec *volume.Spec) (bool, error) {
  84. if spec.PersistentVolume != nil &&
  85. spec.PersistentVolume.Spec.CSI != nil {
  86. return spec.ReadOnly, nil
  87. }
  88. return false, fmt.Errorf("CSIPersistentVolumeSource not defined in spec")
  89. }
  90. // log prepends log string with `kubernetes.io/csi`
  91. func log(msg string, parts ...interface{}) string {
  92. return fmt.Sprintf(fmt.Sprintf("%s: %s", CSIPluginName, msg), parts...)
  93. }
  94. // getVolumeDevicePluginDir returns the path where the CSI plugin keeps the
  95. // symlink for a block device associated with a given specVolumeID.
  96. // path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/dev
  97. func getVolumeDevicePluginDir(specVolID string, host volume.VolumeHost) string {
  98. sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID)
  99. return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "dev")
  100. }
  101. // getVolumeDeviceDataDir returns the path where the CSI plugin keeps the
  102. // volume data for a block device associated with a given specVolumeID.
  103. // path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/data
  104. func getVolumeDeviceDataDir(specVolID string, host volume.VolumeHost) string {
  105. sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID)
  106. return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "data")
  107. }
  108. // hasReadWriteOnce returns true if modes contains v1.ReadWriteOnce
  109. func hasReadWriteOnce(modes []api.PersistentVolumeAccessMode) bool {
  110. if modes == nil {
  111. return false
  112. }
  113. for _, mode := range modes {
  114. if mode == api.ReadWriteOnce {
  115. return true
  116. }
  117. }
  118. return false
  119. }
  120. // getSourceFromSpec returns either CSIVolumeSource or CSIPersistentVolumeSource, but not both
  121. func getSourceFromSpec(spec *volume.Spec) (*api.CSIVolumeSource, *api.CSIPersistentVolumeSource, error) {
  122. if spec == nil {
  123. return nil, nil, fmt.Errorf("volume.Spec nil")
  124. }
  125. if spec.Volume != nil && spec.PersistentVolume != nil {
  126. return nil, nil, fmt.Errorf("volume.Spec has both volume and persistent volume sources")
  127. }
  128. if spec.Volume != nil && spec.Volume.CSI != nil && utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) {
  129. return spec.Volume.CSI, nil, nil
  130. }
  131. if spec.PersistentVolume != nil &&
  132. spec.PersistentVolume.Spec.CSI != nil {
  133. return nil, spec.PersistentVolume.Spec.CSI, nil
  134. }
  135. return nil, nil, fmt.Errorf("volume source not found in volume.Spec")
  136. }
  137. // getPVSourceFromSpec ensures only CSIPersistentVolumeSource is present in volume.Spec
  138. func getPVSourceFromSpec(spec *volume.Spec) (*api.CSIPersistentVolumeSource, error) {
  139. volSrc, pvSrc, err := getSourceFromSpec(spec)
  140. if err != nil {
  141. return nil, err
  142. }
  143. if volSrc != nil {
  144. return nil, fmt.Errorf("unexpected api.CSIVolumeSource found in volume.Spec")
  145. }
  146. return pvSrc, nil
  147. }