2020-08-07 07:06:45 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
package core
|
2020-08-07 07:06:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2021-08-25 06:46:03 +00:00
|
|
|
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2020-08-07 07:06:45 +00:00
|
|
|
)
|
|
|
|
|
2020-10-28 14:20:41 +00:00
|
|
|
// cephFSCloneState describes the status of the clone.
|
|
|
|
type cephFSCloneState string
|
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
const (
|
2022-02-15 12:11:09 +00:00
|
|
|
// 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")
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
// SnapshotIsProtected string indicates that the snapshot is currently protected.
|
|
|
|
SnapshotIsProtected = "yes"
|
2020-08-07 07:06:45 +00:00
|
|
|
)
|
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
// toError checks the state of the clone if it's not cephFSCloneComplete.
|
|
|
|
func (cs cephFSCloneState) toError() error {
|
|
|
|
switch cs {
|
2022-02-15 12:11:09 +00:00
|
|
|
case CephFSCloneComplete:
|
2021-07-05 13:10:53 +00:00
|
|
|
return nil
|
2022-02-15 12:11:09 +00:00
|
|
|
case CephFSCloneError:
|
2021-08-25 06:46:03 +00:00
|
|
|
return cerrors.ErrInvalidClone
|
2022-02-15 12:11:09 +00:00
|
|
|
case CephFSCloneInprogress:
|
2021-08-25 06:46:03 +00:00
|
|
|
return cerrors.ErrCloneInProgress
|
2022-02-15 12:11:09 +00:00
|
|
|
case CephFSClonePending:
|
2021-08-25 06:46:03 +00:00
|
|
|
return cerrors.ErrClonePending
|
2022-02-15 12:11:09 +00:00
|
|
|
case CephFSCloneFailed:
|
2021-08-25 06:46:03 +00:00
|
|
|
return cerrors.ErrCloneFailed
|
2021-07-05 13:10:53 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// CreateCloneFromSubvolume creates a clone from a subvolume.
|
|
|
|
func (s *subVolumeClient) CreateCloneFromSubvolume(
|
2021-09-16 13:47:57 +00:00
|
|
|
ctx context.Context,
|
2022-02-15 12:11:09 +00:00
|
|
|
parentvolOpt *SubVolume) error {
|
|
|
|
snapshotID := s.VolID
|
|
|
|
snapClient := NewSnapshot(s.conn, snapshotID, parentvolOpt)
|
|
|
|
err := snapClient.CreateSnapshot(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to create snapshot %s %v", snapshotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
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 {
|
2022-02-15 12:11:09 +00:00
|
|
|
err = snapClient.DeleteSnapshot(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cloneErr != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = s.PurgeVolume(ctx, true); err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", s.VolID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = snapClient.UnprotectSnapshot(ctx); err != nil {
|
2020-11-24 11:54:29 +00:00
|
|
|
// In case the snap is already unprotected we get ErrSnapProtectionExist error code
|
2020-08-07 07:06:45 +00:00
|
|
|
// in that case we are safe and we could discard this error and we are good to go
|
|
|
|
// ahead with deletion
|
2021-08-25 06:46:03 +00:00
|
|
|
if !errors.Is(err, cerrors.ErrSnapProtectionExist) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = snapClient.DeleteSnapshot(ctx); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-02-15 12:11:09 +00:00
|
|
|
protectErr = snapClient.ProtectSnapshot(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if protectErr != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to protect snapshot %s %v", snapshotID, protectErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return protectErr
|
|
|
|
}
|
2022-02-15 12:11:09 +00:00
|
|
|
cloneErr = snapClient.CloneSnapshot(ctx, s.SubVolume)
|
2020-08-07 07:06:45 +00:00
|
|
|
if cloneErr != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "failed to clone snapshot %s %s to %s %v", parentvolOpt.VolID, snapshotID, s.VolID, cloneErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return cloneErr
|
|
|
|
}
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
cloneState, cloneErr := s.GetCloneState(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if cloneErr != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get clone state: %v", cloneErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return cloneErr
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
if cloneState != CephFSCloneComplete {
|
|
|
|
log.ErrorLog(ctx, "clone %s did not complete: %v", s.VolID, cloneState.toError())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return cloneState.toError()
|
|
|
|
}
|
2022-01-10 04:56:28 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
err = s.ExpandVolume(ctx, s.Size)
|
2021-07-05 13:10:53 +00:00
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "failed to expand volume %s: %v", s.VolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-10 04:56:28 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
// As we completed clone, remove the intermediate snap
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = snapClient.UnprotectSnapshot(ctx); err != nil {
|
2021-07-05 13:10:53 +00:00
|
|
|
// 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
|
2021-08-25 06:46:03 +00:00
|
|
|
if !errors.Is(err, cerrors.ErrSnapProtectionExist) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapshotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = snapClient.DeleteSnapshot(ctx); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// CleanupSnapshotFromSubvolume removes the snapshot from the subvolume.
|
|
|
|
func (s *subVolumeClient) CleanupSnapshotFromSubvolume(
|
|
|
|
ctx context.Context, parentVol *SubVolume) error {
|
2020-08-07 07:06:45 +00:00
|
|
|
// snapshot name is same as clone name as we need a name which can be
|
|
|
|
// identified during PVC-PVC cloning.
|
2022-02-15 12:11:09 +00:00
|
|
|
snapShotID := s.VolID
|
|
|
|
snapClient := NewSnapshot(s.conn, snapShotID, parentVol)
|
|
|
|
snapInfo, err := snapClient.GetSnapshotInfo(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-25 06:46:03 +00:00
|
|
|
if errors.Is(err, cerrors.ErrSnapNotFound) {
|
2020-08-07 07:06:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
if snapInfo.Protected == SnapshotIsProtected {
|
2022-02-15 12:11:09 +00:00
|
|
|
err = snapClient.UnprotectSnapshot(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapShotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 12:11:09 +00:00
|
|
|
err = snapClient.DeleteSnapshot(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapShotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// CreateSnapshotFromSubvolume creates a clone from subvolume snapshot.
|
|
|
|
func (s *subVolumeClient) CreateCloneFromSnapshot(
|
|
|
|
ctx context.Context, snap Snapshot) error {
|
|
|
|
snapID := snap.SnapshotID
|
|
|
|
snapClient := NewSnapshot(s.conn, snapID, snap.SubVolume)
|
|
|
|
err := snapClient.CloneSnapshot(ctx, s.SubVolume)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2021-09-16 13:47:57 +00:00
|
|
|
if !cerrors.IsCloneRetryError(err) {
|
2022-02-15 12:11:09 +00:00
|
|
|
if dErr := s.PurgeVolume(ctx, true); dErr != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", s.VolID, dErr)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
cloneState, err := s.GetCloneState(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get clone state: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
if cloneState != CephFSCloneComplete {
|
2021-07-05 13:10:53 +00:00
|
|
|
return cloneState.toError()
|
|
|
|
}
|
2022-01-10 04:56:28 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
err = s.ExpandVolume(ctx, s.Size)
|
2021-07-05 13:10:53 +00:00
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "failed to expand volume %s with error: %v", s.VolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return err
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// GetCloneState returns the clone state of the subvolume.
|
|
|
|
func (s *subVolumeClient) GetCloneState(ctx context.Context) (cephFSCloneState, error) {
|
|
|
|
fsa, err := s.conn.GetFSAdmin()
|
2020-11-04 10:26:49 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(
|
2021-06-25 10:18:59 +00:00
|
|
|
ctx,
|
|
|
|
"could not get FSAdmin, can get clone status for volume %s with ID %s: %v",
|
2022-02-15 12:11:09 +00:00
|
|
|
s.FsName,
|
|
|
|
s.VolID,
|
2021-06-25 10:18:59 +00:00
|
|
|
err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
return CephFSCloneError, err
|
2020-10-28 14:20:41 +00:00
|
|
|
}
|
2020-08-07 07:06:45 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
cs, err := fsa.CloneStatus(s.FsName, s.SubvolumeGroup, s.VolID)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "could not get clone state for volume %s with ID %s: %v", s.FsName, s.VolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
return CephFSCloneError, err
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
2020-11-04 10:26:49 +00:00
|
|
|
|
|
|
|
return cephFSCloneState(cs.State), nil
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|