feat(clone): add support for creating the Clone from volume as datasource (#234)

This PR adds the capability to create the Clone from pvc directly

```
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-clone
spec:
  storageClassName: openebs-snap
  dataSource:
    name: pvc-snap
    kind: PersistentVolumeClaim
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
```
The ZFS_LocalPV driver will create one internal snapshot of the name
same as the new volume name and will create a clone out of it. Also,
while destroying the volume the driver will take care of deleting
the created snapshot for the clone.

Signed-off-by: Pawan <pawan@mayadata.io>
This commit is contained in:
Pawan Prakash Sharma 2020-11-11 18:58:25 +05:30 committed by GitHub
parent e52d6c7067
commit fb6f1006da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 3 deletions

View file

@ -39,6 +39,8 @@ const (
ZFSFinalizer string = "zfs.openebs.io/finalizer"
// ZFSVolKey for the ZfsSnapshot CR to store Persistence Volume name
ZFSVolKey string = "openebs.io/persistent-volume"
// ZFSSrcVolKey key for the source Volume name
ZFSSrcVolKey string = "openebs.io/source-volume"
// PoolNameKey is key for ZFS pool name
PoolNameKey string = "openebs.io/poolname"
// ZFSNodeKey will be used to insert Label in ZfsVolume CR

View file

@ -401,6 +401,26 @@ func CreateVolume(vol *apis.ZFSVolume) error {
func CreateClone(vol *apis.ZFSVolume) error {
volume := vol.Spec.PoolName + "/" + vol.Name
if srcVol, ok := vol.Labels[ZFSSrcVolKey]; ok {
// datasource is volume, create the snapshot first
snap := &apis.ZFSSnapshot{}
snap.Name = vol.Name // use volname as snapname
snap.Spec = vol.Spec
// add src vol name
snap.Labels = map[string]string{ZFSVolKey: srcVol}
klog.Infof("creating snapshot %s@%s for the clone %s", srcVol, snap.Name, volume)
err := CreateSnapshot(snap)
if err != nil {
klog.Errorf(
"zfs: could not create snapshot for the clone vol %s snap %s err %v", volume, snap.Name, err,
)
return err
}
}
if err := getVolume(volume); err != nil {
var args []string
args = buildCloneCreateArgs(vol)
@ -580,6 +600,27 @@ func DestroyVolume(vol *apis.ZFSVolume) error {
)
return err
}
if srcVol, ok := vol.Labels[ZFSSrcVolKey]; ok {
// datasource is volume, delete the dependent snapshot
snap := &apis.ZFSSnapshot{}
snap.Name = vol.Name // snapname is same as volname
snap.Spec = vol.Spec
// add src vol name
snap.Labels = map[string]string{ZFSVolKey: srcVol}
klog.Infof("destroying snapshot %s@%s for the clone %s", srcVol, snap.Name, volume)
err := DestroySnapshot(snap)
if err != nil {
// no need to reconcile as volume has already been deleted
klog.Errorf(
"zfs: could not destroy snapshot for the clone vol %s snap %s err %v", volume, snap.Name, err,
)
}
}
klog.Infof("destroyed volume %s", volume)
return nil