From 073d0b234076b91b83f97085656c8b614955a04f Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Mon, 17 Feb 2020 10:51:39 +0100 Subject: [PATCH] Add getPublishDir and getVolumePluginDir So we don't need to compute these backwards from getPublishPath and getVolumeDevicePluginDir. --- pkg/volume/csi/BUILD | 1 + pkg/volume/csi/csi_block.go | 51 +++++++++++++++---------------------- pkg/volume/csi/csi_util.go | 12 ++++++--- 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/pkg/volume/csi/BUILD b/pkg/volume/csi/BUILD index f81314c7565..6d237e7f052 100644 --- a/pkg/volume/csi/BUILD +++ b/pkg/volume/csi/BUILD @@ -17,6 +17,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/features:go_default_library", + "//pkg/util/removeall:go_default_library", "//pkg/volume:go_default_library", "//pkg/volume/csi/nodeinfomanager:go_default_library", "//pkg/volume/util:go_default_library", diff --git a/pkg/volume/csi/csi_block.go b/pkg/volume/csi/csi_block.go index 20cd9791790..ee2ae319ee4 100644 --- a/pkg/volume/csi/csi_block.go +++ b/pkg/volume/csi/csi_block.go @@ -72,16 +72,15 @@ import ( "os" "path/filepath" - volumetypes "k8s.io/kubernetes/pkg/volume/util/types" - - "k8s.io/klog" - v1 "k8s.io/api/core/v1" storage "k8s.io/api/storage/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" + "k8s.io/klog" + "k8s.io/kubernetes/pkg/util/removeall" "k8s.io/kubernetes/pkg/volume" + volumetypes "k8s.io/kubernetes/pkg/volume/util/types" utilstrings "k8s.io/utils/strings" ) @@ -115,10 +114,16 @@ func (m *csiBlockMapper) getStagingPath() string { return filepath.Join(m.plugin.host.GetVolumeDevicePluginDir(CSIPluginName), "staging", m.specName) } +// getPublishDir returns path to a directory, where the volume is published to each pod. +// Example: plugins/kubernetes.io/csi/volumeDevices/publish/{specName} +func (m *csiBlockMapper) getPublishDir() string { + return filepath.Join(m.plugin.host.GetVolumeDevicePluginDir(CSIPluginName), "publish", m.specName) +} + // getPublishPath returns a publish path for a file (on the node) that should be used on NodePublishVolume/NodeUnpublishVolume // Example: plugins/kubernetes.io/csi/volumeDevices/publish/{specName}/{podUID} func (m *csiBlockMapper) getPublishPath() string { - return filepath.Join(m.plugin.host.GetVolumeDevicePluginDir(CSIPluginName), "publish", m.specName, string(m.podUID)) + return filepath.Join(m.getPublishDir(), string(m.podUID)) } // 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 } } if err = m.cleanupOrphanDeviceFiles(); err != nil { - return err + // V(4) for not so serious error + klog.V(4).Infof("Failed to clean up block volume directory %s", err) } return nil @@ -456,15 +462,10 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error // files are indeed orphaned. func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error { // Remove artifacts of NodePublish. - // publishPath: xxx/plugins/kubernetes.io/csi/volumeDevices/publish// - // publishPath was removed by the driver. We need to remove the / dir. - publishPath := m.getPublishPath() - publishDir := filepath.Dir(publishPath) - if m.podUID == "" { - // Pod UID is not known during device teardown ("NodeUnstage"). - // getPublishPath() squashed "/" into "/". - publishDir = publishPath - } + // publishDir: xxx/plugins/kubernetes.io/csi/volumeDevices/publish/ + // Each PublishVolume() created a subdirectory there. Since everything should be + // already unpublished at this point, the directory should be empty by now. + publishDir := m.getPublishDir() if err := os.Remove(publishDir); err != nil && !os.IsNotExist(err) { return errors.New(log("failed to remove publish directory [%s]: %v", publishDir, err)) } @@ -478,22 +479,10 @@ func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error { // Remove everything under xxx/plugins/kubernetes.io/csi/volumeDevices/. // At this point it contains only "data/vol_data.json" and empty "dev/". - dataDir := getVolumeDeviceDataDir(m.specName, m.plugin.host) - dataFile := filepath.Join(dataDir, volDataFileName) - if err := os.Remove(dataFile); err != nil && !os.IsNotExist(err) { - return errors.New(log("failed to delete volume data file [%s]: %v", dataFile, err)) - } - if err := os.Remove(dataDir); err != nil && !os.IsNotExist(err) { - return errors.New(log("failed to delete volume data directory [%s]: %v", dataDir, err)) - } - - volumeDir := filepath.Dir(dataDir) - deviceDir := filepath.Join(volumeDir, "dev") - if err := os.Remove(deviceDir); err != nil && !os.IsNotExist(err) { - return errors.New(log("failed to delete volume directory [%s]: %v", deviceDir, err)) - } - if err := os.Remove(volumeDir); err != nil && !os.IsNotExist(err) { - return errors.New(log("failed to delete volume directory [%s]: %v", volumeDir, err)) + volumeDir := getVolumePluginDir(m.specName, m.plugin.host) + mounter := m.plugin.host.GetMounter(m.plugin.GetPluginName()) + if err := removeall.RemoveAllOneFilesystem(mounter, volumeDir); err != nil { + return err } return nil diff --git a/pkg/volume/csi/csi_util.go b/pkg/volume/csi/csi_util.go index ca678bfac45..96b3c497f0e 100644 --- a/pkg/volume/csi/csi_util.go +++ b/pkg/volume/csi/csi_util.go @@ -108,20 +108,24 @@ func log(msg string, parts ...interface{}) string { return fmt.Sprintf(fmt.Sprintf("%s: %s", CSIPluginName, msg), parts...) } +// getVolumePluginDir returns the path where CSI plugin keeps metadata for given volume +func getVolumePluginDir(specVolID string, host volume.VolumeHost) string { + sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID) + return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID) +} + // getVolumeDevicePluginDir returns the path where the CSI plugin keeps the // symlink for a block device associated with a given specVolumeID. // path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/dev func getVolumeDevicePluginDir(specVolID string, host volume.VolumeHost) string { - sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID) - return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "dev") + return filepath.Join(getVolumePluginDir(specVolID, host), "dev") } // getVolumeDeviceDataDir returns the path where the CSI plugin keeps the // volume data for a block device associated with a given specVolumeID. // path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/data func getVolumeDeviceDataDir(specVolID string, host volume.VolumeHost) string { - sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID) - return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "data") + return filepath.Join(getVolumePluginDir(specVolID, host), "data") } // hasReadWriteOnce returns true if modes contains v1.ReadWriteOnce