Skip to content

Commit 1695ceb

Browse files
committed
Round up pdcsi driver size in CreateVolume
1 parent 0a18035 commit 1695ceb

File tree

7 files changed

+98
-14
lines changed

7 files changed

+98
-14
lines changed

pkg/common/utils.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ const (
4949
regionalDeviceNameSuffix = "_regional"
5050
)
5151

52-
func BytesToGb(bytes int64) int64 {
52+
func BytesToGbRoundDown(bytes int64) int64 {
5353
// TODO: Throw an error when div to 0
5454
return bytes / (1024 * 1024 * 1024)
5555
}
5656

57+
func BytesToGbRoundUp(bytes int64) int64 {
58+
re := bytes / (1024 * 1024 * 1024)
59+
if (bytes % (1024 * 1024 * 1024)) != 0 {
60+
re++
61+
}
62+
return re
63+
}
64+
5765
func GbToBytes(Gb int64) int64 {
5866
// TODO: Check for overflow
5967
return Gb * 1024 * 1024 * 1024

pkg/common/utils_test.go

+45-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
volIDRegionFmt = "projects/%s/regions/%s/disks/%s"
3030
)
3131

32-
func TestBytesToGb(t *testing.T) {
32+
func TestBytesToGbRoundDown(t *testing.T) {
3333
testCases := []struct {
3434
name string
3535
bytes int64
@@ -58,7 +58,50 @@ func TestBytesToGb(t *testing.T) {
5858
}
5959
for _, tc := range testCases {
6060
t.Logf("test case: %s", tc.name)
61-
gotGB := BytesToGb(tc.bytes)
61+
gotGB := BytesToGbRoundDown(tc.bytes)
62+
63+
if gotGB != tc.expGB {
64+
t.Errorf("got GB %v, expected %v", gotGB, tc.expGB)
65+
}
66+
67+
}
68+
}
69+
70+
func TestBytesToGbRoundUp(t *testing.T) {
71+
testCases := []struct {
72+
name string
73+
bytes int64
74+
expGB int64
75+
}{
76+
{
77+
name: "normal 5gb",
78+
bytes: 5368709120,
79+
expGB: 5,
80+
},
81+
{
82+
name: "slightly less than 5gb",
83+
bytes: 5368709119,
84+
expGB: 5,
85+
},
86+
{
87+
name: "slightly more than 5gb",
88+
bytes: 5368709121,
89+
expGB: 6,
90+
},
91+
{
92+
name: "1.5Gi",
93+
bytes: 1610612736,
94+
expGB: 2,
95+
},
96+
{
97+
name: "zero",
98+
bytes: 0,
99+
expGB: 0,
100+
},
101+
}
102+
for _, tc := range testCases {
103+
t.Logf("test case: %s", tc.name)
104+
gotGB := BytesToGbRoundUp(tc.bytes)
62105

63106
if gotGB != tc.expGB {
64107
t.Errorf("got GB %v, expected %v", gotGB, tc.expGB)

pkg/gce-cloud-provider/compute/fake-gce.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, volKey *meta.Key
262262

263263
computeDisk := &computev1.Disk{
264264
Name: volKey.Name,
265-
SizeGb: common.BytesToGb(capBytes),
265+
SizeGb: common.BytesToGbRoundUp(capBytes),
266266
Description: "Disk created by GCE-PD CSI Driver",
267267
Type: cloud.GetDiskTypeURI(volKey, params.DiskType),
268268
SourceSnapshotId: snapshotID,
@@ -414,9 +414,11 @@ func (cloud *FakeCloudProvider) ResizeDisk(ctx context.Context, volKey *meta.Key
414414
return -1, notFoundError()
415415
}
416416

417-
disk.setSizeGb(common.BytesToGb(requestBytes))
417+
requestSizGb := common.BytesToGbRoundUp(requestBytes)
418418

419-
return common.BytesToGb(requestBytes), nil
419+
disk.setSizeGb(requestSizGb)
420+
421+
return requestSizGb, nil
420422

421423
}
422424

pkg/gce-cloud-provider/compute/gce-compute.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func (cloud *CloudProvider) insertRegionalDisk(
381381

382382
diskToCreate := &computev1.Disk{
383383
Name: volKey.Name,
384-
SizeGb: common.BytesToGb(capBytes),
384+
SizeGb: common.BytesToGbRoundUp(capBytes),
385385
Description: description,
386386
Type: cloud.GetDiskTypeURI(volKey, params.DiskType),
387387
}
@@ -474,7 +474,7 @@ func (cloud *CloudProvider) insertZonalDisk(
474474

475475
diskToCreate := &computev1.Disk{
476476
Name: volKey.Name,
477-
SizeGb: common.BytesToGb(capBytes),
477+
SizeGb: common.BytesToGbRoundUp(capBytes),
478478
Description: description,
479479
Type: cloud.GetDiskTypeURI(volKey, params.DiskType),
480480
}
@@ -815,7 +815,7 @@ func (cloud *CloudProvider) ResizeDisk(ctx context.Context, volKey *meta.Key, re
815815
}
816816

817817
sizeGb := cloudDisk.GetSizeGb()
818-
requestGb := common.BytesToGb(requestBytes)
818+
requestGb := common.BytesToGbRoundUp(requestBytes)
819819

820820
// If disk is already of size equal or greater than requested size, we
821821
// simply return the found size

pkg/gce-pd-csi-driver/controller.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
182182

183183
// If there is no validation error, immediately return success
184184
klog.V(4).Infof("CreateVolume succeeded for disk %v, it already exists and was compatible", volKey)
185-
return generateCreateVolumeResponse(existingDisk, capBytes, zones), nil
185+
return generateCreateVolumeResponse(existingDisk, zones), nil
186186
}
187187

188188
snapshotID := ""
@@ -234,7 +234,7 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
234234
}
235235

236236
klog.V(4).Infof("CreateVolume succeeded for disk %v", volKey)
237-
return generateCreateVolumeResponse(disk, capBytes, zones), nil
237+
return generateCreateVolumeResponse(disk, zones), nil
238238

239239
}
240240

@@ -979,16 +979,17 @@ func getDefaultZonesInRegion(ctx context.Context, gceCS *GCEControllerServer, ex
979979
return ret, nil
980980
}
981981

982-
func generateCreateVolumeResponse(disk *gce.CloudDisk, capBytes int64, zones []string) *csi.CreateVolumeResponse {
982+
func generateCreateVolumeResponse(disk *gce.CloudDisk, zones []string) *csi.CreateVolumeResponse {
983983
tops := []*csi.Topology{}
984984
for _, zone := range zones {
985985
tops = append(tops, &csi.Topology{
986986
Segments: map[string]string{common.TopologyKeyZone: zone},
987987
})
988988
}
989+
realDiskSizeBytes := common.GbToBytes(disk.GetSizeGb())
989990
createResp := &csi.CreateVolumeResponse{
990991
Volume: &csi.Volume{
991-
CapacityBytes: capBytes,
992+
CapacityBytes: realDiskSizeBytes,
992993
VolumeId: cleanSelfLink(disk.GetSelfLink()),
993994
VolumeContext: nil,
994995
AccessibleTopology: tops,

test/e2e/utils/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func GetBlockSizeInGb(instance *remote.InstanceInfo, devicePath string) (int64,
210210
if err != nil {
211211
return -1, fmt.Errorf("failed to parse size %s into int", output)
212212
}
213-
return utilcommon.BytesToGb(n), nil
213+
return utilcommon.BytesToGbRoundDown(n), nil
214214
}
215215

216216
func Symlink(instance *remote.InstanceInfo, src, dest string) error {

test/k8s-integration/test-config.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
ShortName: pdtest
2+
StorageClass:
3+
FromExistingClassName: standard-rwo
4+
DriverInfo:
5+
Name: pd.csi.storage.gke.io
6+
SupportedFsType:
7+
ext2:
8+
ext3:
9+
ext4:
10+
# The following FS types supported by GCE PD but
11+
# currently we do not test the CSI Driver on Ubuntu or Windows
12+
# xfs: XFS only available on Ubuntu
13+
# ntfs: NTFS only available on Windows
14+
Capabilities:
15+
persistence: true
16+
multipods: true
17+
fsGroup: true
18+
exec: true
19+
block: true
20+
topology: true
21+
# dataSource: true
22+
# RWX: true
23+
SupportedMountOption:
24+
debug:
25+
nouid32:
26+
SupportedSizeRange:
27+
Min: 5Gi
28+
Max: 64Ti
29+
TopologyKeys:
30+
- topology.gke.io/zone

0 commit comments

Comments
 (0)