2020-06-24 07:43:24 +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 rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-06 17:51:04 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-06-24 07:43:24 +00:00
|
|
|
|
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2020-06-24 07:43:24 +00:00
|
|
|
)
|
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
func createRBDClone(
|
|
|
|
ctx context.Context,
|
|
|
|
parentVol, cloneRbdVol *rbdVolume,
|
2022-06-01 10:17:19 +00:00
|
|
|
snap *rbdSnapshot,
|
|
|
|
) error {
|
2020-06-24 07:43:24 +00:00
|
|
|
// create snapshot
|
|
|
|
err := parentVol.createSnapshot(ctx, snap)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to create snapshot %s: %v", snap, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:14:19 +00:00
|
|
|
snap.RbdImageName = parentVol.RbdImageName
|
2020-06-24 07:43:24 +00:00
|
|
|
// create clone image and delete snapshot
|
2021-03-18 11:23:08 +00:00
|
|
|
err = cloneRbdVol.cloneRbdImageFromSnapshot(ctx, snap, parentVol)
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx,
|
|
|
|
"failed to clone rbd image %s from snapshot %s: %v",
|
|
|
|
cloneRbdVol.RbdImageName,
|
|
|
|
snap.RbdSnapName,
|
|
|
|
err)
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"failed to clone rbd image %s from snapshot %s: %w",
|
|
|
|
cloneRbdVol.RbdImageName,
|
|
|
|
snap.RbdSnapName,
|
|
|
|
err)
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
errSnap := parentVol.deleteSnapshot(ctx, snap)
|
|
|
|
if errSnap != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot: %v", errSnap)
|
2021-11-10 09:38:32 +00:00
|
|
|
delErr := cloneRbdVol.deleteImage(ctx)
|
2020-06-24 07:43:24 +00:00
|
|
|
if delErr != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete rbd image: %s with error: %v", cloneRbdVol, delErr)
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 13:56:48 +00:00
|
|
|
// cleanUpSnapshot removes the RBD-snapshot (rbdSnap) from the RBD-image
|
|
|
|
// (parentVol) and deletes the RBD-image rbdVol.
|
2021-06-25 11:52:34 +00:00
|
|
|
func cleanUpSnapshot(
|
|
|
|
ctx context.Context,
|
|
|
|
parentVol *rbdVolume,
|
|
|
|
rbdSnap *rbdSnapshot,
|
2022-06-01 10:17:19 +00:00
|
|
|
rbdVol *rbdVolume,
|
|
|
|
) error {
|
2021-06-25 11:52:34 +00:00
|
|
|
err := parentVol.deleteSnapshot(ctx, rbdSnap)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, ErrSnapNotFound) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %q: %v", rbdSnap, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
return err
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 13:56:48 +00:00
|
|
|
|
|
|
|
if rbdVol != nil {
|
2021-11-10 09:38:32 +00:00
|
|
|
err := rbdVol.deleteImage(ctx)
|
2021-06-21 13:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, ErrImageNotFound) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete rbd image %q with error: %v", rbdVol, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-21 13:56:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 13:56:48 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateVolFromSnap(rbdSnap *rbdSnapshot) *rbdVolume {
|
|
|
|
vol := new(rbdVolume)
|
|
|
|
vol.ClusterID = rbdSnap.ClusterID
|
2021-03-12 12:50:07 +00:00
|
|
|
vol.VolID = rbdSnap.VolID
|
2020-06-24 07:43:24 +00:00
|
|
|
vol.Monitors = rbdSnap.Monitors
|
|
|
|
vol.Pool = rbdSnap.Pool
|
|
|
|
vol.JournalPool = rbdSnap.JournalPool
|
2020-06-01 13:57:51 +00:00
|
|
|
vol.RadosNamespace = rbdSnap.RadosNamespace
|
2020-06-24 07:43:24 +00:00
|
|
|
vol.RbdImageName = rbdSnap.RbdSnapName
|
2020-07-06 06:22:34 +00:00
|
|
|
vol.ImageID = rbdSnap.ImageID
|
2021-04-26 13:15:18 +00:00
|
|
|
// copyEncryptionConfig cannot be used here because the volume and the
|
|
|
|
// snapshot will have the same volumeID which cases the panic in
|
|
|
|
// copyEncryptionConfig function.
|
2022-05-27 18:03:32 +00:00
|
|
|
vol.blockEncryption = rbdSnap.blockEncryption
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return vol
|
|
|
|
}
|
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
func undoSnapshotCloning(
|
|
|
|
ctx context.Context,
|
|
|
|
parentVol *rbdVolume,
|
|
|
|
rbdSnap *rbdSnapshot,
|
|
|
|
cloneVol *rbdVolume,
|
2022-06-01 10:17:19 +00:00
|
|
|
cr *util.Credentials,
|
|
|
|
) error {
|
2021-11-10 09:38:32 +00:00
|
|
|
err := cleanUpSnapshot(ctx, parentVol, rbdSnap, cloneVol)
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to clean up %s or %s: %v", cloneVol, rbdSnap, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = undoSnapReservation(ctx, rbdSnap, cr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|