sio_plugin.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 scaleio
  14. import (
  15. "errors"
  16. api "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/klog"
  19. "k8s.io/kubernetes/pkg/volume"
  20. "k8s.io/utils/keymutex"
  21. )
  22. const (
  23. sioPluginName = "kubernetes.io/scaleio"
  24. sioConfigFileName = "sioconf.dat"
  25. )
  26. type sioPlugin struct {
  27. host volume.VolumeHost
  28. volumeMtx keymutex.KeyMutex
  29. }
  30. // ProbeVolumePlugins is the primary entrypoint for volume plugins.
  31. func ProbeVolumePlugins() []volume.VolumePlugin {
  32. p := &sioPlugin{
  33. host: nil,
  34. }
  35. return []volume.VolumePlugin{p}
  36. }
  37. // *******************
  38. // VolumePlugin Impl
  39. // *******************
  40. var _ volume.VolumePlugin = &sioPlugin{}
  41. func (p *sioPlugin) Init(host volume.VolumeHost) error {
  42. p.host = host
  43. p.volumeMtx = keymutex.NewHashed(0)
  44. return nil
  45. }
  46. func (p *sioPlugin) GetPluginName() string {
  47. return sioPluginName
  48. }
  49. func (p *sioPlugin) GetVolumeName(spec *volume.Spec) (string, error) {
  50. attribs, err := getVolumeSourceAttribs(spec)
  51. if err != nil {
  52. return "", err
  53. }
  54. return attribs.volName, nil
  55. }
  56. func (p *sioPlugin) CanSupport(spec *volume.Spec) bool {
  57. return (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.ScaleIO != nil) ||
  58. (spec.Volume != nil && spec.Volume.ScaleIO != nil)
  59. }
  60. func (p *sioPlugin) IsMigratedToCSI() bool {
  61. return false
  62. }
  63. func (p *sioPlugin) RequiresRemount() bool {
  64. return false
  65. }
  66. func (p *sioPlugin) NewMounter(
  67. spec *volume.Spec,
  68. pod *api.Pod,
  69. _ volume.VolumeOptions) (volume.Mounter, error) {
  70. // extract source info from either ScaleIOVolumeSource or ScaleIOPersistentVolumeSource type
  71. attribs, err := getVolumeSourceAttribs(spec)
  72. if err != nil {
  73. return nil, errors.New(log("mounter failed to extract volume attributes from spec: %v", err))
  74. }
  75. secretName, secretNS, err := getSecretAndNamespaceFromSpec(spec, pod)
  76. if err != nil {
  77. return nil, errors.New(log("failed to get secret name or secretNamespace: %v", err))
  78. }
  79. return &sioVolume{
  80. pod: pod,
  81. spec: spec,
  82. secretName: secretName,
  83. secretNamespace: secretNS,
  84. volSpecName: spec.Name(),
  85. volName: attribs.volName,
  86. podUID: pod.UID,
  87. readOnly: attribs.readOnly,
  88. fsType: attribs.fsType,
  89. plugin: p,
  90. }, nil
  91. }
  92. // NewUnmounter creates a representation of the volume to unmount
  93. func (p *sioPlugin) NewUnmounter(specName string, podUID types.UID) (volume.Unmounter, error) {
  94. klog.V(4).Info(log("Unmounter for %s", specName))
  95. return &sioVolume{
  96. podUID: podUID,
  97. volSpecName: specName,
  98. plugin: p,
  99. }, nil
  100. }
  101. func (p *sioPlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) {
  102. sioVol := &api.Volume{
  103. Name: volumeName,
  104. VolumeSource: api.VolumeSource{
  105. ScaleIO: &api.ScaleIOVolumeSource{},
  106. },
  107. }
  108. return volume.NewSpecFromVolume(sioVol), nil
  109. }
  110. // SupportsMountOption returns true if volume plugins supports Mount options
  111. // Specifying mount options in a volume plugin that doesn't support
  112. // user specified mount options will result in error creating persistent volumes
  113. func (p *sioPlugin) SupportsMountOption() bool {
  114. return false
  115. }
  116. // SupportsBulkVolumeVerification checks if volume plugin type is capable
  117. // of enabling bulk polling of all nodes. This can speed up verification of
  118. // attached volumes by quite a bit, but underlying pluging must support it.
  119. func (p *sioPlugin) SupportsBulkVolumeVerification() bool {
  120. return false
  121. }
  122. //******************************
  123. // PersistentVolumePlugin Impl
  124. // *****************************
  125. var _ volume.PersistentVolumePlugin = &sioPlugin{}
  126. func (p *sioPlugin) GetAccessModes() []api.PersistentVolumeAccessMode {
  127. return []api.PersistentVolumeAccessMode{
  128. api.ReadWriteOnce,
  129. api.ReadOnlyMany,
  130. }
  131. }
  132. // ***************************
  133. // DeletableVolumePlugin Impl
  134. //****************************
  135. var _ volume.DeletableVolumePlugin = &sioPlugin{}
  136. func (p *sioPlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  137. attribs, err := getVolumeSourceAttribs(spec)
  138. if err != nil {
  139. klog.Error(log("deleter failed to extract volume attributes from spec: %v", err))
  140. return nil, err
  141. }
  142. secretName, secretNS, err := getSecretAndNamespaceFromSpec(spec, nil)
  143. if err != nil {
  144. return nil, errors.New(log("failed to get secret name or secretNamespace: %v", err))
  145. }
  146. return &sioVolume{
  147. spec: spec,
  148. secretName: secretName,
  149. secretNamespace: secretNS,
  150. volSpecName: spec.Name(),
  151. volName: attribs.volName,
  152. plugin: p,
  153. readOnly: attribs.readOnly,
  154. }, nil
  155. }
  156. // *********************************
  157. // ProvisionableVolumePlugin Impl
  158. // *********************************
  159. var _ volume.ProvisionableVolumePlugin = &sioPlugin{}
  160. func (p *sioPlugin) NewProvisioner(options volume.VolumeOptions) (volume.Provisioner, error) {
  161. klog.V(4).Info(log("creating Provisioner"))
  162. configData := options.Parameters
  163. if configData == nil {
  164. klog.Error(log("provisioner missing parameters, unable to continue"))
  165. return nil, errors.New("option parameters missing")
  166. }
  167. // Supports ref of name of secret a couple of ways:
  168. // options.Parameters["secretRef"] for backward compat, or
  169. // options.Parameters["secretName"]
  170. secretName := configData[confKey.secretName]
  171. if secretName == "" {
  172. secretName = configData["secretName"]
  173. configData[confKey.secretName] = secretName
  174. }
  175. secretNS := configData[confKey.secretNamespace]
  176. if secretNS == "" {
  177. secretNS = options.PVC.Namespace
  178. }
  179. return &sioVolume{
  180. configData: configData,
  181. plugin: p,
  182. options: options,
  183. secretName: secretName,
  184. secretNamespace: secretNS,
  185. volSpecName: options.PVName,
  186. }, nil
  187. }