k8s.io/kubernetes@v1.29.3/pkg/volume/flexvolume/detacher.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 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 // Detach is part of the volume.Detacher interface. 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 // UnmountDevice is part of the volume.Detacher interface. 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 // Corrupted error is assumed to be mounted. 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 // Flexvolume driver may remove the directory. Ignore if it does. 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 }