From e557438f876ae736eae250dbc53996e0e6274aff Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Mon, 19 Aug 2019 10:40:03 +0530 Subject: [PATCH] unmap rbd image if connection timeout. Sometime rbd images are mapped even if the connection timeout error occurs, this will try to unmap if the received error message is connection timeout.This will fix stale maps and rbd image deletion issue Signed-off-by: Madhu Rajanna --- pkg/rbd/rbd_attach.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/rbd/rbd_attach.go b/pkg/rbd/rbd_attach.go index a2f982ef9..96bd58e6f 100644 --- a/pkg/rbd/rbd_attach.go +++ b/pkg/rbd/rbd_attach.go @@ -44,6 +44,7 @@ const ( // NOTE: When using devicePath instead of imageSpec, the error strings are different rbdUnmapCmdkRbdMissingMap = "rbd: %s: not a mapped image or snapshot" rbdUnmapCmdNbdMissingMap = "rbd-nbd: %s is not mapped" + rbdMapConnectionTimeout = "Connection timed out" ) var hasNBD = false @@ -193,6 +194,7 @@ func attachRBDImage(volOptions *rbdVolume, cr *util.Credentials) (string, error) } func createPath(volOpt *rbdVolume, cr *util.Credentials) (string, error) { + isNbd := false image := volOpt.RbdImageName imagePath := fmt.Sprintf("%s/%s", volOpt.Pool, image) @@ -209,6 +211,7 @@ func createPath(volOpt *rbdVolume, cr *util.Credentials) (string, error) { // Choose access protocol accessType := accessTypeKRbd if volOpt.Mounter == rbdTonbd && hasNBD { + isNbd = true accessType = accessTypeNbd } @@ -219,6 +222,13 @@ func createPath(volOpt *rbdVolume, cr *util.Credentials) (string, error) { output, err := execCommand(rbd, mapOptions) if err != nil { klog.Warningf("rbd: map error %v, rbd output: %s", err, string(output)) + // unmap rbd image if connection timeout + if strings.Contains(err.Error(), rbdMapConnectionTimeout) { + detErr := detachRBDImageOrDeviceSpec(imagePath, true, isNbd) + if detErr != nil { + klog.Warningf("rbd: %s unmap error %v", imagePath, detErr) + } + } return "", fmt.Errorf("rbd: map failed %v, rbd output: %s", err, string(output)) } devicePath := strings.TrimSuffix(string(output), "\n")