@@ -97,12 +97,23 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
97
97
requestedAccessType = state .MountAccess
98
98
}
99
99
100
+ capacity := int64 (req .GetCapacityRange ().GetRequiredBytes ())
101
+ limit := int64 (req .GetCapacityRange ().GetLimitBytes ())
102
+ if capacity < 0 || limit < 0 {
103
+ return nil , status .Error (codes .InvalidArgument , "cannot have negative capacity" )
104
+ }
105
+ if limit > 0 && capacity > limit {
106
+ return nil , status .Error (codes .InvalidArgument , "capacity cannot exceed limit" )
107
+ }
108
+ if capacity == 0 && limit > 0 {
109
+ capacity = limit
110
+ }
111
+
100
112
// Lock before acting on global state. A production-quality
101
113
// driver might use more fine-grained locking.
102
114
hp .mutex .Lock ()
103
115
defer hp .mutex .Unlock ()
104
116
105
- capacity := int64 (req .GetCapacityRange ().GetRequiredBytes ())
106
117
topologies := []* csi.Topology {}
107
118
if hp .config .EnableTopology {
108
119
topologies = append (topologies , & csi.Topology {Segments : map [string ]string {TopologyKeyNode : hp .config .NodeID }})
@@ -114,7 +125,7 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
114
125
// Since err is nil, it means the volume with the same name already exists
115
126
// need to check if the size of existing volume is the same as in new
116
127
// request
117
- if exVol .VolSize < capacity {
128
+ if exVol .VolSize < capacity || ( limit > 0 && exVol . VolSize > limit ) {
118
129
return nil , status .Errorf (codes .AlreadyExists , "Volume with the same name: %s but with different size already exist" , req .GetName ())
119
130
}
120
131
if req .GetVolumeContentSource () != nil {
@@ -146,6 +157,8 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
146
157
147
158
volumeID := uuid .NewUUID ().String ()
148
159
kind := req .GetParameters ()[storageKind ]
160
+ // This code does not check whether hp.createVolume rounds capacity up;
161
+ // a more robust driver would ensure any rounding does not exceed limit.
149
162
vol , err := hp .createVolume (volumeID , req .GetName (), capacity , requestedAccessType , false /* ephemeral */ , kind )
150
163
if err != nil {
151
164
return nil , err
@@ -182,7 +195,7 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
182
195
return & csi.CreateVolumeResponse {
183
196
Volume : & csi.Volume {
184
197
VolumeId : volumeID ,
185
- CapacityBytes : req . GetCapacityRange (). GetRequiredBytes () ,
198
+ CapacityBytes : capacity ,
186
199
VolumeContext : req .GetParameters (),
187
200
ContentSource : req .GetVolumeContentSource (),
188
201
AccessibleTopology : topologies ,
0 commit comments