attacher.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 fc
  14. import (
  15. "fmt"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. utilfeature "k8s.io/apiserver/pkg/util/feature"
  23. "k8s.io/klog"
  24. "k8s.io/kubernetes/pkg/features"
  25. "k8s.io/kubernetes/pkg/util/mount"
  26. "k8s.io/kubernetes/pkg/volume"
  27. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  28. )
  29. type fcAttacher struct {
  30. host volume.VolumeHost
  31. manager diskManager
  32. }
  33. var _ volume.Attacher = &fcAttacher{}
  34. var _ volume.DeviceMounter = &fcAttacher{}
  35. var _ volume.AttachableVolumePlugin = &fcPlugin{}
  36. var _ volume.DeviceMountableVolumePlugin = &fcPlugin{}
  37. func (plugin *fcPlugin) NewAttacher() (volume.Attacher, error) {
  38. return &fcAttacher{
  39. host: plugin.host,
  40. manager: &fcUtil{},
  41. }, nil
  42. }
  43. func (plugin *fcPlugin) NewDeviceMounter() (volume.DeviceMounter, error) {
  44. return plugin.NewAttacher()
  45. }
  46. func (plugin *fcPlugin) GetDeviceMountRefs(deviceMountPath string) ([]string, error) {
  47. mounter := plugin.host.GetMounter(plugin.GetPluginName())
  48. return mounter.GetMountRefs(deviceMountPath)
  49. }
  50. func (attacher *fcAttacher) Attach(spec *volume.Spec, nodeName types.NodeName) (string, error) {
  51. return "", nil
  52. }
  53. func (attacher *fcAttacher) VolumesAreAttached(specs []*volume.Spec, nodeName types.NodeName) (map[*volume.Spec]bool, error) {
  54. volumesAttachedCheck := make(map[*volume.Spec]bool)
  55. for _, spec := range specs {
  56. volumesAttachedCheck[spec] = true
  57. }
  58. return volumesAttachedCheck, nil
  59. }
  60. func (attacher *fcAttacher) WaitForAttach(spec *volume.Spec, devicePath string, _ *v1.Pod, timeout time.Duration) (string, error) {
  61. mounter, err := volumeSpecToMounter(spec, attacher.host)
  62. if err != nil {
  63. klog.Warningf("failed to get fc mounter: %v", err)
  64. return "", err
  65. }
  66. return attacher.manager.AttachDisk(*mounter)
  67. }
  68. func (attacher *fcAttacher) GetDeviceMountPath(
  69. spec *volume.Spec) (string, error) {
  70. mounter, err := volumeSpecToMounter(spec, attacher.host)
  71. if err != nil {
  72. klog.Warningf("failed to get fc mounter: %v", err)
  73. return "", err
  74. }
  75. return attacher.manager.MakeGlobalPDName(*mounter.fcDisk), nil
  76. }
  77. func (attacher *fcAttacher) MountDevice(spec *volume.Spec, devicePath string, deviceMountPath string) error {
  78. mounter := attacher.host.GetMounter(fcPluginName)
  79. notMnt, err := mounter.IsLikelyNotMountPoint(deviceMountPath)
  80. if err != nil {
  81. if os.IsNotExist(err) {
  82. if err := os.MkdirAll(deviceMountPath, 0750); err != nil {
  83. return err
  84. }
  85. notMnt = true
  86. } else {
  87. return err
  88. }
  89. }
  90. volumeSource, readOnly, err := getVolumeSource(spec)
  91. if err != nil {
  92. return err
  93. }
  94. options := []string{}
  95. if readOnly {
  96. options = append(options, "ro")
  97. }
  98. if notMnt {
  99. diskMounter := &mount.SafeFormatAndMount{Interface: mounter, Exec: attacher.host.GetExec(fcPluginName)}
  100. mountOptions := volumeutil.MountOptionFromSpec(spec, options...)
  101. err = diskMounter.FormatAndMount(devicePath, deviceMountPath, volumeSource.FSType, mountOptions)
  102. if err != nil {
  103. os.Remove(deviceMountPath)
  104. return err
  105. }
  106. }
  107. return nil
  108. }
  109. type fcDetacher struct {
  110. mounter mount.Interface
  111. manager diskManager
  112. }
  113. var _ volume.Detacher = &fcDetacher{}
  114. var _ volume.DeviceUnmounter = &fcDetacher{}
  115. func (plugin *fcPlugin) NewDetacher() (volume.Detacher, error) {
  116. return &fcDetacher{
  117. mounter: plugin.host.GetMounter(plugin.GetPluginName()),
  118. manager: &fcUtil{},
  119. }, nil
  120. }
  121. func (plugin *fcPlugin) NewDeviceUnmounter() (volume.DeviceUnmounter, error) {
  122. return plugin.NewDetacher()
  123. }
  124. func (detacher *fcDetacher) Detach(volumeName string, nodeName types.NodeName) error {
  125. return nil
  126. }
  127. func (detacher *fcDetacher) UnmountDevice(deviceMountPath string) error {
  128. // Specify device name for DetachDisk later
  129. devName, _, err := mount.GetDeviceNameFromMount(detacher.mounter, deviceMountPath)
  130. if err != nil {
  131. klog.Errorf("fc: failed to get device from mnt: %s\nError: %v", deviceMountPath, err)
  132. return err
  133. }
  134. // Unmount for deviceMountPath(=globalPDPath)
  135. err = mount.CleanupMountPoint(deviceMountPath, detacher.mounter, false)
  136. if err != nil {
  137. return fmt.Errorf("fc: failed to unmount: %s\nError: %v", deviceMountPath, err)
  138. }
  139. unMounter := volumeSpecToUnmounter(detacher.mounter)
  140. err = detacher.manager.DetachDisk(*unMounter, devName)
  141. if err != nil {
  142. return fmt.Errorf("fc: failed to detach disk: %s\nError: %v", devName, err)
  143. }
  144. klog.V(4).Infof("fc: successfully detached disk: %s", devName)
  145. return nil
  146. }
  147. func (plugin *fcPlugin) CanAttach(spec *volume.Spec) (bool, error) {
  148. return true, nil
  149. }
  150. func (plugin *fcPlugin) CanDeviceMount(spec *volume.Spec) (bool, error) {
  151. return true, nil
  152. }
  153. func volumeSpecToMounter(spec *volume.Spec, host volume.VolumeHost) (*fcDiskMounter, error) {
  154. fc, readOnly, err := getVolumeSource(spec)
  155. if err != nil {
  156. return nil, err
  157. }
  158. var lun string
  159. var wwids []string
  160. if fc.Lun != nil && len(fc.TargetWWNs) != 0 {
  161. lun = strconv.Itoa(int(*fc.Lun))
  162. } else if len(fc.WWIDs) != 0 {
  163. for _, wwid := range fc.WWIDs {
  164. wwids = append(wwids, strings.Replace(wwid, " ", "_", -1))
  165. }
  166. } else {
  167. return nil, fmt.Errorf("fc: no fc disk information found. failed to make a new mounter")
  168. }
  169. fcDisk := &fcDisk{
  170. plugin: &fcPlugin{
  171. host: host,
  172. },
  173. wwns: fc.TargetWWNs,
  174. lun: lun,
  175. wwids: wwids,
  176. io: &osIOHandler{},
  177. }
  178. // TODO: remove feature gate check after no longer needed
  179. if utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) {
  180. volumeMode, err := volumeutil.GetVolumeMode(spec)
  181. if err != nil {
  182. return nil, err
  183. }
  184. klog.V(5).Infof("fc: volumeSpecToMounter volumeMode %s", volumeMode)
  185. return &fcDiskMounter{
  186. fcDisk: fcDisk,
  187. fsType: fc.FSType,
  188. volumeMode: volumeMode,
  189. readOnly: readOnly,
  190. mounter: volumeutil.NewSafeFormatAndMountFromHost(fcPluginName, host),
  191. deviceUtil: volumeutil.NewDeviceHandler(volumeutil.NewIOHandler()),
  192. }, nil
  193. }
  194. return &fcDiskMounter{
  195. fcDisk: fcDisk,
  196. fsType: fc.FSType,
  197. readOnly: readOnly,
  198. mounter: volumeutil.NewSafeFormatAndMountFromHost(fcPluginName, host),
  199. deviceUtil: volumeutil.NewDeviceHandler(volumeutil.NewIOHandler()),
  200. }, nil
  201. }
  202. func volumeSpecToUnmounter(mounter mount.Interface) *fcDiskUnmounter {
  203. return &fcDiskUnmounter{
  204. fcDisk: &fcDisk{
  205. io: &osIOHandler{},
  206. },
  207. mounter: mounter,
  208. deviceUtil: volumeutil.NewDeviceHandler(volumeutil.NewIOHandler()),
  209. }
  210. }