diff --git a/internal/driver/nodeserver.go b/internal/driver/nodeserver.go index 901a360f..8ab25701 100644 --- a/internal/driver/nodeserver.go +++ b/internal/driver/nodeserver.go @@ -247,45 +247,22 @@ func (ns *NodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstag ns.mux.Lock() defer ns.mux.Unlock() - // Create a new file system executor - fs := mountmanager.NewFileSystem() - // Validate req (NodeUnstageVolumeRequest) log.V(4).Info("Validating request", "volumeID", volumeID) if err := validateNodeUnstageVolumeRequest(ctx, req); err != nil { return nil, err } - // Check if the staging target path is already unmounted - notMnt, err := ns.mounter.IsLikelyNotMountPoint(stagingTargetPath) - if err != nil { - if fs.IsNotExist(err) { - log.V(4).Info("Staging target path does not exist, considering as unmounted", "stagingTargetPath", stagingTargetPath) - return &csi.NodeUnstageVolumeResponse{}, nil - } - return nil, errInternal("Check if %s is a mount point failed: %v", stagingTargetPath, err) - } - if notMnt { - log.V(4).Info("Staging target path is not a mount point, skipping unmount", "stagingTargetPath", stagingTargetPath) - } else { - // Unmount the staging target path - log.V(4).Info("Unmounting staging target path", "volumeID", volumeID, "stagingTargetPath", stagingTargetPath) - if err := ns.mounter.Unmount(stagingTargetPath); err != nil { - return nil, errInternal("Failed to unmount %s: %v", stagingTargetPath, err) - } + // Unmount the target path + log.V(4).Info("Unmounting and deleting staging target path", "volumeID", volumeID, "targetPath", stagingTargetPath) + if err := mount.CleanupMountPoint(stagingTargetPath, ns.mounter.Interface, true /* bind mount */); err != nil { + return nil, errInternal("Failed to unmount %s: %v", stagingTargetPath, err) } // If LUKS volume is used, close the LUKS device log.V(4).Info("Closing LUKS device", "volumeID", volumeID, "stagingTargetPath", stagingTargetPath) if err := ns.closeLuksMountSource(ctx, volumeID); err != nil { - log.Error(err, "Failed to close LUKS device", "volumeID", volumeID, "stagingTargetPath", stagingTargetPath) - // Continue with cleanup even if LUKS closing fails - } - - // Remove the staging target path directory - log.V(4).Info("Removing staging target path", "volumeID", volumeID, "stagingTargetPath", stagingTargetPath) - if err := fs.Remove(stagingTargetPath); err != nil && !fs.IsNotExist(err) { - return nil, errInternal("Failed to remove staging target path %s: %v", stagingTargetPath, err) + return nil, errInternal("Failed to close the luks volume %s: %v", volumeID, err) } log.V(2).Info("Successfully completed", "volumeID", volumeID) diff --git a/internal/driver/nodeserver_helpers.go b/internal/driver/nodeserver_helpers.go index 4e5e07ae..3b20d695 100644 --- a/internal/driver/nodeserver_helpers.go +++ b/internal/driver/nodeserver_helpers.go @@ -361,8 +361,8 @@ func (ns *NodeServer) prepareLUKSVolume(ctx context.Context, devicePath string, return luksSource, nil } -// closeMountSources closes any LUKS-encrypted mount sources associated with the given path. -// It retrieves mount sources, checks if each source is a LUKS mapping, and closes it if so. +// closeLuksMountSource closes a LUKS-encrypted mount source for a given volume ID. +// It retrieves the mount source, checks if it's a LUKS volume, and closes it if so. // Returns an error if any operation fails during the process. func (ns *NodeServer) closeLuksMountSource(ctx context.Context, volumeID string) error { log := logger.GetLogger(ctx) @@ -372,7 +372,6 @@ func (ns *NodeServer) closeLuksMountSource(ctx context.Context, volumeID string) if err != nil { return errInternal("closeLuksMountSource failed to get mount source %s: %v", volumeID, err) } - log.V(4).Info("Retrieved mount source", "source", mountSource) log.V(4).Info("Processing mount source", "source", mountSource) isLuksMapping, err := ns.encrypt.isLuksMapping(mountSource) @@ -382,25 +381,26 @@ func (ns *NodeServer) closeLuksMountSource(ctx context.Context, volumeID string) log.V(4).Info("LUKS mapping check result", "isLuksMapping", isLuksMapping) if isLuksMapping { - log.V(4).Info("Closing LUKS mapping", "mappingName", mountSource) + log.V(4).Info("Closing LUKS volume", "PVC", mountSource) if err := ns.encrypt.luksClose(mountSource); err != nil { return errInternal("closeLuksMountSource failed to close luks mount %s: %v", mountSource, err) } - log.V(4).Info("Successfully closed LUKS mapping", "mountSource", mountSource) + log.V(4).Info("Successfully closed LUKS volume", "PVC", mountSource) } - + log.V(4).Info("Exiting closeLuksMountSource") return nil } -// getMountSources retrieves the mount sources for a given target path using the 'findmnt' command. -// It returns a slice of strings containing the mount sources, or an error if the operation fails. -// If 'findmnt' is not found or returns no results, appropriate errors or an empty slice are returned. + +// getMountSource extracts the PVC name from a given input string. +// The input is expected to be in the format "number-pvcname", e.g., "8934-pvc232323". +// It returns the PVC name (the part starting with "pvc") or an error if the input format is invalid. func (ns *NodeServer) getMountSource(ctx context.Context, input string) (string, error) { log := logger.GetLogger(ctx) log.V(4).Info("Entering getMountSources", "input", input) - // Split the input string by "-" + // Split the input string by "-". example: '8934-pvc232323' parts := strings.Split(input, "-") if len(parts) != 2 { return "", fmt.Errorf("invalid input format: %s", input)