From 7a18e68a6e8b5f195028e53ff51404c3a70dafc8 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 8 May 2020 16:10:18 +0200 Subject: [PATCH] rbd: add rbdVolume.open() to get access to an image Signed-off-by: Niels de Vos --- internal/rbd/rbd_util.go | 21 +++++++++++++++++++++ internal/util/connection.go | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index 96c9f18b3..b15d93b61 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -200,6 +200,27 @@ func createImage(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) er return nil } +// open the rbdVolume after it has been connected. +// ErrPoolNotFound or ErrImageNotFound are returned in case the pool or image +// can not be found, other errors will contain more details about other issues +// (permission denied, ...) and are expected to relate to configuration issues. +func (rv *rbdVolume) open() (*librbd.Image, error) { + ioctx, err := rv.conn.GetIoctx(rv.Pool) + if err != nil { + // GetIoctx() can return util.ErrPoolNotFound + return nil, err + } + + image, err := librbd.OpenImage(ioctx, rv.RbdImageName, librbd.NoSnapshot) + if err != nil { + if err == librbd.ErrNotFound { + err = ErrImageNotFound{rv.RbdImageName, err} + } + return nil, err + } + return image, nil +} + // rbdStatus checks if there is watcher on the image. // It returns true if there is a watcher on the image, otherwise returns false. func rbdStatus(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) (bool, string, error) { diff --git a/internal/util/connection.go b/internal/util/connection.go index 4d3cfd68e..d048bcb87 100644 --- a/internal/util/connection.go +++ b/internal/util/connection.go @@ -71,7 +71,13 @@ func (cc *ClusterConnection) GetIoctx(pool string) (*rados.IOContext, error) { ioctx, err := cc.conn.OpenIOContext(pool) if err != nil { - return nil, errors.Wrapf(err, "failed to open IOContext for pool %s", pool) + // ErrNotFound indicates the Pool was not found + if err == rados.ErrNotFound { + err = ErrPoolNotFound{pool, err} + } else { + err = errors.Wrapf(err, "failed to open IOContext for pool %s", pool) + } + return nil, err } return ioctx, nil