From 76c0c6239cfaa63a0a4852ea205f186723b04600 Mon Sep 17 00:00:00 2001
From: Niels de Vos <ndevos@ibm.com>
Date: Mon, 16 Sep 2024 18:24:58 +0200
Subject: [PATCH] rbd: prevent panic when using rbdImage that is not connected

When an `rbdVolume` or `rbdSnapshot` is not connected with credentials
to the Ceph cluster, operations may try to get the IOContext which then
causes a panic.

Signed-off-by: Niels de Vos <ndevos@ibm.com>
---
 internal/rbd/rbd_util.go | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go
index 703809c953e..e363e391cc1 100644
--- a/internal/rbd/rbd_util.go
+++ b/internal/rbd/rbd_util.go
@@ -468,6 +468,10 @@ func (ri *rbdImage) openIoctx() error {
 		return nil
 	}
 
+	if ri.conn == nil {
+		return fmt.Errorf("can not get IOContext of unconnected image %q", ri)
+	}
+
 	ioctx, err := ri.conn.GetIoctx(ri.Pool)
 	if err != nil {
 		// GetIoctx() can return util.ErrPoolNotFound