detacher.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 flexvolume
  14. import (
  15. "fmt"
  16. "os"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/klog"
  19. "k8s.io/kubernetes/pkg/util/mount"
  20. "k8s.io/kubernetes/pkg/volume"
  21. )
  22. type flexVolumeDetacher struct {
  23. plugin *flexVolumeAttachablePlugin
  24. }
  25. var _ volume.Detacher = &flexVolumeDetacher{}
  26. var _ volume.DeviceUnmounter = &flexVolumeDetacher{}
  27. // Detach is part of the volume.Detacher interface.
  28. func (d *flexVolumeDetacher) Detach(volumeName string, hostName types.NodeName) error {
  29. call := d.plugin.NewDriverCall(detachCmd)
  30. call.Append(volumeName)
  31. call.Append(string(hostName))
  32. _, err := call.Run()
  33. if isCmdNotSupportedErr(err) {
  34. return (*detacherDefaults)(d).Detach(volumeName, hostName)
  35. }
  36. return err
  37. }
  38. // UnmountDevice is part of the volume.Detacher interface.
  39. func (d *flexVolumeDetacher) UnmountDevice(deviceMountPath string) error {
  40. pathExists, pathErr := mount.PathExists(deviceMountPath)
  41. if !pathExists {
  42. klog.Warningf("Warning: Unmount skipped because path does not exist: %v", deviceMountPath)
  43. return nil
  44. }
  45. if pathErr != nil && !mount.IsCorruptedMnt(pathErr) {
  46. return fmt.Errorf("Error checking path: %v", pathErr)
  47. }
  48. notmnt, err := isNotMounted(d.plugin.host.GetMounter(d.plugin.GetPluginName()), deviceMountPath)
  49. if err != nil {
  50. if mount.IsCorruptedMnt(err) {
  51. notmnt = false // Corrupted error is assumed to be mounted.
  52. } else {
  53. return err
  54. }
  55. }
  56. if notmnt {
  57. klog.Warningf("Warning: Path: %v already unmounted", deviceMountPath)
  58. } else {
  59. call := d.plugin.NewDriverCall(unmountDeviceCmd)
  60. call.Append(deviceMountPath)
  61. _, err := call.Run()
  62. if isCmdNotSupportedErr(err) {
  63. err = (*detacherDefaults)(d).UnmountDevice(deviceMountPath)
  64. }
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. // Flexvolume driver may remove the directory. Ignore if it does.
  70. if pathExists, pathErr := mount.PathExists(deviceMountPath); pathErr != nil {
  71. return fmt.Errorf("Error checking if path exists: %v", pathErr)
  72. } else if !pathExists {
  73. return nil
  74. }
  75. return os.Remove(deviceMountPath)
  76. }