Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaja Omer committed Sep 10, 2024
1 parent 7454f96 commit d94b6da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 38 deletions.
33 changes: 5 additions & 28 deletions internal/driver/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions internal/driver/nodeserver_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d94b6da

Please sign in to comment.