...
1
16
17 package flexvolume
18
19 import (
20 "fmt"
21 "os"
22
23 "k8s.io/klog/v2"
24 "k8s.io/mount-utils"
25
26 "k8s.io/apimachinery/pkg/types"
27 "k8s.io/kubernetes/pkg/volume"
28 )
29
30 type flexVolumeDetacher struct {
31 plugin *flexVolumeAttachablePlugin
32 }
33
34 var _ volume.Detacher = &flexVolumeDetacher{}
35
36 var _ volume.DeviceUnmounter = &flexVolumeDetacher{}
37
38
39 func (d *flexVolumeDetacher) Detach(volumeName string, hostName types.NodeName) error {
40
41 call := d.plugin.NewDriverCall(detachCmd)
42 call.Append(volumeName)
43 call.Append(string(hostName))
44
45 _, err := call.Run()
46 if isCmdNotSupportedErr(err) {
47 return (*detacherDefaults)(d).Detach(volumeName, hostName)
48 }
49 return err
50 }
51
52
53 func (d *flexVolumeDetacher) UnmountDevice(deviceMountPath string) error {
54
55 pathExists, pathErr := mount.PathExists(deviceMountPath)
56 if !pathExists {
57 klog.Warningf("Warning: Unmount skipped because path does not exist: %v", deviceMountPath)
58 return nil
59 }
60 if pathErr != nil && !mount.IsCorruptedMnt(pathErr) {
61 return fmt.Errorf("error checking path: %w", pathErr)
62 }
63
64 notmnt, err := isNotMounted(d.plugin.host.GetMounter(d.plugin.GetPluginName()), deviceMountPath)
65 if err != nil {
66 if mount.IsCorruptedMnt(err) {
67 notmnt = false
68 } else {
69 return err
70 }
71 }
72
73 if notmnt {
74 klog.Warningf("Warning: Path: %v already unmounted", deviceMountPath)
75 } else {
76 call := d.plugin.NewDriverCall(unmountDeviceCmd)
77 call.Append(deviceMountPath)
78
79 _, err := call.Run()
80 if isCmdNotSupportedErr(err) {
81 err = (*detacherDefaults)(d).UnmountDevice(deviceMountPath)
82 }
83 if err != nil {
84 return err
85 }
86 }
87
88
89 if pathExists, pathErr := mount.PathExists(deviceMountPath); pathErr != nil {
90 return fmt.Errorf("error checking if path exists: %w", pathErr)
91 } else if !pathExists {
92 return nil
93 }
94 return os.Remove(deviceMountPath)
95 }
96
View as plain text