diff --git a/pkg/rbd/controllerserver.go b/pkg/rbd/controllerserver.go index 546a27d90..9dbe1f55a 100644 --- a/pkg/rbd/controllerserver.go +++ b/pkg/rbd/controllerserver.go @@ -108,7 +108,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol } snapshotID := snapshot.GetId() - if snapshotID == "" { + if len(snapshotID) == 0 { return nil, status.Error(codes.InvalidArgument, "Volume Snapshot ID cannot be empty") } @@ -137,7 +137,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol if err := persistVolInfo(volumeID, path.Join(PluginFolder, "controller"), rbdVol); err != nil { glog.Warningf("rbd: failed to store volInfo with error: %v", err) } - rbdVolumes[volumeID] = *rbdVol + rbdVolumes[volumeID] = rbdVol return &csi.CreateVolumeResponse{ Volume: &csi.Volume{ Id: volumeID, @@ -238,7 +238,7 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS } rbdSnap.VolName = rbdVolume.VolName rbdSnap.SnapName = snapName - snapshotID := "csi-rbd-snapshot-" + uniqueID + snapshotID := "csi-rbd-" + rbdVolume.VolName + "-snap-" + uniqueID rbdSnap.SnapID = snapshotID rbdSnap.SourceVolumeID = req.GetSourceVolumeId() rbdSnap.SizeBytes = rbdVolume.VolSize @@ -280,9 +280,10 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS // Storing snapInfo into a persistent file. if err := persistSnapInfo(snapshotID, path.Join(PluginFolder, "controller-snap"), rbdSnap); err != nil { glog.Warningf("rbd: failed to store sanpInfo with error: %v", err) + return nil, err } - rbdSnapshots[snapshotID] = *rbdSnap + rbdSnapshots[snapshotID] = rbdSnap return &csi.CreateSnapshotResponse{ Snapshot: &csi.Snapshot{ SizeBytes: rbdSnap.SizeBytes, @@ -303,7 +304,7 @@ func (cs *controllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS } snapshotID := req.GetSnapshotId() - if snapshotID == "" { + if len(snapshotID) == 0 { return nil, status.Error(codes.InvalidArgument, "Snapshot ID cannot be empty") } rbdSnap := &rbdSnapshot{} @@ -344,10 +345,10 @@ func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnap // TODO (sngchlko) list with token // list only a specific snapshot which has snapshot ID - if snapshotID := req.GetSnapshotId(); snapshotID != "" { + if snapshotID := req.GetSnapshotId(); len(snapshotID) != 0 { if rbdSnap, ok := rbdSnapshots[snapshotID]; ok { // if source volume ID also set, check source volume id on the cache. - if sourceVolumeId != "" && rbdSnap.SourceVolumeID != sourceVolumeId { + if len(sourceVolumeId) != 0 && rbdSnap.SourceVolumeID != sourceVolumeId { return nil, status.Error(codes.Unknown, fmt.Sprintf("Requested Source Volume ID %s is different from %s", sourceVolumeId, rbdSnap.SourceVolumeID)) } return &csi.ListSnapshotsResponse{ @@ -373,7 +374,7 @@ func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnap entries := []*csi.ListSnapshotsResponse_Entry{} for _, rbdSnap := range rbdSnapshots { // if source volume ID also set, check source volume id on the cache. - if sourceVolumeId != "" && rbdSnap.SourceVolumeID != sourceVolumeId { + if len(sourceVolumeId) != 0 && rbdSnap.SourceVolumeID != sourceVolumeId { continue } entries = append(entries, &csi.ListSnapshotsResponse_Entry{ diff --git a/pkg/rbd/rbd.go b/pkg/rbd/rbd.go index 09395a286..d5c6d000e 100644 --- a/pkg/rbd/rbd.go +++ b/pkg/rbd/rbd.go @@ -54,14 +54,14 @@ var ( version = "0.3.0" ) -var rbdVolumes map[string]rbdVolume -var rbdSnapshots map[string]rbdSnapshot +var rbdVolumes map[string]*rbdVolume +var rbdSnapshots map[string]*rbdSnapshot // Init checks for the persistent volume file and loads all found volumes // into a memory structure func init() { - rbdVolumes = map[string]rbdVolume{} - rbdSnapshots = map[string]rbdSnapshot{} + rbdVolumes = map[string]*rbdVolume{} + rbdSnapshots = map[string]*rbdSnapshot{} if _, err := os.Stat(path.Join(PluginFolder, "controller")); os.IsNotExist(err) { glog.Infof("rbd: folder %s not found. Creating... \n", path.Join(PluginFolder, "controller")) if err := os.Mkdir(path.Join(PluginFolder, "controller"), 0755); err != nil { @@ -108,7 +108,7 @@ func loadExSnapshots() { fp.Close() continue } - rbdSnapshots[rbdSnap.SnapID] = rbdSnap + rbdSnapshots[rbdSnap.SnapID] = &rbdSnap } glog.Infof("rbd: Loaded %d snapshots from %s", len(rbdSnapshots), path.Join(PluginFolder, "controller-snap")) } @@ -137,7 +137,7 @@ func loadExVolumes() { fp.Close() continue } - rbdVolumes[rbdVol.VolID] = rbdVol + rbdVolumes[rbdVol.VolID] = &rbdVol } glog.Infof("rbd: Loaded %d volumes from %s", len(rbdVolumes), path.Join(PluginFolder, "controller")) } diff --git a/pkg/rbd/rbd_util.go b/pkg/rbd/rbd_util.go index 796ed606a..66c4d1da4 100644 --- a/pkg/rbd/rbd_util.go +++ b/pkg/rbd/rbd_util.go @@ -446,29 +446,29 @@ func deleteSnapInfo(snapshot string, persistentStoragePath string) error { return nil } -func getRBDVolumeByID(volumeID string) (rbdVolume, error) { +func getRBDVolumeByID(volumeID string) (*rbdVolume, error) { if rbdVol, ok := rbdVolumes[volumeID]; ok { return rbdVol, nil } - return rbdVolume{}, fmt.Errorf("volume id %s does not exit in the volumes list", volumeID) + return nil, fmt.Errorf("volume id %s does not exit in the volumes list", volumeID) } -func getRBDVolumeByName(volName string) (rbdVolume, error) { +func getRBDVolumeByName(volName string) (*rbdVolume, error) { for _, rbdVol := range rbdVolumes { if rbdVol.VolName == volName { return rbdVol, nil } } - return rbdVolume{}, fmt.Errorf("volume name %s does not exit in the volumes list", volName) + return nil, fmt.Errorf("volume name %s does not exit in the volumes list", volName) } -func getRBDSnapshotByName(snapName string) (rbdSnapshot, error) { +func getRBDSnapshotByName(snapName string) (*rbdSnapshot, error) { for _, rbdSnap := range rbdSnapshots { if rbdSnap.SnapName == snapName { return rbdSnap, nil } } - return rbdSnapshot{}, fmt.Errorf("snapshot name %s does not exit in the snapshots list", snapName) + return nil, fmt.Errorf("snapshot name %s does not exit in the snapshots list", snapName) } func protectSnapshot(pOpts *rbdSnapshot, credentials map[string]string) error {