rbd: unmap rbd image if the mounting fails

There is a bug in current code where the devicePath
is always empty and the rbd image unmap never
happens if nodeplugin fails to mount the rbd image
to the stagingpath.
This is a fix to unmap the rbd image if some issue
occurs after rbd image is mapped.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
(cherry picked from commit 2f7e51076b)
This commit is contained in:
Madhu Rajanna 2020-05-18 17:13:15 +05:30 committed by Madhu Rajanna
parent 4c6d384991
commit c2d0cc9ec1

View File

@ -55,6 +55,8 @@ type stageTransaction struct {
isMounted bool isMounted bool
// isEncrypted represents if the volume was encrypted or not // isEncrypted represents if the volume was encrypted or not
isEncrypted bool isEncrypted bool
// devicePath represents the path where rbd device is mapped
devicePath string
} }
// NodeStageVolume mounts the volume to a staging path on the node. // 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 volOptions.VolID = volID
transaction := stageTransaction{} transaction := stageTransaction{}
devicePath := ""
// Stash image details prior to mapping the image (useful during Unstage as it has no // 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) // 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() { defer func() {
if err != nil { 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 { if err != nil {
return transaction, err return transaction, err
} }
transaction.devicePath = devicePath
klog.V(4).Infof(util.Log(ctx, "rbd image: %s/%s was successfully mapped at %s\n"), klog.V(4).Infof(util.Log(ctx, "rbd image: %s/%s was successfully mapped at %s\n"),
req.GetVolumeId(), volOptions.Pool, devicePath) req.GetVolumeId(), volOptions.Pool, devicePath)
@ -242,7 +243,7 @@ func (ns *NodeServer) stageTransaction(ctx context.Context, req *csi.NodeStageVo
return transaction, err 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 var err error
stagingTargetPath := getStagingTargetPath(req) stagingTargetPath := getStagingTargetPath(req)
@ -266,10 +267,10 @@ func (ns *NodeServer) undoStagingTransaction(ctx context.Context, req *csi.NodeS
volID := req.GetVolumeId() volID := req.GetVolumeId()
// Unmapping rbd device // Unmapping rbd device
if devicePath != "" { if transaction.devicePath != "" {
err = detachRBDDevice(ctx, devicePath, volID, transaction.isEncrypted) err = detachRBDDevice(ctx, transaction.devicePath, volID, transaction.isEncrypted)
if err != nil { 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 // continue on failure to delete the stash file, as kubernetes will fail to delete the staging path otherwise
} }
} }