Skip to content

Commit

Permalink
rbd: use VolumeGroup.GetName() to check if it exists already
Browse files Browse the repository at this point in the history
When repeated CreateVolumeGroup requests come in, it is needed to check
if the VolumeGroup already exists in the backend. If it does, there is
no need to create it again (and fail).

Signed-off-by: Niels de Vos <ndevos@ibm.com>
  • Loading branch information
nixpanic committed Jul 19, 2024
1 parent eb2bd46 commit 7a758aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
13 changes: 10 additions & 3 deletions internal/rbd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,16 @@ func (mgr *rbdManager) CreateVolumeGroup(ctx context.Context, name string) (type
return nil, fmt.Errorf("failed to get volume group %q at cluster %q: %w", name, clusterID, err)
}

err = vg.Create(ctx, vgName)
if err != nil {
return nil, fmt.Errorf("failed to create volume group %q: %w", name, err)
// check if the volume group exists in the backend
existingName, err := vg.GetName(ctx)
if err != nil {
// the volume group does not exist yet
err = vg.Create(ctx, vgName)
if err != nil {
return nil, fmt.Errorf("failed to create volume group %q: %w", name, err)
}
} else if existingName != vgName {
return nil, fmt.Errorf("volume group id %q has a name mismatch, expected %q, not %q", name, vgName, existingName)
}

// TODO: create the volume group in the journal
Expand Down
23 changes: 20 additions & 3 deletions internal/rbd_group/volume_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ var (

// GetVolumeGroup initializes a new VolumeGroup object that can be used
// to manage an `rbd group`.
// If the .GetName() function returns an error, the VolumeGroup does not exist
// yet. It is needed to call .Create() in that case first.
func GetVolumeGroup(
ctx context.Context,
id string,
Expand Down Expand Up @@ -107,13 +109,22 @@ func GetVolumeGroup(

attrs, err := j.GetVolumeGroupAttributes(ctx, pool, csiID.ObjectUUID)
if err != nil {
return nil, fmt.Errorf("failed to get attributes for volume group id %q: %w", id, err)
if !errors.Is(err, util.ErrKeyNotFound) && !errors.Is(err, util.ErrPoolNotFound) {
return nil, fmt.Errorf("failed to get attributes for volume group id %q: %w", id, err)
}

attrs = &journal.VolumeGroupAttributes{}
}

var volumes []types.Volume
for volID := range attrs.VolumeMap {
vol, err := volumeResolver.GetVolumeByID(ctx, volID)
if err != nil {
// free the previously allocated volumes
for _, v := range volumes {
v.Destroy(ctx)
}

return nil, fmt.Errorf("failed to get attributes for volume group id %q: %w", id, err)
}

Expand Down Expand Up @@ -285,18 +296,24 @@ func (vg *volumeGroup) Create(ctx context.Context, name string) error {
return fmt.Errorf("failed to create volume group %q: %w", name, err)
}

log.DebugLog(ctx, "volume group %q has been created", name)
vg.name = name
log.DebugLog(ctx, "volume group %q has been created", vg)

return nil
}

func (vg *volumeGroup) Delete(ctx context.Context) error {
name, err := vg.GetName(ctx)
if err != nil {
return err
}

ioctx, err := vg.GetIOContext(ctx)
if err != nil {
return err
}

err = librbd.GroupRemove(ioctx, vg.name)
err = librbd.GroupRemove(ioctx, name)
if err != nil {
return fmt.Errorf("failed to remove volume group %q: %w", vg, err)
}
Expand Down

0 comments on commit 7a758aa

Please sign in to comment.