rbd: setup connection in genVolFromVolID()

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-03-17 14:39:35 +01:00 committed by mergify[bot]
parent 12130123ac
commit ea51b04017
2 changed files with 22 additions and 17 deletions

View File

@ -389,9 +389,8 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
} }
defer cs.VolumeLocks.Release(volumeID) defer cs.VolumeLocks.Release(volumeID)
rbdVol := &rbdVolume{} rbdVol, err := genVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer rbdVol.Destroy() if err != nil {
if err = genVolFromVolID(ctx, rbdVol, volumeID, cr, req.GetSecrets()); err != nil {
if _, ok := err.(util.ErrPoolNotFound); ok { if _, ok := err.(util.ErrPoolNotFound); ok {
klog.Warningf(util.Log(ctx, "failed to get backend volume for %s: %v"), volumeID, err) klog.Warningf(util.Log(ctx, "failed to get backend volume for %s: %v"), volumeID, err)
return &csi.DeleteVolumeResponse{}, nil return &csi.DeleteVolumeResponse{}, nil
@ -436,6 +435,7 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
} }
return &csi.DeleteVolumeResponse{}, nil return &csi.DeleteVolumeResponse{}, nil
} }
defer rbdVol.Destroy()
// lock out parallel create requests against the same volume name as we // lock out parallel create requests against the same volume name as we
// cleanup the image and associated omaps for the same // cleanup the image and associated omaps for the same
@ -506,8 +506,7 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
defer cr.DeleteCredentials() defer cr.DeleteCredentials()
// Fetch source volume information // Fetch source volume information
rbdVol := new(rbdVolume) rbdVol, err := genVolFromVolID(ctx, req.GetSourceVolumeId(), cr, req.GetSecrets())
err = genVolFromVolID(ctx, rbdVol, req.GetSourceVolumeId(), cr, req.GetSecrets())
if err != nil { if err != nil {
if _, ok := err.(ErrImageNotFound); ok { if _, ok := err.(ErrImageNotFound); ok {
return nil, status.Errorf(codes.NotFound, "source Volume ID %s not found", req.GetSourceVolumeId()) return nil, status.Errorf(codes.NotFound, "source Volume ID %s not found", req.GetSourceVolumeId())
@ -519,6 +518,7 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
} }
return nil, status.Errorf(codes.Internal, err.Error()) return nil, status.Errorf(codes.Internal, err.Error())
} }
defer rbdVol.Destroy()
// TODO: re-encrypt snapshot with a new passphrase // TODO: re-encrypt snapshot with a new passphrase
if rbdVol.Encrypted { if rbdVol.Encrypted {
@ -780,9 +780,7 @@ func (cs *ControllerServer) ControllerExpandVolume(ctx context.Context, req *csi
} }
defer cr.DeleteCredentials() defer cr.DeleteCredentials()
rbdVol := &rbdVolume{} rbdVol, err := genVolFromVolID(ctx, volID, cr, req.GetSecrets())
defer rbdVol.Destroy()
err = genVolFromVolID(ctx, rbdVol, volID, cr, req.GetSecrets())
if err != nil { if err != nil {
if _, ok := err.(ErrImageNotFound); ok { if _, ok := err.(ErrImageNotFound); ok {
return nil, status.Errorf(codes.NotFound, "volume ID %s not found", volID) return nil, status.Errorf(codes.NotFound, "volume ID %s not found", volID)
@ -795,6 +793,7 @@ func (cs *ControllerServer) ControllerExpandVolume(ctx context.Context, req *csi
return nil, status.Errorf(codes.Internal, err.Error()) return nil, status.Errorf(codes.Internal, err.Error())
} }
defer rbdVol.Destroy()
if rbdVol.Encrypted { if rbdVol.Encrypted {
return nil, status.Errorf(codes.InvalidArgument, "encrypted volumes do not support resize (%s/%s)", return nil, status.Errorf(codes.InvalidArgument, "encrypted volumes do not support resize (%s/%s)",

View File

@ -389,21 +389,22 @@ func genSnapFromSnapID(ctx context.Context, rbdSnap *rbdSnapshot, snapshotID str
// genVolFromVolID generates a rbdVolume structure from the provided identifier, updating // genVolFromVolID generates a rbdVolume structure from the provided identifier, updating
// the structure with elements from on-disk image metadata as well // the structure with elements from on-disk image metadata as well
func genVolFromVolID(ctx context.Context, rbdVol *rbdVolume, volumeID string, cr *util.Credentials, secrets map[string]string) error { func genVolFromVolID(ctx context.Context, volumeID string, cr *util.Credentials, secrets map[string]string) (*rbdVolume, error) {
var ( var (
options map[string]string options map[string]string
vi util.CSIIdentifier vi util.CSIIdentifier
rbdVol *rbdVolume
) )
options = make(map[string]string) options = make(map[string]string)
// rbdVolume fields that are not filled up in this function are: // rbdVolume fields that are not filled up in this function are:
// Mounter, MultiNodeWritable // Mounter, MultiNodeWritable
rbdVol.VolID = volumeID rbdVol = &rbdVolume{VolID: volumeID}
err := vi.DecomposeCSIID(rbdVol.VolID) err := vi.DecomposeCSIID(rbdVol.VolID)
if err != nil { if err != nil {
err = fmt.Errorf("error decoding volume ID (%s) (%s)", err, rbdVol.VolID) err = fmt.Errorf("error decoding volume ID (%s) (%s)", err, rbdVol.VolID)
return ErrInvalidVolID{err} return nil, ErrInvalidVolID{err}
} }
rbdVol.ClusterID = vi.ClusterID rbdVol.ClusterID = vi.ClusterID
@ -411,26 +412,31 @@ func genVolFromVolID(ctx context.Context, rbdVol *rbdVolume, volumeID string, cr
rbdVol.Monitors, _, err = getMonsAndClusterID(ctx, options) rbdVol.Monitors, _, err = getMonsAndClusterID(ctx, options)
if err != nil { if err != nil {
return err return nil, err
} }
rbdVol.Pool, err = util.GetPoolName(ctx, rbdVol.Monitors, cr, vi.LocationID) rbdVol.Pool, err = util.GetPoolName(ctx, rbdVol.Monitors, cr, vi.LocationID)
if err != nil { if err != nil {
return err return nil, err
}
err = rbdVol.Connect(cr)
if err != nil {
return nil, err
} }
rbdVol.JournalPool = rbdVol.Pool rbdVol.JournalPool = rbdVol.Pool
imageAttributes, err := volJournal.GetImageAttributes(ctx, rbdVol.Monitors, cr, imageAttributes, err := volJournal.GetImageAttributes(ctx, rbdVol.Monitors, cr,
rbdVol.Pool, vi.ObjectUUID, false) rbdVol.Pool, vi.ObjectUUID, false)
if err != nil { if err != nil {
return err return nil, err
} }
if imageAttributes.KmsID != "" { if imageAttributes.KmsID != "" {
rbdVol.Encrypted = true rbdVol.Encrypted = true
rbdVol.KMS, err = util.GetKMS(imageAttributes.KmsID, secrets) rbdVol.KMS, err = util.GetKMS(imageAttributes.KmsID, secrets)
if err != nil { if err != nil {
return err return nil, err
} }
} }
rbdVol.RequestName = imageAttributes.RequestName rbdVol.RequestName = imageAttributes.RequestName
@ -441,13 +447,13 @@ func genVolFromVolID(ctx context.Context, rbdVol *rbdVolume, volumeID string, cr
rbdVol.JournalPool, err = util.GetPoolName(ctx, rbdVol.Monitors, cr, imageAttributes.JournalPoolID) rbdVol.JournalPool, err = util.GetPoolName(ctx, rbdVol.Monitors, cr, imageAttributes.JournalPoolID)
if err != nil { if err != nil {
// TODO: If pool is not found we may leak the image (as DeleteVolume will return success) // TODO: If pool is not found we may leak the image (as DeleteVolume will return success)
return err return nil, err
} }
} }
err = updateVolWithImageInfo(ctx, rbdVol, cr) err = updateVolWithImageInfo(ctx, rbdVol, cr)
return err return rbdVol, err
} }
func execCommand(command string, args []string) ([]byte, error) { func execCommand(command string, args []string) ([]byte, error) {