@@ -72,16 +72,15 @@ import (
72
72
"os"
73
73
"path/filepath"
74
74
75
- volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
76
-
77
- "k8s.io/klog"
78
-
79
75
v1 "k8s.io/api/core/v1"
80
76
storage "k8s.io/api/storage/v1"
81
77
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
82
78
"k8s.io/apimachinery/pkg/types"
83
79
"k8s.io/client-go/kubernetes"
80
+ "k8s.io/klog"
81
+ "k8s.io/kubernetes/pkg/util/removeall"
84
82
"k8s.io/kubernetes/pkg/volume"
83
+ volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
85
84
utilstrings "k8s.io/utils/strings"
86
85
)
87
86
@@ -115,10 +114,16 @@ func (m *csiBlockMapper) getStagingPath() string {
115
114
return filepath .Join (m .plugin .host .GetVolumeDevicePluginDir (CSIPluginName ), "staging" , m .specName )
116
115
}
117
116
117
+ // getPublishDir returns path to a directory, where the volume is published to each pod.
118
+ // Example: plugins/kubernetes.io/csi/volumeDevices/publish/{specName}
119
+ func (m * csiBlockMapper ) getPublishDir () string {
120
+ return filepath .Join (m .plugin .host .GetVolumeDevicePluginDir (CSIPluginName ), "publish" , m .specName )
121
+ }
122
+
118
123
// getPublishPath returns a publish path for a file (on the node) that should be used on NodePublishVolume/NodeUnpublishVolume
119
124
// Example: plugins/kubernetes.io/csi/volumeDevices/publish/{specName}/{podUID}
120
125
func (m * csiBlockMapper ) getPublishPath () string {
121
- return filepath .Join (m .plugin . host . GetVolumeDevicePluginDir ( CSIPluginName ), "publish" , m . specName , string (m .podUID ))
126
+ return filepath .Join (m .getPublishDir () , string (m .podUID ))
122
127
}
123
128
124
129
// GetPodDeviceMapPath returns pod's device file which will be mapped to a volume
@@ -445,7 +450,8 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
445
450
}
446
451
}
447
452
if err = m .cleanupOrphanDeviceFiles (); err != nil {
448
- return err
453
+ // V(4) for not so serious error
454
+ klog .V (4 ).Infof ("Failed to clean up block volume directory %s" , err )
449
455
}
450
456
451
457
return nil
@@ -456,15 +462,10 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
456
462
// files are indeed orphaned.
457
463
func (m * csiBlockMapper ) cleanupOrphanDeviceFiles () error {
458
464
// Remove artifacts of NodePublish.
459
- // publishPath: xxx/plugins/kubernetes.io/csi/volumeDevices/publish/<volume name>/<pod UUID>
460
- // publishPath was removed by the driver. We need to remove the <volume name>/ dir.
461
- publishPath := m .getPublishPath ()
462
- publishDir := filepath .Dir (publishPath )
463
- if m .podUID == "" {
464
- // Pod UID is not known during device teardown ("NodeUnstage").
465
- // getPublishPath() squashed "<volume name>/<pod UUID>" into "<volume name>/".
466
- publishDir = publishPath
467
- }
465
+ // publishDir: xxx/plugins/kubernetes.io/csi/volumeDevices/publish/<volume name>
466
+ // Each PublishVolume() created a subdirectory there. Since everything should be
467
+ // already unpublished at this point, the directory should be empty by now.
468
+ publishDir := m .getPublishDir ()
468
469
if err := os .Remove (publishDir ); err != nil && ! os .IsNotExist (err ) {
469
470
return errors .New (log ("failed to remove publish directory [%s]: %v" , publishDir , err ))
470
471
}
@@ -478,22 +479,10 @@ func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error {
478
479
479
480
// Remove everything under xxx/plugins/kubernetes.io/csi/volumeDevices/<volume name>.
480
481
// At this point it contains only "data/vol_data.json" and empty "dev/".
481
- dataDir := getVolumeDeviceDataDir (m .specName , m .plugin .host )
482
- dataFile := filepath .Join (dataDir , volDataFileName )
483
- if err := os .Remove (dataFile ); err != nil && ! os .IsNotExist (err ) {
484
- return errors .New (log ("failed to delete volume data file [%s]: %v" , dataFile , err ))
485
- }
486
- if err := os .Remove (dataDir ); err != nil && ! os .IsNotExist (err ) {
487
- return errors .New (log ("failed to delete volume data directory [%s]: %v" , dataDir , err ))
488
- }
489
-
490
- volumeDir := filepath .Dir (dataDir )
491
- deviceDir := filepath .Join (volumeDir , "dev" )
492
- if err := os .Remove (deviceDir ); err != nil && ! os .IsNotExist (err ) {
493
- return errors .New (log ("failed to delete volume directory [%s]: %v" , deviceDir , err ))
494
- }
495
- if err := os .Remove (volumeDir ); err != nil && ! os .IsNotExist (err ) {
496
- return errors .New (log ("failed to delete volume directory [%s]: %v" , volumeDir , err ))
482
+ volumeDir := getVolumePluginDir (m .specName , m .plugin .host )
483
+ mounter := m .plugin .host .GetMounter (m .plugin .GetPluginName ())
484
+ if err := removeall .RemoveAllOneFilesystem (mounter , volumeDir ); err != nil {
485
+ return err
497
486
}
498
487
499
488
return nil
0 commit comments