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