ceph-csi/internal/rbd/clone.go

224 lines
6.6 KiB
Go
Raw Normal View History

rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +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"
"errors"
"fmt"
"github.com/ceph/ceph-csi/internal/util/log"
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
librbd "github.com/ceph/go-ceph/rbd"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// checkCloneImage check the cloned image exists, if the cloned image is not
// found it will check the temporary cloned snapshot exists, and again it will
// check the snapshot exists on the temporary cloned image, if yes it will
// create a new cloned and delete the temporary snapshot and adds a task to
// flatten the temp cloned image and return success.
//
// if the temporary snapshot does not exists it creates a temporary snapshot on
// temporary cloned image and creates a new cloned with user-provided image
// features and delete the temporary snapshot and adds a task to flatten the
// temp cloned image and return success
//
// if the temporary clone does not exist and if there is a temporary snapshot
// present on the parent image it will delete the temporary snapshot and
// returns.
func (rv *rbdVolume) checkCloneImage(ctx context.Context, parentVol *rbdVolume) (bool, error) {
// generate temp cloned volume
tempClone := rv.generateTempClone()
defer tempClone.Destroy()
snap := &rbdSnapshot{}
defer snap.Destroy()
snap.RbdSnapName = rv.RbdImageName
snap.Pool = rv.Pool
err := tempClone.checkSnapExists(snap)
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
if err != nil {
switch {
case errors.Is(err, ErrSnapNotFound):
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
// as the snapshot is not present, create new snapshot,clone and
// delete the temporary snapshot
err = createRBDClone(ctx, tempClone, rv, snap)
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
if err != nil {
return false, err
}
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
return true, nil
case errors.Is(err, ErrImageNotFound):
// as the temp clone does not exist,check snapshot exists on parent volume
// snapshot name is same as temporary clone image
snap.RbdImageName = tempClone.RbdImageName
err = parentVol.checkSnapExists(snap)
if err == nil {
// the temp clone exists, delete it lets reserve a new ID and
// create new resources for a cleaner approach
err = parentVol.deleteSnapshot(ctx, snap)
}
if errors.Is(err, ErrSnapNotFound) {
return false, nil
}
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
return false, err
default:
// any error other than the above return error
return false, err
}
}
// snap will be created after we flatten the temporary cloned image,no
// need to check for flatten here.
// as the snap exists,create clone image and delete temporary snapshot
// and add task to flatten temporary cloned image
err = rv.cloneRbdImageFromSnapshot(ctx, snap, parentVol)
if err != nil {
log.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", rv.RbdImageName, snap.RbdSnapName, err)
err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", rv.RbdImageName, snap.RbdSnapName, err)
return false, err
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
}
err = tempClone.deleteSnapshot(ctx, snap)
if err != nil {
log.ErrorLog(ctx, "failed to delete snapshot: %v", err)
return false, err
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
}
return true, nil
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
}
func (rv *rbdVolume) generateTempClone() *rbdVolume {
tempClone := rbdVolume{}
tempClone.conn = rv.conn.Copy()
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
// The temp clone image need to have deep flatten feature
f := []string{librbd.FeatureNameLayering, librbd.FeatureNameDeepFlatten}
tempClone.ImageFeatureSet = librbd.FeatureSetFromNames(f)
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
tempClone.ClusterID = rv.ClusterID
tempClone.Monitors = rv.Monitors
tempClone.Pool = rv.Pool
tempClone.RadosNamespace = rv.RadosNamespace
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
// The temp cloned image name will be always (rbd image name + "-temp")
// this name will be always unique, as cephcsi never creates an image with
// this format for new rbd images
tempClone.RbdImageName = rv.RbdImageName + "-temp"
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
return &tempClone
}
func (rv *rbdVolume) createCloneFromImage(ctx context.Context, parentVol *rbdVolume) error {
j, err := volJournal.Connect(rv.Monitors, rv.RadosNamespace, rv.conn.Creds)
if err != nil {
return status.Error(codes.Internal, err.Error())
}
defer j.Destroy()
err = rv.doSnapClone(ctx, parentVol)
if err != nil {
return err
}
err = rv.getImageID()
if err != nil {
log.ErrorLog(ctx, "failed to get volume id %s: %v", rv, err)
return err
}
err = parentVol.copyEncryptionConfig(&rv.rbdImage, true)
if err != nil {
return fmt.Errorf("failed to copy encryption config for %q: %w", rv, err)
}
err = j.StoreImageID(ctx, rv.JournalPool, rv.ReservedID, rv.ImageID)
if err != nil {
log.ErrorLog(ctx, "failed to store volume %s: %v", rv, err)
return err
}
// expand the image if the requested size is greater than the current size
err = rv.expand()
if err != nil {
log.ErrorLog(ctx, "failed to resize volume %s: %v", rv, err)
return err
}
return nil
}
func (rv *rbdVolume) doSnapClone(ctx context.Context, parentVol *rbdVolume) error {
var errClone error
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
// generate temp cloned volume
tempClone := rv.generateTempClone()
// snapshot name is same as temporary cloned image, This helps to
// flatten the temporary cloned images as we cannot have more than 510
// snapshots on an rbd image
tempSnap := &rbdSnapshot{}
tempSnap.RbdSnapName = tempClone.RbdImageName
tempSnap.Pool = rv.Pool
cloneSnap := &rbdSnapshot{}
cloneSnap.RbdSnapName = rv.RbdImageName
cloneSnap.Pool = rv.Pool
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
// create snapshot and temporary clone and delete snapshot
err := createRBDClone(ctx, parentVol, tempClone, tempSnap)
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
if err != nil {
return err
}
defer func() {
if err != nil || errClone != nil {
cErr := cleanUpSnapshot(ctx, tempClone, cloneSnap, rv)
if cErr != nil {
log.ErrorLog(ctx, "failed to cleanup image %s or snapshot %s: %v", cloneSnap, tempClone, cErr)
}
}
if err != nil {
// cleanup snapshot
cErr := cleanUpSnapshot(ctx, parentVol, tempSnap, tempClone)
if cErr != nil {
log.ErrorLog(ctx, "failed to cleanup image %s or snapshot %s: %v", tempClone, tempSnap, cErr)
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
}
}
}()
// create snap of temp clone from temporary cloned image
// create final clone
// delete snap of temp clone
errClone = createRBDClone(ctx, tempClone, rv, cloneSnap)
if errClone != nil {
return errClone
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
}
err = parentVol.copyEncryptionConfig(&rv.rbdImage, true)
if err != nil {
return fmt.Errorf("failed to copy encryption config for %q: %w", rv, err)
}
rbd: Add support for smart cloning Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
2020-07-07 12:14:19 +00:00
return nil
}