Skip to content

Commit

Permalink
feat(clone): add support for creating the Clone from volume as dataso…
Browse files Browse the repository at this point in the history
…urce

Signed-off-by: Pawan <pawan@mayadata.io>
  • Loading branch information
pawanpraka1 committed Nov 10, 2020
1 parent 64bc7cb commit 86058ec
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/234-pawanpraka1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add support for creating the Clone from volume as datasource
71 changes: 68 additions & 3 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,70 @@ func CreateZFSVolume(req *csi.CreateVolumeRequest) (string, error) {
return selected, nil
}

// CreateZFSClone create a clone of zfs volume
func CreateZFSClone(req *csi.CreateVolumeRequest, snapshot string) (string, error) {
// CreateVolClone creates the clone from a volume
func CreateVolClone(req *csi.CreateVolumeRequest, srcVol string) (string, error) {
volName := req.GetName()
parameters := req.GetParameters()
// lower case keys, cf CreateZFSVolume()
pool := helpers.GetInsensitiveParameter(&parameters, "poolname")
size := getRoundedCapacity(req.GetCapacityRange().RequiredBytes)
volsize := strconv.FormatInt(int64(size), 10)

vol, err := zfs.GetZFSVolume(srcVol)
if err != nil {
return "", status.Error(codes.NotFound, err.Error())
}

if vol.Spec.PoolName != pool {
return "", status.Errorf(codes.Internal,
"vol clone to a different pool src pool %s dst pool %s",
vol.Spec.PoolName, pool)
}

if vol.Spec.Capacity != volsize {
return "", status.Error(codes.Internal, "vol clone volume size is not matching")
}

selected := vol.Spec.OwnerNodeID

// create the snpashot

labels := map[string]string{zfs.ZFSVolKey: vol.Name}

snapObj, err := snapbuilder.NewBuilder().
WithName(volName).
WithLabels(labels).Build()

snapObj.Spec = vol.Spec
snapObj.Status.State = zfs.ZFSStatusPending

err = zfs.ProvisionSnapshot(snapObj)
if err != nil {
return "", status.Errorf(codes.Internal,
"clone: not able to provision the snapshot %s", err.Error())
}

// create the clone from the above snapshot

volObj, err := volbuilder.NewBuilder().
WithName(volName).
WithVolumeStatus(zfs.ZFSStatusPending).
Build()

volObj.Spec = vol.Spec
volObj.Spec.SnapName = volName

err = zfs.ProvisionVolume(volObj)
if err != nil {
return "", status.Errorf(codes.Internal,
"clone: not able to provision the volume %s", err.Error())
}

return selected, nil
}

// CreateSnapClone creates the clone from a snapshot
func CreateSnapClone(req *csi.CreateVolumeRequest, snapshot string) (string, error) {

volName := req.GetName()
parameters := req.GetParameters()
Expand Down Expand Up @@ -243,7 +305,10 @@ func (cs *controller) CreateVolume(
if contentSource != nil && contentSource.GetSnapshot() != nil {
snapshotID := contentSource.GetSnapshot().GetSnapshotId()

selected, err = CreateZFSClone(req, snapshotID)
selected, err = CreateSnapClone(req, snapshotID)
} else if contentSource != nil && contentSource.GetVolume() != nil {
srcVol := contentSource.GetVolume().GetVolumeId()
selected, err = CreateVolClone(req, srcVol)
} else {
selected, err = CreateZFSVolume(req)
}
Expand Down

0 comments on commit 86058ec

Please sign in to comment.