diff --git a/pkg/cephfs/controllerserver.go b/pkg/cephfs/controllerserver.go index 538cff351..b75406e2d 100644 --- a/pkg/cephfs/controllerserver.go +++ b/pkg/cephfs/controllerserver.go @@ -183,7 +183,7 @@ func (cs *ControllerServer) deleteVolumeDeprecated(req *csi.DeleteVolumeRequest) idLk := volumeIDLocker.Lock(string(volID)) defer volumeIDLocker.Unlock(idLk, string(volID)) - if err = purgeVolume(volID, cr, &ce.VolOptions); err != nil { + if err = purgeVolumeDeprecated(volID, cr, &ce.VolOptions); err != nil { klog.Errorf("failed to delete volume %s: %v", volID, err) return nil, status.Error(codes.Internal, err.Error()) } diff --git a/pkg/cephfs/util.go b/pkg/cephfs/util.go index 7cd07db7d..52d9c9bb5 100644 --- a/pkg/cephfs/util.go +++ b/pkg/cephfs/util.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "os" "os/exec" "google.golang.org/grpc/codes" @@ -90,6 +91,11 @@ func isMountPoint(p string) (bool, error) { return !notMnt, nil } +func pathExists(p string) bool { + _, err := os.Stat(p) + return err == nil +} + // Controller service request validation func (cs *ControllerServer) validateCreateVolumeRequest(req *csi.CreateVolumeRequest) error { if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil { diff --git a/pkg/cephfs/volume.go b/pkg/cephfs/volume.go index e93d8d7ec..7a613f3d1 100644 --- a/pkg/cephfs/volume.go +++ b/pkg/cephfs/volume.go @@ -17,6 +17,9 @@ limitations under the License. package cephfs import ( + "fmt" + "os" + "path" "strconv" "strings" @@ -37,6 +40,18 @@ var ( cephfsInit = false ) +func getCephRootVolumePathLocalDeprecated(volID volumeID) string { + return path.Join(getCephRootPathLocalDeprecated(volID), "csi-volumes", string(volID)) +} + +func getVolumeRootPathCephDeprecated(volID volumeID) string { + return path.Join("/", "csi-volumes", string(volID)) +} + +func getCephRootPathLocalDeprecated(volID volumeID) string { + return fmt.Sprintf("%s/controller/volumes/root-%s", PluginFolder, string(volID)) +} + func getVolumeRootPathCeph(volOptions *volumeOptions, cr *util.Credentials, volID volumeID) (string, error) { stdout, _, err := util.ExecCommand( "ceph", @@ -110,6 +125,70 @@ func createVolume(volOptions *volumeOptions, cr *util.Credentials, volID volumeI return nil } +func mountCephRoot(volID volumeID, volOptions *volumeOptions, adminCr *util.Credentials) error { + cephRoot := getCephRootPathLocalDeprecated(volID) + + // Root path is not set for dynamically provisioned volumes + // Access to cephfs's / is required + volOptions.RootPath = "/" + + if err := createMountPoint(cephRoot); err != nil { + return err + } + + m, err := newMounter(volOptions) + if err != nil { + return fmt.Errorf("failed to create mounter: %v", err) + } + + if err = m.mount(cephRoot, adminCr, volOptions); err != nil { + return fmt.Errorf("error mounting ceph root: %v", err) + } + + return nil +} + +func unmountCephRoot(volID volumeID) { + cephRoot := getCephRootPathLocalDeprecated(volID) + + if err := unmountVolume(cephRoot); err != nil { + klog.Errorf("failed to unmount %s with error %s", cephRoot, err) + } else { + if err := os.Remove(cephRoot); err != nil { + klog.Errorf("failed to remove %s with error %s", cephRoot, err) + } + } +} + +func purgeVolumeDeprecated(volID volumeID, adminCr *util.Credentials, volOptions *volumeOptions) error { + if err := mountCephRoot(volID, volOptions, adminCr); err != nil { + return err + } + defer unmountCephRoot(volID) + + var ( + volRoot = getCephRootVolumePathLocalDeprecated(volID) + volRootDeleting = volRoot + "-deleting" + ) + + if pathExists(volRoot) { + if err := os.Rename(volRoot, volRootDeleting); err != nil { + return fmt.Errorf("couldn't mark volume %s for deletion: %v", volID, err) + } + } else { + if !pathExists(volRootDeleting) { + klog.V(4).Infof("cephfs: volume %s not found, assuming it to be already deleted", volID) + return nil + } + } + + if err := os.RemoveAll(volRootDeleting); err != nil { + return fmt.Errorf("failed to delete volume %s: %v", volID, err) + } + + return nil +} + func purgeVolume(volID volumeID, cr *util.Credentials, volOptions *volumeOptions) error { err := execCommandErr( "ceph", diff --git a/pkg/cephfs/volumeoptions.go b/pkg/cephfs/volumeoptions.go index 11f9f3d6c..4fac5ba43 100644 --- a/pkg/cephfs/volumeoptions.go +++ b/pkg/cephfs/volumeoptions.go @@ -18,7 +18,6 @@ package cephfs import ( "fmt" - "path" "strconv" "github.com/pkg/errors" @@ -272,7 +271,7 @@ func newVolumeOptionsFromVersion1Context(volID string, options, secrets map[stri return nil, nil, err } - opts.RootPath = path.Join("/csi-volumes", string(volumeID(volID))) + opts.RootPath = getVolumeRootPathCephDeprecated(volumeID(volID)) } else { if err = extractOption(&opts.RootPath, "rootPath", options); err != nil { return nil, nil, err