Add getPublishDir and getVolumePluginDir

So we don't need to compute these backwards from getPublishPath and
getVolumeDevicePluginDir.
This commit is contained in:
Jan Safranek 2020-02-17 10:51:39 +01:00
parent 0bd2e629c7
commit 073d0b2340
3 changed files with 29 additions and 35 deletions

View File

@ -17,6 +17,7 @@ go_library(
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//pkg/features:go_default_library", "//pkg/features:go_default_library",
"//pkg/util/removeall:go_default_library",
"//pkg/volume:go_default_library", "//pkg/volume:go_default_library",
"//pkg/volume/csi/nodeinfomanager:go_default_library", "//pkg/volume/csi/nodeinfomanager:go_default_library",
"//pkg/volume/util:go_default_library", "//pkg/volume/util:go_default_library",

View File

@ -72,16 +72,15 @@ import (
"os" "os"
"path/filepath" "path/filepath"
volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
"k8s.io/klog"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1" storage "k8s.io/api/storage/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/util/removeall"
"k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume"
volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
utilstrings "k8s.io/utils/strings" 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) 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 // 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} // Example: plugins/kubernetes.io/csi/volumeDevices/publish/{specName}/{podUID}
func (m *csiBlockMapper) getPublishPath() string { 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 // 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 { 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 return nil
@ -456,15 +462,10 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
// files are indeed orphaned. // files are indeed orphaned.
func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error { func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error {
// Remove artifacts of NodePublish. // Remove artifacts of NodePublish.
// publishPath: xxx/plugins/kubernetes.io/csi/volumeDevices/publish/<volume name>/<pod UUID> // publishDir: xxx/plugins/kubernetes.io/csi/volumeDevices/publish/<volume name>
// publishPath was removed by the driver. We need to remove the <volume name>/ dir. // Each PublishVolume() created a subdirectory there. Since everything should be
publishPath := m.getPublishPath() // already unpublished at this point, the directory should be empty by now.
publishDir := filepath.Dir(publishPath) publishDir := m.getPublishDir()
if m.podUID == "" {
// Pod UID is not known during device teardown ("NodeUnstage").
// getPublishPath() squashed "<volume name>/<pod UUID>" into "<volume name>/".
publishDir = publishPath
}
if err := os.Remove(publishDir); err != nil && !os.IsNotExist(err) { if err := os.Remove(publishDir); err != nil && !os.IsNotExist(err) {
return errors.New(log("failed to remove publish directory [%s]: %v", publishDir, 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/<volume name>. // Remove everything under xxx/plugins/kubernetes.io/csi/volumeDevices/<volume name>.
// At this point it contains only "data/vol_data.json" and empty "dev/". // At this point it contains only "data/vol_data.json" and empty "dev/".
dataDir := getVolumeDeviceDataDir(m.specName, m.plugin.host) volumeDir := getVolumePluginDir(m.specName, m.plugin.host)
dataFile := filepath.Join(dataDir, volDataFileName) mounter := m.plugin.host.GetMounter(m.plugin.GetPluginName())
if err := os.Remove(dataFile); err != nil && !os.IsNotExist(err) { if err := removeall.RemoveAllOneFilesystem(mounter, volumeDir); err != nil {
return errors.New(log("failed to delete volume data file [%s]: %v", dataFile, err)) return 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))
} }
return nil return nil

View File

@ -108,20 +108,24 @@ func log(msg string, parts ...interface{}) string {
return fmt.Sprintf(fmt.Sprintf("%s: %s", CSIPluginName, msg), parts...) 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 // getVolumeDevicePluginDir returns the path where the CSI plugin keeps the
// symlink for a block device associated with a given specVolumeID. // symlink for a block device associated with a given specVolumeID.
// path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/dev // path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/dev
func getVolumeDevicePluginDir(specVolID string, host volume.VolumeHost) string { func getVolumeDevicePluginDir(specVolID string, host volume.VolumeHost) string {
sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID) return filepath.Join(getVolumePluginDir(specVolID, host), "dev")
return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "dev")
} }
// getVolumeDeviceDataDir returns the path where the CSI plugin keeps the // getVolumeDeviceDataDir returns the path where the CSI plugin keeps the
// volume data for a block device associated with a given specVolumeID. // volume data for a block device associated with a given specVolumeID.
// path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/data // path: plugins/kubernetes.io/csi/volumeDevices/{specVolumeID}/data
func getVolumeDeviceDataDir(specVolID string, host volume.VolumeHost) string { func getVolumeDeviceDataDir(specVolID string, host volume.VolumeHost) string {
sanitizedSpecVolID := utilstrings.EscapeQualifiedName(specVolID) return filepath.Join(getVolumePluginDir(specVolID, host), "data")
return filepath.Join(host.GetVolumeDevicePluginDir(CSIPluginName), sanitizedSpecVolID, "data")
} }
// hasReadWriteOnce returns true if modes contains v1.ReadWriteOnce // hasReadWriteOnce returns true if modes contains v1.ReadWriteOnce