unmounter.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/klog"
  18. "k8s.io/utils/exec"
  19. "k8s.io/utils/mount"
  20. "k8s.io/kubernetes/pkg/volume"
  21. )
  22. // FlexVolumeUnmounter is the disk that will be cleaned by this plugin.
  23. type flexVolumeUnmounter struct {
  24. *flexVolume
  25. // Runner used to teardown the volume.
  26. runner exec.Interface
  27. }
  28. var _ volume.Unmounter = &flexVolumeUnmounter{}
  29. // Unmounter interface
  30. func (f *flexVolumeUnmounter) TearDown() error {
  31. path := f.GetPath()
  32. return f.TearDownAt(path)
  33. }
  34. func (f *flexVolumeUnmounter) TearDownAt(dir string) error {
  35. pathExists, pathErr := mount.PathExists(dir)
  36. if pathErr != nil {
  37. // only log warning here since plugins should anyways have to deal with errors
  38. klog.Warningf("Error checking path: %v", pathErr)
  39. } else {
  40. if !pathExists {
  41. klog.Warningf("Warning: Unmount skipped because path does not exist: %v", dir)
  42. return nil
  43. }
  44. }
  45. call := f.plugin.NewDriverCall(unmountCmd)
  46. call.Append(dir)
  47. _, err := call.Run()
  48. if isCmdNotSupportedErr(err) {
  49. err = (*unmounterDefaults)(f).TearDownAt(dir)
  50. }
  51. if err != nil {
  52. return err
  53. }
  54. // Flexvolume driver may remove the directory. Ignore if it does.
  55. if pathExists, pathErr := mount.PathExists(dir); pathErr != nil {
  56. return fmt.Errorf("Error checking if path exists: %v", pathErr)
  57. } else if !pathExists {
  58. return nil
  59. }
  60. return os.Remove(dir)
  61. }