Skip to content

Commit 3c5e557

Browse files
authored
Merge pull request #151 from Madhu-1/fix-size
Validate Requested size in Create Volume
2 parents 9572e1c + 403e310 commit 3c5e557

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

pkg/hostpath/controllerserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
154154
if req.GetVolumeContentSource() != nil {
155155
contentSource := req.GetVolumeContentSource()
156156
if snapshot := contentSource.GetSnapshot(); snapshot != nil {
157-
err = loadFromSnapshot(snapshot.GetSnapshotId(), path)
157+
err = loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path)
158158
}
159159
if srcVolume := contentSource.GetVolume(); srcVolume != nil {
160-
err = loadFromVolume(srcVolume.GetVolumeId(), path)
160+
err = loadFromVolume(capacity, srcVolume.GetVolumeId(), path)
161161
}
162162
if err != nil {
163163
if delErr := deleteHostpathVolume(volumeID); delErr != nil {

pkg/hostpath/hostpath.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,17 @@ func hostPathIsEmpty(p string) (bool, error) {
275275
}
276276

277277
// loadFromSnapshot populates the given destPath with data from the snapshotID
278-
func loadFromSnapshot(snapshotId, destPath string) error {
278+
func loadFromSnapshot(size int64, snapshotId, destPath string) error {
279279
snapshot, ok := hostPathVolumeSnapshots[snapshotId]
280280
if !ok {
281281
return status.Errorf(codes.NotFound, "cannot find snapshot %v", snapshotId)
282282
}
283283
if snapshot.ReadyToUse != true {
284284
return status.Errorf(codes.Internal, "snapshot %v is not yet ready to use.", snapshotId)
285285
}
286+
if snapshot.SizeBytes > size {
287+
return status.Errorf(codes.InvalidArgument, "snapshot %v size %v is greater than requested volume size %v", snapshotId, snapshot.SizeBytes, size)
288+
}
286289
snapshotPath := snapshot.Path
287290
args := []string{"zxvf", snapshotPath, "-C", destPath}
288291
executor := utilexec.New()
@@ -294,7 +297,7 @@ func loadFromSnapshot(snapshotId, destPath string) error {
294297
}
295298

296299
// loadfromVolume populates the given destPath with data from the srcVolumeID
297-
func loadFromVolume(srcVolumeId, destPath string) error {
300+
func loadFromVolume(size int64, srcVolumeId, destPath string) error {
298301
hostPathVolume, ok := hostPathVolumes[srcVolumeId]
299302
if !ok {
300303
return status.Error(codes.NotFound, "source volumeId does not exist, are source/destination in the same storage class?")
@@ -305,6 +308,10 @@ func loadFromVolume(srcVolumeId, destPath string) error {
305308
return status.Errorf(codes.Internal, "failed verification check of source hostpath volume: %s: %v", srcVolumeId, err)
306309
}
307310

311+
if hostPathVolume.VolSize > size {
312+
return status.Errorf(codes.InvalidArgument, "volume %v size %v is greater than requested volume size %v", srcVolumeId, hostPathVolume.VolSize, size)
313+
}
314+
308315
// If the source hostpath volume is empty it's a noop and we just move along, otherwise the cp call will fail with a a file stat error DNE
309316
if !isEmpty {
310317
args := []string{"-a", srcPath + "/.", destPath + "/"}

0 commit comments

Comments
 (0)