Skip to content

Commit 073d0b2

Browse files
committed
Add getPublishDir and getVolumePluginDir
So we don't need to compute these backwards from getPublishPath and getVolumeDevicePluginDir.
1 parent 0bd2e62 commit 073d0b2

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

pkg/volume/csi/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_library(
1717
visibility = ["//visibility:public"],
1818
deps = [
1919
"//pkg/features:go_default_library",
20+
"//pkg/util/removeall:go_default_library",
2021
"//pkg/volume:go_default_library",
2122
"//pkg/volume/csi/nodeinfomanager:go_default_library",
2223
"//pkg/volume/util:go_default_library",

pkg/volume/csi/csi_block.go

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,15 @@ import (
7272
"os"
7373
"path/filepath"
7474

75-
volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
76-
77-
"k8s.io/klog"
78-
7975
v1 "k8s.io/api/core/v1"
8076
storage "k8s.io/api/storage/v1"
8177
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
8278
"k8s.io/apimachinery/pkg/types"
8379
"k8s.io/client-go/kubernetes"
80+
"k8s.io/klog"
81+
"k8s.io/kubernetes/pkg/util/removeall"
8482
"k8s.io/kubernetes/pkg/volume"
83+
volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
8584
utilstrings "k8s.io/utils/strings"
8685
)
8786

@@ -115,10 +114,16 @@ func (m *csiBlockMapper) getStagingPath() string {
115114
return filepath.Join(m.plugin.host.GetVolumeDevicePluginDir(CSIPluginName), "staging", m.specName)
116115
}
117116

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+
118123
// getPublishPath returns a publish path for a file (on the node) that should be used on NodePublishVolume/NodeUnpublishVolume
119124
// Example: plugins/kubernetes.io/csi/volumeDevices/publish/{specName}/{podUID}
120125
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))
122127
}
123128

124129
// 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
445450
}
446451
}
447452
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)
449455
}
450456

451457
return nil
@@ -456,15 +462,10 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
456462
// files are indeed orphaned.
457463
func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error {
458464
// 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()
468469
if err := os.Remove(publishDir); err != nil && !os.IsNotExist(err) {
469470
return errors.New(log("failed to remove publish directory [%s]: %v", publishDir, err))
470471
}
@@ -478,22 +479,10 @@ func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error {
478479

479480
// Remove everything under xxx/plugins/kubernetes.io/csi/volumeDevices/<volume name>.
480481
// 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
497486
}
498487

499488
return nil

pkg/volume/csi/csi_util.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,24 @@ func log(msg string, parts ...interface{}) string {
108108
return fmt.Sprintf(fmt.Sprintf("%s: %s", CSIPluginName, msg), parts...)
109109
}
110110

111+
// getVolumePluginDir returns the path where CSI plugin keeps metadata for given volume
112+
func getVolumePluginDir(specVolID string, host volume.VolumeHost) string {
113+
sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID)
114+
return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID)
115+
}
116+
111117
// getVolumeDevicePluginDir returns the path where the CSI plugin keeps the
112118
// symlink for a block device associated with a given specVolumeID.
113119
// path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/dev
114120
func getVolumeDevicePluginDir(specVolID string, host volume.VolumeHost) string {
115-
sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID)
116-
return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "dev")
121+
return filepath.Join(getVolumePluginDir(specVolID, host), "dev")
117122
}
118123

119124
// getVolumeDeviceDataDir returns the path where the CSI plugin keeps the
120125
// volume data for a block device associated with a given specVolumeID.
121126
// path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/data
122127
func getVolumeDeviceDataDir(specVolID string, host volume.VolumeHost) string {
123-
sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID)
124-
return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "data")
128+
return filepath.Join(getVolumePluginDir(specVolID, host), "data")
125129
}
126130

127131
// hasReadWriteOnce returns true if modes contains v1.ReadWriteOnce

0 commit comments

Comments
 (0)