From 9bea3feff12eda8350f6b3dcb106c0dc3d01810e Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 4 Oct 2024 15:40:33 +0200 Subject: [PATCH] rbd: add manager GetSnapshotByID and SnapshotResolver interface A (CSI) VolumeGroupSnapshot object contains references to Snapshot IDs (or CSI Snapshot handles). In order to work with a VolumeGroupSnapshot struct, the Snapshot IDs need to be resolved into rbdSnapshot structs. Signed-off-by: Niels de Vos --- internal/rbd/manager.go | 25 +++++++++++++++++++++++++ internal/rbd/types/manager.go | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/internal/rbd/manager.go b/internal/rbd/manager.go index 7eaf536c2..1830df79b 100644 --- a/internal/rbd/manager.go +++ b/internal/rbd/manager.go @@ -139,6 +139,31 @@ func (mgr *rbdManager) GetVolumeByID(ctx context.Context, id string) (types.Volu return volume, nil } +func (mgr *rbdManager) GetSnapshotByID(ctx context.Context, id string) (types.Snapshot, error) { + creds, err := mgr.getCredentials() + if err != nil { + return nil, err + } + + snapshot, err := genSnapFromSnapID(ctx, id, creds, mgr.secrets) + if err != nil { + switch { + case errors.Is(err, ErrImageNotFound): + err = fmt.Errorf("volume %s not found: %w", id, err) + + return nil, err + case errors.Is(err, util.ErrPoolNotFound): + err = fmt.Errorf("pool %s not found for %s: %w", snapshot.Pool, id, err) + + return nil, err + default: + return nil, fmt.Errorf("failed to get volume from id %q: %w", id, err) + } + } + + return snapshot, nil +} + func (mgr *rbdManager) GetVolumeGroupByID(ctx context.Context, id string) (types.VolumeGroup, error) { creds, err := mgr.getCredentials() if err != nil { diff --git a/internal/rbd/types/manager.go b/internal/rbd/types/manager.go index 2e9521570..2d9eb36e3 100644 --- a/internal/rbd/types/manager.go +++ b/internal/rbd/types/manager.go @@ -26,6 +26,12 @@ type VolumeResolver interface { GetVolumeByID(ctx context.Context, id string) (Volume, error) } +// SnapshotResolver can be used to construct a Snapshot from a CSI SnapshotId. +type SnapshotResolver interface { + // GetSnapshotByID uses the CSI SnapshotId to resolve the returned Snapshot. + GetSnapshotByID(ctx context.Context, id string) (Snapshot, error) +} + // Manager provides a way for other packages to get Volumes and VolumeGroups. // It handles the operations on the backend, and makes sure the journal // reflects the expected state. @@ -33,6 +39,9 @@ type Manager interface { // VolumeResolver is fully implemented by the Manager. VolumeResolver + // SnapshotResolver is fully implemented by the Manager. + SnapshotResolver + // Destroy frees all resources that the Manager allocated. Destroy(ctx context.Context)