Skip to content

Commit

Permalink
Add snapshot API #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamdy-khader committed Aug 22, 2023
1 parent e3b7637 commit 9e525d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/spdk/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (cs *controllerServer) DeleteSnapshot(_ context.Context, req *csi.DeleteSna
return nil, err
}

err = cs.spdkNodes[spdkVol.nodeName].DeleteVolume(spdkVol.lvolID)
err = cs.spdkNodes[spdkVol.nodeName].DeleteSnapshot(spdkVol.lvolID)
if err != nil {
klog.Errorf("failed to delete snapshot, snapshotID: %s err: %v", snapshotID, err)
return nil, status.Error(codes.Internal, err.Error())
Expand Down
22 changes: 16 additions & 6 deletions pkg/util/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type SpdkNode interface {
PublishVolume(lvolID string) error
UnpublishVolume(lvolID string) error
CreateSnapshot(lvolName, snapshotName string) (string, error)
DeleteSnapshot(snapshotID string) error
}

// logical volume store
Expand Down Expand Up @@ -278,18 +279,27 @@ func (client *rpcClient) deleteVolume(lvolID string) error {
return err
}

func (client *rpcClient) snapshot(lvolName, snapShotName string) (string, error) {
func (client *rpcClient) deleteSnapshot(snapshotID string) error {
err := client.callSBCLI("DELETE",
fmt.Sprintf("csi/delete_snapshot/%s", snapshotID), nil, nil)

if errorMatches(err, ErrJSONNoSuchDevice) {
err = ErrJSONNoSuchDevice // may happen in concurrency
}

return err
}

func (client *rpcClient) snapshot(lvolID, snapShotName string) (string, error) {
params := struct {
LvolName string `json:"lvol_name"`
LvolName string `json:"lvol_id"`
SnapShotName string `json:"snapshot_name"`
}{
LvolName: lvolName,
LvolName: lvolID,
SnapShotName: snapShotName,
}

var snapshotID string
err := client.call("bdev_lvol_snapshot", &params, &snapshotID)

err := client.callSBCLI("POST", "/csi/create_snapshot", &params, &snapshotID)
return snapshotID, err
}

Expand Down
23 changes: 11 additions & 12 deletions pkg/util/nvmf.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,11 @@ func (node *nodeNVMf) isVolumeCreated(lvolID string) (bool, error) {
return node.client.isVolumeCreated(lvolID)
}

func (node *nodeNVMf) CreateSnapshot(lvolName, snapshotName string) (string, error) {
lvsName, err := node.client.getLvstore(lvolName)
func (node *nodeNVMf) CreateSnapshot(lvolID, snapshotName string) (string, error) {
snapshotID, err := node.client.snapshot(lvolID, snapshotName)
if err != nil {
return "", err
}
lvol, err := node.client.getVolume(fmt.Sprintf("%s/%s", lvsName, snapshotName))
if err == nil {
klog.Warningf("snapshot already created: %s", lvol.UUID)
return lvol.UUID, nil
}
snapshotID, err := node.client.snapshot(lvolName, snapshotName)
if err != nil {
return "", err
}

klog.V(5).Infof("snapshot created: %s", snapshotID)
return snapshotID, nil
}
Expand All @@ -115,6 +105,15 @@ func (node *nodeNVMf) DeleteVolume(lvolID string) error {
return nil
}

func (node *nodeNVMf) DeleteSnapshot(snapshotID string) error {
err := node.client.deleteSnapshot(snapshotID)
if err != nil {
return err
}
klog.V(5).Infof("snapshot deleted: %s", snapshotID)
return nil
}

// PublishVolume exports a volume through NVMf target
func (node *nodeNVMf) PublishVolume(lvolID string) error {
var isPublished bool
Expand Down

0 comments on commit 9e525d4

Please sign in to comment.