Skip to content

Commit

Permalink
snap-deletion-flatten
Browse files Browse the repository at this point in the history
Signed-off-by: Rakshith R <[email protected]>
  • Loading branch information
Rakshith-R committed Sep 25, 2023
1 parent 91f677f commit 4ffb174
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
8 changes: 1 addition & 7 deletions e2e/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,9 @@ var _ = Describe("RBD", func() {
validatePVCSnapshotDeletion(
higherCloneCount,
pvcPath,
appPath,
snapshotPath,
pvcClonePath,
appClonePath,
noKMS, noKMS,
defaultSCName,
noDataPool,
f,
noPVCValidation)
f)
})

By("create a PVC and check PVC/PV metadata on RBD image", func() {
Expand Down
16 changes: 8 additions & 8 deletions e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1404,14 +1404,17 @@ func validatePVCSnapshot(
validateRBDImageCount(f, 0, defaultRBDPool)
}

//nolint:gocyclo,gocognit,nestif,cyclop // reduce complexity
func validatePVCSnapshotDeletion(
totalCount int,
pvcPath, appPath, snapshotPath, pvcClonePath, appClonePath string,
kms, restoreKMS kmsConfig, restoreSCName,
dataPool string, f *framework.Framework,
isEncryptedPVC validateFunc,
pvcPath, snapshotPath, pvcClonePath string,
f *framework.Framework,
) {
defer func() {
std, stderr, err := execCommandInToolBoxPod(f, "ceph rbd task list", f.UniqueName)
framework.Logf("ceph rbd task list output: %s : %s", std, stderr)
framework.Logf("ceph rbd task list error: %v", err)

}()
var wg sync.WaitGroup
wgErrs := make([]error, totalCount)
err := createRBDSnapshotClass(f)
Expand Down Expand Up @@ -1444,9 +1447,6 @@ func validatePVCSnapshotDeletion(
framework.Failf("failed to load PVC: %v", err)
}
pvcClone.Namespace = f.UniqueName
if restoreSCName != "" {
pvcClone.Spec.StorageClassName = &restoreSCName
}

// create snapshot, restore to PVC, delete snapshot
for i := 0; i < totalCount; i++ {
Expand Down
8 changes: 7 additions & 1 deletion internal/rbd/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,13 @@ func (cs *ControllerServer) DeleteSnapshot(
}
defer rbdVol.Destroy()

rbdVol.ImageID = rbdSnap.ImageID
err = flattenSnapshotImage(ctx, rbdVol)
if err != nil {
log.ErrorLog(ctx, "failed to flatten snapshot: %v", err)

return nil, status.Error(codes.Internal, err.Error())
}

// update parent name to delete the snapshot
rbdSnap.RbdImageName = rbdVol.RbdImageName
err = cleanUpSnapshot(ctx, rbdVol, rbdSnap, rbdVol)
Expand Down
16 changes: 16 additions & 0 deletions internal/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,22 @@ func (ri *rbdImage) getImageInfo() error {
return nil
}

// checkChildrenExists checks if the image has any children.
func (ri *rbdImage) checkChildrenExists() (bool, error) {
image, err := ri.open()
if err != nil {
return false, fmt.Errorf("failed to open image %s: %w", ri, err)
}
defer image.Close()

_, children, err := image.ListChildren()
if err != nil {
return false, fmt.Errorf("failed to list children of image %s: %w", ri, err)
}

return (len(children) > 0), nil
}

// getParent returns parent image if it exists.
func (ri *rbdImage) getParent() (*rbdImage, error) {
err := ri.getImageInfo()
Expand Down
51 changes: 51 additions & 0 deletions internal/rbd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,57 @@ func createRBDClone(
return nil
}

// flattenSnapshotImage flattens triggers flattening of the rbd image underlying
// the snapshot if the following conditions are met:
// - the parent rbd image still exists
// - the parent rbd image has more snapshots than minSnapshotsOnImageToStartFlatten
// - the rbd image underlying snapshot has children
// FlattenInProgress error is ignored.
// This process ensures that further snapshot creation on the parent image will
// not fail due to existing snapshots' image being stuck in trash and
// minSnapshotsOnImageToStartFlatten limit being reached.
func flattenSnapshotImage(
ctx context.Context,
rbdVol *rbdVolume,
) error {
parentImage, err := rbdVol.getParent()
if err != nil {
return fmt.Errorf("failed to get parent image: %w", err)
}
if parentImage == nil {
return nil
}
defer parentImage.Destroy()

snapList, err := parentImage.listSnapshots()
if err != nil {
return fmt.Errorf("failed to list snapshots: %w", err)
}

if len(snapList) <= int(minSnapshotsOnImageToStartFlatten) { // minSnapshotsOnImageToStartFlatten) {
return nil
}

exists, err := rbdVol.checkChildrenExists()
if err != nil {
return fmt.Errorf("failed to check children exists: %w", err)
}

if !exists {
return nil
}

log.DebugLog(ctx, "Flattening image %s", rbdVol.RbdImageName)
// flatten image
err = rbdVol.flattenRbdImage(ctx,
true, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
if err != nil && !errors.Is(err, ErrFlattenInProgress) {
return fmt.Errorf("failed to flatten image: %w", err)
}

return nil
}

// cleanUpSnapshot removes the RBD-snapshot (rbdSnap) from the RBD-image
// (parentVol) and deletes the RBD-image rbdVol.
func cleanUpSnapshot(
Expand Down

0 comments on commit 4ffb174

Please sign in to comment.