Fix csi plugin nil pointer for storage classes with no defined params
This fixes an issue for RWX volume creation as well as volume creation
based on a snapshot where the storage class has no params defined.
Longhorn 2154
@@ -61,23 +61,19 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
return nil, status.Error(codes.InvalidArgument, err.Error())
}
// Check request parameters like Name and Volume Capabilities
- volumeParameters := req.GetParameters()
if len(req.GetName()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume Name cannot be empty")
}
volumeCaps := req.GetVolumeCapabilities()
- if volumeCaps == nil {
- return nil, status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
- }
-
- if err := cs.validateVolumeCapabilities(req.GetVolumeCapabilities()); err != nil {
+ if err := cs.validateVolumeCapabilities(volumeCaps); err != nil {
return nil, err
}
+ volumeParameters := req.GetParameters()
// check if we need to restore from a csi snapshot
// we don't support volume cloning at the moment
- if req.VolumeContentSource != nil {
- if snapshot := req.VolumeContentSource.GetSnapshot(); snapshot != nil {
+ if req.VolumeContentSource != nil && req.VolumeContentSource.GetSnapshot() != nil {
+ snapshot := req.VolumeContentSource.GetSnapshot()
_, volumeName, backupName := decodeSnapshotID(snapshot.SnapshotId)
bv, err := cs.apiClient.BackupVolume.ById(volumeName)
if err != nil {
@@ -95,8 +91,10 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
// use the fromBackup method for the csi snapshot restores as well
// the same parameter was previously only used for restores based on the storage class
- volumeParameters["fromBackup"] = backup.Url
+ if volumeParameters == nil {
+ volumeParameters = map[string]string{}
}
+ volumeParameters["fromBackup"] = backup.Url
}
// check for already existing volume name
@@ -129,6 +127,9 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
// we need to mark the volume as a shared volume
for _, cap := range volumeCaps {
if requiresSharedAccess(nil, cap) {
+ if volumeParameters == nil {
+ volumeParameters = map[string]string{}
+ }
volumeParameters["share"] = "true"
break
}
