mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-10 00:10:20 +00:00
ef14ea7723
Currently, as a workaround, we are calling the resize volume on the cloned, restore volumes to adjust the cloned, restored volumes. With this fix, we are calling the resize volume only if there is a size mismatch with requested and the volume from which the new volume needs to be created. Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
264 lines
7.5 KiB
Go
264 lines
7.5 KiB
Go
/*
|
|
Copyright 2020 The Ceph-CSI Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package core
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
|
|
fsutil "github.com/ceph/ceph-csi/internal/cephfs/util"
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
|
)
|
|
|
|
// cephFSCloneState describes the status of the clone.
|
|
type cephFSCloneState string
|
|
|
|
const (
|
|
// cephFSCloneError indicates that fetching the clone state returned an error.
|
|
cephFSCloneError = cephFSCloneState("")
|
|
// cephFSCloneFailed indicates that clone is in failed state.
|
|
cephFSCloneFailed = cephFSCloneState("failed")
|
|
// cephFSClonePending indicates that clone is in pending state.
|
|
cephFSClonePending = cephFSCloneState("pending")
|
|
// cephFSCloneInprogress indicates that clone is in in-progress state.
|
|
cephFSCloneInprogress = cephFSCloneState("in-progress")
|
|
// cephFSCloneComplete indicates that clone is in complete state.
|
|
cephFSCloneComplete = cephFSCloneState("complete")
|
|
|
|
// SnapshotIsProtected string indicates that the snapshot is currently protected.
|
|
SnapshotIsProtected = "yes"
|
|
)
|
|
|
|
// toError checks the state of the clone if it's not cephFSCloneComplete.
|
|
func (cs cephFSCloneState) toError() error {
|
|
switch cs {
|
|
case cephFSCloneComplete:
|
|
return nil
|
|
case cephFSCloneError:
|
|
return cerrors.ErrInvalidClone
|
|
case cephFSCloneInprogress:
|
|
return cerrors.ErrCloneInProgress
|
|
case cephFSClonePending:
|
|
return cerrors.ErrClonePending
|
|
case cephFSCloneFailed:
|
|
return cerrors.ErrCloneFailed
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CreateCloneFromSubvolume(
|
|
ctx context.Context,
|
|
volID, cloneID fsutil.VolumeID,
|
|
volOpt,
|
|
parentvolOpt *VolumeOptions) error {
|
|
snapshotID := cloneID
|
|
err := parentvolOpt.CreateSnapshot(ctx, snapshotID, volID)
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "failed to create snapshot %s %v", snapshotID, err)
|
|
|
|
return err
|
|
}
|
|
var (
|
|
// if protectErr is not nil we will delete the snapshot as the protect fails
|
|
protectErr error
|
|
// if cloneErr is not nil we will unprotect the snapshot and delete the snapshot
|
|
cloneErr error
|
|
)
|
|
defer func() {
|
|
if protectErr != nil {
|
|
err = parentvolOpt.DeleteSnapshot(ctx, snapshotID, volID)
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
|
}
|
|
}
|
|
|
|
if cloneErr != nil {
|
|
if err = volOpt.PurgeVolume(ctx, cloneID, true); err != nil {
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", cloneID, err)
|
|
}
|
|
if err = parentvolOpt.UnprotectSnapshot(ctx, snapshotID, volID); err != nil {
|
|
// In case the snap is already unprotected we get ErrSnapProtectionExist error code
|
|
// in that case we are safe and we could discard this error and we are good to go
|
|
// ahead with deletion
|
|
if !errors.Is(err, cerrors.ErrSnapProtectionExist) {
|
|
log.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapshotID, err)
|
|
}
|
|
}
|
|
if err = parentvolOpt.DeleteSnapshot(ctx, snapshotID, volID); err != nil {
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
|
}
|
|
}
|
|
}()
|
|
protectErr = parentvolOpt.ProtectSnapshot(ctx, snapshotID, volID)
|
|
if protectErr != nil {
|
|
log.ErrorLog(ctx, "failed to protect snapshot %s %v", snapshotID, protectErr)
|
|
|
|
return protectErr
|
|
}
|
|
|
|
cloneErr = parentvolOpt.cloneSnapshot(ctx, volID, snapshotID, cloneID, volOpt)
|
|
if cloneErr != nil {
|
|
log.ErrorLog(ctx, "failed to clone snapshot %s %s to %s %v", volID, snapshotID, cloneID, cloneErr)
|
|
|
|
return cloneErr
|
|
}
|
|
|
|
cloneState, cloneErr := volOpt.getCloneState(ctx, cloneID)
|
|
if cloneErr != nil {
|
|
log.ErrorLog(ctx, "failed to get clone state: %v", cloneErr)
|
|
|
|
return cloneErr
|
|
}
|
|
|
|
if cloneState != cephFSCloneComplete {
|
|
log.ErrorLog(ctx, "clone %s did not complete: %v", cloneID, cloneState.toError())
|
|
|
|
return cloneState.toError()
|
|
}
|
|
|
|
err = volOpt.ExpandVolume(ctx, cloneID, volOpt.Size)
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "failed to expand volume %s: %v", cloneID, err)
|
|
|
|
return err
|
|
}
|
|
|
|
// As we completed clone, remove the intermediate snap
|
|
if err = parentvolOpt.UnprotectSnapshot(ctx, snapshotID, volID); err != nil {
|
|
// In case the snap is already unprotected we get ErrSnapProtectionExist error code
|
|
// in that case we are safe and we could discard this error and we are good to go
|
|
// ahead with deletion
|
|
if !errors.Is(err, cerrors.ErrSnapProtectionExist) {
|
|
log.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapshotID, err)
|
|
|
|
return err
|
|
}
|
|
}
|
|
if err = parentvolOpt.DeleteSnapshot(ctx, snapshotID, volID); err != nil {
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func cleanupCloneFromSubvolumeSnapshot(
|
|
ctx context.Context,
|
|
volID, cloneID fsutil.VolumeID,
|
|
parentVolOpt *VolumeOptions) error {
|
|
// snapshot name is same as clone name as we need a name which can be
|
|
// identified during PVC-PVC cloning.
|
|
snapShotID := cloneID
|
|
snapInfo, err := parentVolOpt.GetSnapshotInfo(ctx, snapShotID, volID)
|
|
if err != nil {
|
|
if errors.Is(err, cerrors.ErrSnapNotFound) {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
if snapInfo.Protected == SnapshotIsProtected {
|
|
err = parentVolOpt.UnprotectSnapshot(ctx, snapShotID, volID)
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapShotID, err)
|
|
|
|
return err
|
|
}
|
|
}
|
|
err = parentVolOpt.DeleteSnapshot(ctx, snapShotID, volID)
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapShotID, err)
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CreateCloneFromSnapshot(
|
|
ctx context.Context,
|
|
parentVolOpt, volOptions *VolumeOptions,
|
|
vID *VolumeIdentifier,
|
|
sID *SnapshotIdentifier) error {
|
|
snapID := fsutil.VolumeID(sID.FsSnapshotName)
|
|
err := parentVolOpt.cloneSnapshot(
|
|
ctx,
|
|
fsutil.VolumeID(sID.FsSubvolName),
|
|
snapID,
|
|
fsutil.VolumeID(vID.FsSubvolName),
|
|
volOptions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err != nil {
|
|
if !cerrors.IsCloneRetryError(err) {
|
|
if dErr := volOptions.PurgeVolume(ctx, fsutil.VolumeID(vID.FsSubvolName), true); dErr != nil {
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", vID.FsSubvolName, dErr)
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
cloneState, err := volOptions.getCloneState(ctx, fsutil.VolumeID(vID.FsSubvolName))
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "failed to get clone state: %v", err)
|
|
|
|
return err
|
|
}
|
|
|
|
if cloneState != cephFSCloneComplete {
|
|
return cloneState.toError()
|
|
}
|
|
|
|
err = volOptions.ExpandVolume(ctx, fsutil.VolumeID(vID.FsSubvolName), volOptions.Size)
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "failed to expand volume %s with error: %v", vID.FsSubvolName, err)
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (vo *VolumeOptions) getCloneState(ctx context.Context, volID fsutil.VolumeID) (cephFSCloneState, error) {
|
|
fsa, err := vo.conn.GetFSAdmin()
|
|
if err != nil {
|
|
log.ErrorLog(
|
|
ctx,
|
|
"could not get FSAdmin, can get clone status for volume %s with ID %s: %v",
|
|
vo.FsName,
|
|
string(volID),
|
|
err)
|
|
|
|
return cephFSCloneError, err
|
|
}
|
|
|
|
cs, err := fsa.CloneStatus(vo.FsName, vo.SubvolumeGroup, string(volID))
|
|
if err != nil {
|
|
log.ErrorLog(ctx, "could not get clone state for volume %s with ID %s: %v", vo.FsName, string(volID), err)
|
|
|
|
return cephFSCloneError, err
|
|
}
|
|
|
|
return cephFSCloneState(cs.State), nil
|
|
}
|