@@ -52,23 +52,38 @@ const (
52
52
attachableDiskTypePersistent = "PERSISTENT"
53
53
)
54
54
55
- func getRequestCapacity (capRange * csi.CapacityRange ) (capBytes int64 ) {
55
+ func getRequestCapacity (capRange * csi.CapacityRange ) (int64 , error ) {
56
56
// TODO: Take another look at these casts/caps. Make sure this func is correct
57
+ var capBytes int64
58
+ // Default case where nothing is set
57
59
if capRange == nil {
58
60
capBytes = MinimumVolumeSizeInBytes
59
- return
61
+ return capBytes , nil
60
62
}
61
63
62
- if tcap := capRange .GetRequiredBytes (); tcap > 0 {
63
- capBytes = tcap
64
- } else if tcap = capRange .GetLimitBytes (); tcap > 0 {
65
- capBytes = tcap
64
+ rBytes := capRange .GetRequiredBytes ()
65
+ rSet := rBytes > 0
66
+ lBytes := capRange .GetLimitBytes ()
67
+ lSet := lBytes > 0
68
+
69
+ if lSet && rSet && lBytes < rBytes {
70
+ return 0 , fmt .Errorf ("Limit bytes %v is less than required bytes %v" , lBytes , rBytes )
71
+ }
72
+ if lSet && lBytes < MinimumVolumeSizeInBytes {
73
+ return 0 , fmt .Errorf ("Limit bytes %v is less than minimum volume size: %v" , lBytes , MinimumVolumeSizeInBytes )
74
+ }
75
+
76
+ // If Required set just set capacity to that which is Required
77
+ if rSet {
78
+ capBytes = rBytes
66
79
}
80
+
81
+ // Limit is more than Required, but larger than Minimum. So we just set capcity to Minimum
67
82
// Too small, default
68
83
if capBytes < MinimumVolumeSizeInBytes {
69
84
capBytes = MinimumVolumeSizeInBytes
70
85
}
71
- return
86
+ return capBytes , nil
72
87
}
73
88
74
89
func (gceCS * GCEControllerServer ) CreateVolume (ctx context.Context , req * csi.CreateVolumeRequest ) (* csi.CreateVolumeResponse , error ) {
@@ -86,7 +101,10 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
86
101
return nil , status .Error (codes .InvalidArgument , "CreateVolume Volume capabilities must be provided" )
87
102
}
88
103
89
- capBytes := getRequestCapacity (capacityRange )
104
+ capBytes , err := getRequestCapacity (capacityRange )
105
+ if err != nil {
106
+ return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("CreateVolume Request Capacity is invalid: %v" , err ))
107
+ }
90
108
91
109
// TODO: Validate volume capabilities
92
110
0 commit comments