From 9e525d4676a6162c3db307717bfbd3698b65b424 Mon Sep 17 00:00:00 2001 From: Hamdy Khader Date: Tue, 22 Aug 2023 09:32:40 +0300 Subject: [PATCH] Add snapshot API #1 --- pkg/spdk/controllerserver.go | 2 +- pkg/util/jsonrpc.go | 22 ++++++++++++++++------ pkg/util/nvmf.go | 23 +++++++++++------------ 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/pkg/spdk/controllerserver.go b/pkg/spdk/controllerserver.go index a18ae66..9087567 100644 --- a/pkg/spdk/controllerserver.go +++ b/pkg/spdk/controllerserver.go @@ -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()) diff --git a/pkg/util/jsonrpc.go b/pkg/util/jsonrpc.go index bc6d409..ebccecd 100644 --- a/pkg/util/jsonrpc.go +++ b/pkg/util/jsonrpc.go @@ -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 @@ -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", ¶ms, &snapshotID) - + err := client.callSBCLI("POST", "/csi/create_snapshot", ¶ms, &snapshotID) return snapshotID, err } diff --git a/pkg/util/nvmf.go b/pkg/util/nvmf.go index 32b869e..88245d8 100644 --- a/pkg/util/nvmf.go +++ b/pkg/util/nvmf.go @@ -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 } @@ -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