rbd: configureEncryption() in genSnapFromSnapID()

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2021-03-30 22:08:24 +02:00 committed by mergify[bot]
parent 6fd3f57f40
commit fd5f4dbafd
2 changed files with 28 additions and 8 deletions

View File

@ -302,7 +302,7 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
} }
}() }()
err = cs.createBackingImage(ctx, cr, rbdVol, parentVol, rbdSnap) err = cs.createBackingImage(ctx, cr, req.GetSecrets(), rbdVol, parentVol, rbdSnap)
if err != nil { if err != nil {
if errors.Is(err, ErrFlattenInProgress) { if errors.Is(err, ErrFlattenInProgress) {
return nil, status.Error(codes.Aborted, err.Error()) return nil, status.Error(codes.Aborted, err.Error())
@ -425,7 +425,7 @@ func checkFlatten(ctx context.Context, rbdVol *rbdVolume, cr *util.Credentials)
return nil return nil
} }
func (cs *ControllerServer) createVolumeFromSnapshot(ctx context.Context, cr *util.Credentials, rbdVol *rbdVolume, snapshotID string) error { func (cs *ControllerServer) createVolumeFromSnapshot(ctx context.Context, cr *util.Credentials, secrets map[string]string, rbdVol *rbdVolume, snapshotID string) error {
rbdSnap := &rbdSnapshot{} rbdSnap := &rbdSnapshot{}
if acquired := cs.SnapshotLocks.TryAcquire(snapshotID); !acquired { if acquired := cs.SnapshotLocks.TryAcquire(snapshotID); !acquired {
util.ErrorLog(ctx, util.SnapshotOperationAlreadyExistsFmt, snapshotID) util.ErrorLog(ctx, util.SnapshotOperationAlreadyExistsFmt, snapshotID)
@ -433,7 +433,7 @@ func (cs *ControllerServer) createVolumeFromSnapshot(ctx context.Context, cr *ut
} }
defer cs.SnapshotLocks.Release(snapshotID) defer cs.SnapshotLocks.Release(snapshotID)
err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr) err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr, secrets)
if err != nil { if err != nil {
if errors.Is(err, util.ErrPoolNotFound) { if errors.Is(err, util.ErrPoolNotFound) {
util.ErrorLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err) util.ErrorLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err)
@ -455,7 +455,7 @@ func (cs *ControllerServer) createVolumeFromSnapshot(ctx context.Context, cr *ut
return nil return nil
} }
func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Credentials, rbdVol, parentVol *rbdVolume, rbdSnap *rbdSnapshot) error { func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Credentials, secrets map[string]string, rbdVol, parentVol *rbdVolume, rbdSnap *rbdSnapshot) error {
var err error var err error
var j = &journal.Connection{} var j = &journal.Connection{}
@ -473,7 +473,7 @@ func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Cre
} }
defer cs.OperationLocks.ReleaseRestoreLock(rbdSnap.VolID) defer cs.OperationLocks.ReleaseRestoreLock(rbdSnap.VolID)
err = cs.createVolumeFromSnapshot(ctx, cr, rbdVol, rbdSnap.VolID) err = cs.createVolumeFromSnapshot(ctx, cr, secrets, rbdVol, rbdSnap.VolID)
if err != nil { if err != nil {
return err return err
} }
@ -542,7 +542,7 @@ func checkContentSource(ctx context.Context, req *csi.CreateVolumeRequest, cr *u
return nil, nil, status.Errorf(codes.NotFound, "volume Snapshot ID cannot be empty") return nil, nil, status.Errorf(codes.NotFound, "volume Snapshot ID cannot be empty")
} }
rbdSnap := &rbdSnapshot{} rbdSnap := &rbdSnapshot{}
if err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr); err != nil { if err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr, req.GetSecrets()); err != nil {
util.ErrorLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err) util.ErrorLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err)
if !errors.Is(err, ErrSnapNotFound) { if !errors.Is(err, ErrSnapNotFound) {
return nil, nil, status.Error(codes.Internal, err.Error()) return nil, nil, status.Error(codes.Internal, err.Error())
@ -1000,7 +1000,7 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
defer cs.OperationLocks.ReleaseDeleteLock(snapshotID) defer cs.OperationLocks.ReleaseDeleteLock(snapshotID)
rbdSnap := &rbdSnapshot{} rbdSnap := &rbdSnapshot{}
if err = genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr); err != nil { if err = genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr, req.GetSecrets()); err != nil {
// if error is ErrPoolNotFound, the pool is already deleted we dont // if error is ErrPoolNotFound, the pool is already deleted we dont
// need to worry about deleting snapshot or omap data, return success // need to worry about deleting snapshot or omap data, return success
if errors.Is(err, util.ErrPoolNotFound) { if errors.Is(err, util.ErrPoolNotFound) {

View File

@ -723,7 +723,7 @@ func (rv *rbdVolume) checkImageChainHasFeature(ctx context.Context, feature uint
// genSnapFromSnapID generates a rbdSnapshot structure from the provided identifier, updating // genSnapFromSnapID generates a rbdSnapshot structure from the provided identifier, updating
// the structure with elements from on-disk snapshot metadata as well. // the structure with elements from on-disk snapshot metadata as well.
func genSnapFromSnapID(ctx context.Context, rbdSnap *rbdSnapshot, snapshotID string, cr *util.Credentials) error { func genSnapFromSnapID(ctx context.Context, rbdSnap *rbdSnapshot, snapshotID string, cr *util.Credentials, secrets map[string]string) error {
var ( var (
options map[string]string options map[string]string
vi util.CSIIdentifier vi util.CSIIdentifier
@ -774,6 +774,7 @@ func genSnapFromSnapID(ctx context.Context, rbdSnap *rbdSnapshot, snapshotID str
rbdSnap.RbdImageName = imageAttributes.SourceName rbdSnap.RbdImageName = imageAttributes.SourceName
rbdSnap.RbdSnapName = imageAttributes.ImageName rbdSnap.RbdSnapName = imageAttributes.ImageName
rbdSnap.ReservedID = vi.ObjectUUID rbdSnap.ReservedID = vi.ObjectUUID
rbdSnap.Owner = imageAttributes.Owner
// convert the journal pool ID to name, for use in DeleteSnapshot cases // convert the journal pool ID to name, for use in DeleteSnapshot cases
if imageAttributes.JournalPoolID != util.InvalidPoolID { if imageAttributes.JournalPoolID != util.InvalidPoolID {
rbdSnap.JournalPool, err = util.GetPoolName(rbdSnap.Monitors, cr, imageAttributes.JournalPoolID) rbdSnap.JournalPool, err = util.GetPoolName(rbdSnap.Monitors, cr, imageAttributes.JournalPoolID)
@ -783,6 +784,25 @@ func genSnapFromSnapID(ctx context.Context, rbdSnap *rbdSnapshot, snapshotID str
} }
} }
err = rbdSnap.Connect(cr)
defer func() {
if err != nil {
rbdSnap.Destroy()
}
}()
if err != nil {
return fmt.Errorf("failed to connect to %q: %w",
rbdSnap.String(), err)
}
if imageAttributes.KmsID != "" {
err = rbdSnap.configureEncryption(imageAttributes.KmsID, secrets)
if err != nil {
return fmt.Errorf("failed to configure encryption for "+
"%q: %w", rbdSnap.String(), err)
}
}
return err return err
} }