sio_plugin.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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) RequiresRemount() bool {
  61. return false
  62. }
  63. func (p *sioPlugin) NewMounter(
  64. spec *volume.Spec,
  65. pod *api.Pod,
  66. _ volume.VolumeOptions) (volume.Mounter, error) {
  67. // extract source info from either ScaleIOVolumeSource or ScaleIOPersistentVolumeSource type
  68. attribs, err := getVolumeSourceAttribs(spec)
  69. if err != nil {
  70. return nil, errors.New(log("mounter failed to extract volume attributes from spec: %v", err))
  71. }
  72. secretName, secretNS, err := getSecretAndNamespaceFromSpec(spec, pod)
  73. if err != nil {
  74. return nil, errors.New(log("failed to get secret name or secretNamespace: %v", err))
  75. }
  76. return &sioVolume{
  77. pod: pod,
  78. spec: spec,
  79. secretName: secretName,
  80. secretNamespace: secretNS,
  81. volSpecName: spec.Name(),
  82. volName: attribs.volName,
  83. podUID: pod.UID,
  84. readOnly: attribs.readOnly,
  85. fsType: attribs.fsType,
  86. plugin: p,
  87. }, nil
  88. }
  89. // NewUnmounter creates a representation of the volume to unmount
  90. func (p *sioPlugin) NewUnmounter(specName string, podUID types.UID) (volume.Unmounter, error) {
  91. klog.V(4).Info(log("Unmounter for %s", specName))
  92. return &sioVolume{
  93. podUID: podUID,
  94. volSpecName: specName,
  95. plugin: p,
  96. }, nil
  97. }
  98. func (p *sioPlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) {
  99. sioVol := &api.Volume{
  100. Name: volumeName,
  101. VolumeSource: api.VolumeSource{
  102. ScaleIO: &api.ScaleIOVolumeSource{},
  103. },
  104. }
  105. return volume.NewSpecFromVolume(sioVol), nil
  106. }
  107. // SupportsMountOption returns true if volume plugins supports Mount options
  108. // Specifying mount options in a volume plugin that doesn't support
  109. // user specified mount options will result in error creating persistent volumes
  110. func (p *sioPlugin) SupportsMountOption() bool {
  111. return false
  112. }
  113. // SupportsBulkVolumeVerification checks if volume plugin type is capable
  114. // of enabling bulk polling of all nodes. This can speed up verification of
  115. // attached volumes by quite a bit, but underlying pluging must support it.
  116. func (p *sioPlugin) SupportsBulkVolumeVerification() bool {
  117. return false
  118. }
  119. //******************************
  120. // PersistentVolumePlugin Impl
  121. // *****************************
  122. var _ volume.PersistentVolumePlugin = &sioPlugin{}
  123. func (p *sioPlugin) GetAccessModes() []api.PersistentVolumeAccessMode {
  124. return []api.PersistentVolumeAccessMode{
  125. api.ReadWriteOnce,
  126. api.ReadOnlyMany,
  127. }
  128. }
  129. // ***************************
  130. // DeletableVolumePlugin Impl
  131. //****************************
  132. var _ volume.DeletableVolumePlugin = &sioPlugin{}
  133. func (p *sioPlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  134. attribs, err := getVolumeSourceAttribs(spec)
  135. if err != nil {
  136. klog.Error(log("deleter failed to extract volume attributes from spec: %v", err))
  137. return nil, err
  138. }
  139. secretName, secretNS, err := getSecretAndNamespaceFromSpec(spec, nil)
  140. if err != nil {
  141. return nil, errors.New(log("failed to get secret name or secretNamespace: %v", err))
  142. }
  143. return &sioVolume{
  144. spec: spec,
  145. secretName: secretName,
  146. secretNamespace: secretNS,
  147. volSpecName: spec.Name(),
  148. volName: attribs.volName,
  149. plugin: p,
  150. readOnly: attribs.readOnly,
  151. }, nil
  152. }
  153. // *********************************
  154. // ProvisionableVolumePlugin Impl
  155. // *********************************
  156. var _ volume.ProvisionableVolumePlugin = &sioPlugin{}
  157. func (p *sioPlugin) NewProvisioner(options volume.VolumeOptions) (volume.Provisioner, error) {
  158. klog.V(4).Info(log("creating Provisioner"))
  159. configData := options.Parameters
  160. if configData == nil {
  161. klog.Error(log("provisioner missing parameters, unable to continue"))
  162. return nil, errors.New("option parameters missing")
  163. }
  164. // Supports ref of name of secret a couple of ways:
  165. // options.Parameters["secretRef"] for backward compat, or
  166. // options.Parameters["secretName"]
  167. secretName := configData[confKey.secretName]
  168. if secretName == "" {
  169. secretName = configData["secretName"]
  170. configData[confKey.secretName] = secretName
  171. }
  172. secretNS := configData[confKey.secretNamespace]
  173. if secretNS == "" {
  174. secretNS = options.PVC.Namespace
  175. }
  176. return &sioVolume{
  177. configData: configData,
  178. plugin: p,
  179. options: options,
  180. secretName: secretName,
  181. secretNamespace: secretNS,
  182. volSpecName: options.PVName,
  183. }, nil
  184. }