Skip to content

Commit

Permalink
cleanup: do not pass an empty snapshot to genSnapFromSnapID()
Browse files Browse the repository at this point in the history
Just like GenVolFromVolID() the genSnapFromSnapID() function can return
a snapshot. There is no need to allocated an empty snapshot and pass
that to the genSnapFromSnapID() function.

Signed-off-by: Niels de Vos <[email protected]>
  • Loading branch information
nixpanic committed Mar 21, 2024
1 parent cd18490 commit b09f947
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
11 changes: 5 additions & 6 deletions internal/rbd/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,14 @@ func (cs *ControllerServer) createVolumeFromSnapshot(
rbdVol *rbdVolume,
snapshotID string,
) error {
rbdSnap := &rbdSnapshot{}
if acquired := cs.SnapshotLocks.TryAcquire(snapshotID); !acquired {
log.ErrorLog(ctx, util.SnapshotOperationAlreadyExistsFmt, snapshotID)

return status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, snapshotID)
}
defer cs.SnapshotLocks.Release(snapshotID)

err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr, secrets)
rbdSnap, err := genSnapFromSnapID(ctx, snapshotID, cr, secrets)
if err != nil {
if errors.Is(err, util.ErrPoolNotFound) {
log.ErrorLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err)
Expand Down Expand Up @@ -776,8 +775,8 @@ func checkContentSource(
if snapshotID == "" {
return nil, nil, status.Errorf(codes.NotFound, "volume Snapshot ID cannot be empty")
}
rbdSnap := &rbdSnapshot{}
if err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr, req.GetSecrets()); err != nil {
rbdSnap, err := genSnapFromSnapID(ctx, snapshotID, cr, req.GetSecrets())
if err != nil {
log.ErrorLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err)
if !errors.Is(err, ErrSnapNotFound) {
return nil, nil, status.Error(codes.Internal, err.Error())
Expand Down Expand Up @@ -1416,8 +1415,8 @@ func (cs *ControllerServer) DeleteSnapshot(
}
defer cs.OperationLocks.ReleaseDeleteLock(snapshotID)

rbdSnap := &rbdSnapshot{}
if err = genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr, req.GetSecrets()); err != nil {
rbdSnap, err := genSnapFromSnapID(ctx, snapshotID, cr, req.GetSecrets())
if err != nil {
// if error is ErrPoolNotFound, the pool is already deleted we don't
// need to worry about deleting snapshot or omap data, return success
if errors.Is(err, util.ErrPoolNotFound) {
Expand Down
31 changes: 15 additions & 16 deletions internal/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,52 +951,51 @@ func (ri *rbdImage) checkImageChainHasFeature(ctx context.Context, feature uint6
// the structure with elements from on-disk snapshot metadata as well.
func genSnapFromSnapID(
ctx context.Context,
rbdSnap *rbdSnapshot,
snapshotID string,
cr *util.Credentials,
secrets map[string]string,
) error {
) (*rbdSnapshot, error) {
var vi util.CSIIdentifier

rbdSnap.VolID = snapshotID
rbdSnap := &rbdSnapshot{}

err := vi.DecomposeCSIID(rbdSnap.VolID)
if err != nil {
log.ErrorLog(ctx, "error decoding snapshot ID (%s) (%s)", err, rbdSnap.VolID)

return err
return nil, err
}

rbdSnap.VolID = snapshotID
rbdSnap.ClusterID = vi.ClusterID

rbdSnap.Monitors, _, err = util.GetMonsAndClusterID(ctx, rbdSnap.ClusterID, false)
if err != nil {
log.ErrorLog(ctx, "failed getting mons (%s)", err)

return err
return nil, err
}

rbdSnap.Pool, err = util.GetPoolName(rbdSnap.Monitors, cr, vi.LocationID)
if err != nil {
return err
return nil, err
}
rbdSnap.JournalPool = rbdSnap.Pool

rbdSnap.RadosNamespace, err = util.GetRadosNamespace(util.CsiConfigFile, rbdSnap.ClusterID)
if err != nil {
return err
return nil, err
}

j, err := snapJournal.Connect(rbdSnap.Monitors, rbdSnap.RadosNamespace, cr)
if err != nil {
return err
return nil, err
}
defer j.Destroy()

imageAttributes, err := j.GetImageAttributes(
ctx, rbdSnap.Pool, vi.ObjectUUID, true)
if err != nil {
return err
return nil, err
}
rbdSnap.ImageID = imageAttributes.ImageID
rbdSnap.RequestName = imageAttributes.RequestName
Expand All @@ -1009,7 +1008,7 @@ func genSnapFromSnapID(
rbdSnap.JournalPool, err = util.GetPoolName(rbdSnap.Monitors, cr, imageAttributes.JournalPoolID)
if err != nil {
// TODO: If pool is not found we may leak the image (as DeleteSnapshot will return success)
return err
return nil, err
}
}

Expand All @@ -1020,31 +1019,31 @@ func genSnapFromSnapID(
}
}()
if err != nil {
return fmt.Errorf("failed to connect to %q: %w",
return nil, fmt.Errorf("failed to connect to %q: %w",
rbdSnap, err)
}

if imageAttributes.KmsID != "" && imageAttributes.EncryptionType == util.EncryptionTypeBlock {
err = rbdSnap.configureBlockEncryption(imageAttributes.KmsID, secrets)
if err != nil {
return fmt.Errorf("failed to configure block encryption for "+
return nil, fmt.Errorf("failed to configure block encryption for "+
"%q: %w", rbdSnap, err)
}
}
if imageAttributes.KmsID != "" && imageAttributes.EncryptionType == util.EncryptionTypeFile {
err = rbdSnap.configureFileEncryption(ctx, imageAttributes.KmsID, secrets)
if err != nil {
return fmt.Errorf("failed to configure file encryption for "+
return nil, fmt.Errorf("failed to configure file encryption for "+
"%q: %w", rbdSnap, err)
}
}

err = updateSnapshotDetails(rbdSnap)
if err != nil {
return fmt.Errorf("failed to update snapshot details for %q: %w", rbdSnap, err)
return nil, fmt.Errorf("failed to update snapshot details for %q: %w", rbdSnap, err)
}

return err
return rbdSnap, err
}

// updateSnapshotDetails will copy the details from the rbdVolume to the
Expand Down

0 comments on commit b09f947

Please sign in to comment.