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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cephfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// cephFSCloneFailed indicates that clone is in failed state.
|
|
|
|
cephFSCloneFailed = "failed"
|
|
|
|
// cephFSCloneCompleted indicates that clone is in in-progress state.
|
|
|
|
cephFSCloneInprogress = "in-progress"
|
|
|
|
// cephFSCloneComplete indicates that clone is in complete state.
|
|
|
|
cephFSCloneComplete = "complete"
|
|
|
|
// snapshotIsProtected string indicates that the snapshot is currently protected.
|
|
|
|
snapshotIsProtected = "yes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createCloneFromSubvolume(ctx context.Context, volID, cloneID volumeID, volOpt, parentvolOpt *volumeOptions, cr *util.Credentials) error {
|
|
|
|
snapshotID := cloneID
|
|
|
|
err := createSnapshot(ctx, parentvolOpt, cr, snapshotID, volID)
|
|
|
|
if err != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to create snapshot %s %v", snapshotID, err)
|
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 {
|
|
|
|
err = deleteSnapshot(ctx, parentvolOpt, cr, snapshotID, volID)
|
|
|
|
if err != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cloneErr != nil {
|
|
|
|
if err = purgeVolume(ctx, cloneID, cr, volOpt, true); err != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to delete volume %s: %v", cloneID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
if err = unprotectSnapshot(ctx, parentvolOpt, cr, snapshotID, volID); err != nil {
|
|
|
|
// Incase 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, ErrSnapProtectionExist) {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = deleteSnapshot(ctx, parentvolOpt, cr, snapshotID, volID); err != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
protectErr = protectSnapshot(ctx, parentvolOpt, cr, snapshotID, volID)
|
|
|
|
if protectErr != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to protect snapshot %s %v", snapshotID, protectErr)
|
2020-08-07 07:06:45 +00:00
|
|
|
return protectErr
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneErr = cloneSnapshot(ctx, parentvolOpt, cr, volID, snapshotID, cloneID, volOpt)
|
|
|
|
if cloneErr != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to clone snapshot %s %s to %s %v", volID, snapshotID, cloneID, cloneErr)
|
2020-08-07 07:06:45 +00:00
|
|
|
return cloneErr
|
|
|
|
}
|
|
|
|
var clone CloneStatus
|
|
|
|
clone, cloneErr = getCloneInfo(ctx, volOpt, cr, cloneID)
|
|
|
|
if cloneErr != nil {
|
|
|
|
return cloneErr
|
|
|
|
}
|
|
|
|
|
|
|
|
switch clone.Status.State {
|
|
|
|
case cephFSCloneInprogress:
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "clone is in progress for %v", cloneID)
|
2020-08-07 07:06:45 +00:00
|
|
|
return ErrCloneInProgress
|
|
|
|
case cephFSCloneFailed:
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "clone failed for %v", cloneID)
|
2020-08-07 07:06:45 +00:00
|
|
|
cloneFailedErr := fmt.Errorf("clone %s is in %s state", cloneID, clone.Status.State)
|
|
|
|
return cloneFailedErr
|
|
|
|
case cephFSCloneComplete:
|
|
|
|
// This is a work around to fix sizing issue for cloned images
|
|
|
|
err = resizeVolume(ctx, volOpt, cr, cloneID, volOpt.Size)
|
|
|
|
if err != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to expand volume %s: %v", cloneID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// As we completed clone, remove the intermediate snap
|
|
|
|
if err = unprotectSnapshot(ctx, parentvolOpt, cr, snapshotID, volID); err != nil {
|
|
|
|
// Incase 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, ErrSnapProtectionExist) {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = deleteSnapshot(ctx, parentvolOpt, cr, snapshotID, volID); err != nil {
|
2020-08-11 11:46:45 +00:00
|
|
|
util.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupCloneFromSubvolumeSnapshot(ctx context.Context, volID, cloneID volumeID, parentVolOpt *volumeOptions, cr *util.Credentials) 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 := getSnapshotInfo(ctx, parentVolOpt, cr, snapShotID, volID)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrSnapNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if snapInfo.Protected == snapshotIsProtected {
|
|
|
|
err = unprotectSnapshot(ctx, parentVolOpt, cr, snapShotID, volID)
|
|
|
|
if err != nil {
|
2020-08-11 11:47:49 +00:00
|
|
|
util.ErrorLog(ctx, "failed to unprotect snapshot %s %v", snapShotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = deleteSnapshot(ctx, parentVolOpt, cr, snapShotID, volID)
|
|
|
|
if err != nil {
|
2020-08-11 11:47:49 +00:00
|
|
|
util.ErrorLog(ctx, "failed to delete snapshot %s %v", snapShotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createCloneFromSnapshot(ctx context.Context, parentVolOpt, volOptions *volumeOptions, vID *volumeIdentifier, sID *snapshotIdentifier, cr *util.Credentials) error {
|
|
|
|
snapID := volumeID(sID.FsSnapshotName)
|
|
|
|
err := cloneSnapshot(ctx, parentVolOpt, cr, volumeID(sID.FsSubvolName), snapID, volumeID(vID.FsSubvolName), volOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, ErrCloneInProgress) {
|
|
|
|
if dErr := purgeVolume(ctx, volumeID(vID.FsSubvolName), cr, volOptions, true); dErr != nil {
|
2020-08-11 11:49:31 +00:00
|
|
|
util.ErrorLog(ctx, "failed to delete volume %s: %v", vID.FsSubvolName, dErr)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
var clone = CloneStatus{}
|
|
|
|
clone, err = getCloneInfo(ctx, volOptions, cr, volumeID(vID.FsSubvolName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch clone.Status.State {
|
|
|
|
case cephFSCloneInprogress:
|
|
|
|
return ErrCloneInProgress
|
|
|
|
case cephFSCloneFailed:
|
|
|
|
return fmt.Errorf("clone %s is in %s state", vID.FsSubvolName, clone.Status.State)
|
|
|
|
case cephFSCloneComplete:
|
|
|
|
// The clonedvolume currently does not reflect the proper size due to an issue in cephfs
|
|
|
|
// however this is getting addressed in cephfs and the parentvolume size will be reflected
|
|
|
|
// in the new cloned volume too. Till then we are explicitly making the size set
|
|
|
|
err = resizeVolume(ctx, volOptions, cr, volumeID(vID.FsSubvolName), volOptions.Size)
|
|
|
|
if err != nil {
|
2020-08-11 11:49:31 +00:00
|
|
|
util.ErrorLog(ctx, "failed to expand volume %s with error: %v", vID.FsSubvolName, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:50:37 +00:00
|
|
|
// CloneStatus represents the subvolume clone status.
|
2020-08-07 07:06:45 +00:00
|
|
|
type CloneStatus struct {
|
|
|
|
Status struct {
|
|
|
|
State string `json:"state"`
|
|
|
|
} `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCloneInfo(ctx context.Context, volOptions *volumeOptions, cr *util.Credentials, volID volumeID) (CloneStatus, error) {
|
|
|
|
clone := CloneStatus{}
|
|
|
|
args := []string{
|
|
|
|
"fs",
|
|
|
|
"clone",
|
|
|
|
"status",
|
|
|
|
volOptions.FsName,
|
|
|
|
string(volID),
|
|
|
|
"--group_name",
|
|
|
|
volOptions.SubvolumeGroup,
|
|
|
|
"-m", volOptions.Monitors,
|
|
|
|
"-c", util.CephConfigPath,
|
|
|
|
"-n", cephEntityClientPrefix + cr.ID,
|
|
|
|
"--keyfile=" + cr.KeyFile,
|
|
|
|
"--format=json",
|
|
|
|
}
|
2020-11-06 02:57:37 +00:00
|
|
|
stdErr, err := execCommandJSON(
|
2020-08-07 07:06:45 +00:00
|
|
|
ctx,
|
|
|
|
&clone,
|
|
|
|
"ceph",
|
|
|
|
args[:]...)
|
|
|
|
if err != nil {
|
2020-11-06 02:57:37 +00:00
|
|
|
util.ErrorLog(ctx, "failed to get subvolume clone info %s in fs %s with Error: %v. stdError: %s", string(volID), volOptions.FsName, err, stdErr)
|
2020-08-07 07:06:45 +00:00
|
|
|
return clone, err
|
|
|
|
}
|
|
|
|
return clone, nil
|
|
|
|
}
|