diff --git a/pkg/rbd/nodeserver.go b/pkg/rbd/nodeserver.go index fde41a180..24d73be82 100644 --- a/pkg/rbd/nodeserver.go +++ b/pkg/rbd/nodeserver.go @@ -55,6 +55,8 @@ type stageTransaction struct { isMounted bool // isEncrypted represents if the volume was encrypted or not isEncrypted bool + // devicePath represents the path where rbd device is mapped + devicePath string } // NodeStageVolume mounts the volume to a staging path on the node. @@ -163,7 +165,6 @@ func (ns *NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol volOptions.VolID = volID transaction := stageTransaction{} - devicePath := "" // Stash image details prior to mapping the image (useful during Unstage as it has no // voloptions passed to the RPC as per the CSI spec) @@ -173,7 +174,7 @@ func (ns *NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol } defer func() { if err != nil { - ns.undoStagingTransaction(ctx, req, devicePath, transaction) + ns.undoStagingTransaction(ctx, req, transaction) } }() @@ -207,7 +208,7 @@ func (ns *NodeServer) stageTransaction(ctx context.Context, req *csi.NodeStageVo if err != nil { return transaction, err } - + transaction.devicePath = devicePath klog.V(4).Infof(util.Log(ctx, "rbd image: %s/%s was successfully mapped at %s\n"), req.GetVolumeId(), volOptions.Pool, devicePath) @@ -242,7 +243,7 @@ func (ns *NodeServer) stageTransaction(ctx context.Context, req *csi.NodeStageVo return transaction, err } -func (ns *NodeServer) undoStagingTransaction(ctx context.Context, req *csi.NodeStageVolumeRequest, devicePath string, transaction stageTransaction) { +func (ns *NodeServer) undoStagingTransaction(ctx context.Context, req *csi.NodeStageVolumeRequest, transaction stageTransaction) { var err error stagingTargetPath := getStagingTargetPath(req) @@ -266,10 +267,10 @@ func (ns *NodeServer) undoStagingTransaction(ctx context.Context, req *csi.NodeS volID := req.GetVolumeId() // Unmapping rbd device - if devicePath != "" { - err = detachRBDDevice(ctx, devicePath, volID, transaction.isEncrypted) + if transaction.devicePath != "" { + err = detachRBDDevice(ctx, transaction.devicePath, volID, transaction.isEncrypted) if err != nil { - klog.Errorf(util.Log(ctx, "failed to unmap rbd device: %s for volume %s with error: %v"), devicePath, volID, err) + klog.Errorf(util.Log(ctx, "failed to unmap rbd device: %s for volume %s with error: %v"), transaction.devicePath, volID, err) // continue on failure to delete the stash file, as kubernetes will fail to delete the staging path otherwise } }