rbd: implement GetMirrorSource in manager

implementing GetMirrorSource in manager
to return volume or the volumegroup based
on the replication source, if replication
source is nil return the volume details
for backward compatibility.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna
2024-11-19 11:26:00 +01:00
parent 9575e5ace8
commit 3afb22c8ac
6 changed files with 241 additions and 157 deletions

View File

@ -415,3 +415,7 @@ func (vg *volumeGroup) CreateSnapshots(
return snapshots, nil
}
func (vg *volumeGroup) ToMirror() (types.Mirror, error) {
return volumeGroupMirror{vg}, nil
}

View File

@ -21,6 +21,8 @@ import (
"errors"
"fmt"
"github.com/csi-addons/spec/lib/go/replication"
"github.com/ceph/ceph-csi/internal/journal"
rbd_group "github.com/ceph/ceph-csi/internal/rbd/group"
"github.com/ceph/ceph-csi/internal/rbd/types"
@ -504,6 +506,7 @@ func (mgr *rbdManager) CreateVolumeGroupSnapshot(
return vgs, nil
}
<<<<<<< HEAD
// RegenerateVolumeGroupJournal regenerate the omap data for the volume group.
// This performs the following operations:
// - extracts clusterID and Mons from the cluster mapping
@ -633,4 +636,79 @@ func (mgr *rbdManager) RegenerateVolumeGroupJournal(
groupHandle, vgName, requestName)
return groupHandle, nil
=======
func (mgr *rbdManager) GetMirrorSource(ctx context.Context, reqID string,
rep *replication.ReplicationSource,
) ([]types.Volume, types.Mirror, error) {
switch {
// Backward compatibility: if rep is nil, we assume that the sidecar is still old and
// setting only volumeId not the replication source.
case rep == nil || rep.GetVolume() != nil:
rbdVol, err := mgr.GetVolumeByID(ctx, reqID)
if err != nil {
return nil, nil, fmt.Errorf("failed to get volume by id %q: %w", reqID, err)
}
defer func() {
if err != nil {
rbdVol.Destroy(ctx)
}
}()
var mir types.Mirror
mir, err = rbdVol.ToMirror()
if err != nil {
return nil, nil, fmt.Errorf("failed to convert volume %s to mirror: %w", rbdVol, err)
}
return []types.Volume{rbdVol}, mir, nil
case rep.GetVolumegroup() != nil:
rbdGroup, err := mgr.GetVolumeGroupByID(ctx, reqID)
if err != nil {
return nil, nil, fmt.Errorf("failed to get volume group by id %q: %w", reqID, err)
}
defer func() {
if err != nil {
rbdGroup.Destroy(ctx)
}
}()
var mir types.Mirror
mir, err = rbdGroup.ToMirror()
if err != nil {
return nil, nil, fmt.Errorf("failed to convert volume group %s to mirror: %w", rbdGroup, err)
}
var vols []types.Volume
vols, err = rbdGroup.ListVolumes(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to list volumes in volume group %q: %w", rbdGroup, err)
}
// Get all the volume with connection and return it
volumes := make([]types.Volume, len(vols))
// Destroy connections if there is any error
defer func() {
if err != nil {
for _, vol := range vols {
vol.Destroy(ctx)
}
}
}()
for i, vol := range vols {
var id string
id, err = vol.GetID(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get id for volume %q in group %q: %w", vol, rbdGroup, err)
}
var v types.Volume
v, err = mgr.GetVolumeByID(ctx, id)
if err != nil {
return nil, nil, fmt.Errorf("failed to get volume by id %q in group %q: %w", id, rbdGroup, err)
}
volumes[i] = v
}
return volumes, mir, nil
default:
return nil, nil, errors.New("replication source is not set")
}
>>>>>>> 71b235ff1 (rbd: implement GetMirrorSource in manager)
}

View File

@ -73,4 +73,7 @@ type VolumeGroup interface {
// The Snapshots are crash consistent, and created as a consistency
// group.
CreateSnapshots(ctx context.Context, cr *util.Credentials, name string) ([]Snapshot, error)
// ToMirror converts the VolumeGroup to a Mirror.
ToMirror() (Mirror, error)
}

View File

@ -18,6 +18,8 @@ package types
import (
"context"
"github.com/csi-addons/spec/lib/go/replication"
)
// VolumeResolver can be used to construct a Volume from a CSI VolumeId.
@ -71,4 +73,8 @@ type Manager interface {
// RegenerateVolumeGroupJournal regenerate the omap data for the volume group.
// returns the volume group handle
RegenerateVolumeGroupJournal(ctx context.Context, groupID, requestName string, volumeIds []string) (string, error)
// GetMirrorSource returns the source of the mirror for the given volume or group.
GetMirrorSource(ctx context.Context, volumeID string,
rep *replication.ReplicationSource) ([]Volume, Mirror, error)
}