Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#170 from huffmanca/enhance-error
Browse files Browse the repository at this point in the history
Bug 1871683: UPSTREAM: 550: Clarify error message when unsupported volume capabilities are provided
  • Loading branch information
openshift-merge-robot authored Sep 25, 2020
2 parents 7196f8e + 4ecddab commit a84081f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ func (d *controllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
}

if !isValidVolumeCapabilities(volCaps) {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities not supported")
modes := util.GetAccessModes(volCaps)
stringModes := strings.Join(*modes, ", ")
errString := "Volume capabilities " + stringModes + " not supported. Only AccessModes[ReadWriteOnce] supported."
return nil, status.Error(codes.InvalidArgument, errString)
}

disk, err := d.cloud.GetDiskByName(ctx, volName, volSizeBytes)
Expand Down Expand Up @@ -256,7 +259,10 @@ func (d *controllerService) ControllerPublishVolume(ctx context.Context, req *cs

caps := []*csi.VolumeCapability{volCap}
if !isValidVolumeCapabilities(caps) {
return nil, status.Error(codes.InvalidArgument, "Volume capability not supported")
modes := util.GetAccessModes(caps)
stringModes := strings.Join(*modes, ", ")
errString := "Volume capabilities " + stringModes + " not supported. Only AccessModes[ReadWriteOnce] supported."
return nil, status.Error(codes.InvalidArgument, errString)
}

if !d.cloud.IsExistInstance(ctx, nodeID) {
Expand Down
46 changes: 46 additions & 0 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ func TestCreateVolume(t *testing.T) {
},
},
}
invalidVolCap := []*csi.VolumeCapability{
{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER,
},
},
}
stdVolSize := int64(5 * 1024 * 1024 * 1024)
stdCapRange := &csi.CapacityRange{RequiredBytes: stdVolSize}
stdParams := map[string]string{}
Expand Down Expand Up @@ -1181,6 +1188,45 @@ func TestCreateVolume(t *testing.T) {
}
},
},
{
name: "fail with invalid volume access modes",
testFunc: func(t *testing.T) {
req := &csi.CreateVolumeRequest{
Name: "vol-test",
CapacityRange: stdCapRange,
VolumeCapabilities: invalidVolCap,
Parameters: map[string]string{
VolumeTypeKey: cloud.VolumeTypeIO1,
"unknownKey": "unknownValue",
},
}

ctx := context.Background()

mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockCloud := mocks.NewMockCloud(mockCtl)

awsDriver := controllerService{
cloud: mockCloud,
driverOptions: &DriverOptions{},
}

_, err := awsDriver.CreateVolume(ctx, req)
if err == nil {
t.Fatalf("Expected CreateVolume to fail but got no error")
}

srvErr, ok := status.FromError(err)
if !ok {
t.Fatalf("Could not get error status code from error: %v", srvErr)
}
if srvErr.Code() != codes.InvalidArgument {
t.Fatalf("Expect InvalidArgument but got: %s", srvErr.Code())
}
},
},
}

for _, tc := range testCases {
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"path"
"path/filepath"
"strings"

csi "github.com/container-storage-interface/spec/lib/go/csi"
)

const (
Expand Down Expand Up @@ -78,3 +80,13 @@ func ParseEndpoint(endpoint string) (string, string, error) {
func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 {
return (volumeSizeBytes + allocationUnitBytes - 1) / allocationUnitBytes
}

// GetAccessModes returns a slice containing all of the access modes defined
// in the passed in VolumeCapabilities.
func GetAccessModes(caps []*csi.VolumeCapability) *[]string {
modes := []string{}
for _, c := range caps {
modes = append(modes, c.AccessMode.GetMode().String())
}
return &modes
}
26 changes: 26 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package util

import (
"fmt"
"reflect"
"testing"

csi "github.com/container-storage-interface/spec/lib/go/csi"
)

func TestRoundUpBytes(t *testing.T) {
Expand Down Expand Up @@ -125,3 +128,26 @@ func TestParseEndpoint(t *testing.T) {
}

}

func TestGetAccessModes(t *testing.T) {
testVolCap := []*csi.VolumeCapability{
{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
},
{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY,
},
},
}
expectedModes := []string{
"SINGLE_NODE_WRITER",
"SINGLE_NODE_READER_ONLY",
}
actualModes := GetAccessModes(testVolCap)
if !reflect.DeepEqual(expectedModes, *actualModes) {
t.Fatalf("Wrong values returned for volume capabilities. Expected %v, got %v", expectedModes, actualModes)
}
}

0 comments on commit a84081f

Please sign in to comment.