Skip to content

Round up pdcsi driver size in CreateVolume #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ const (
regionalDeviceNameSuffix = "_regional"
)

func BytesToGb(bytes int64) int64 {
func BytesToGbRoundDown(bytes int64) int64 {
// TODO: Throw an error when div to 0
return bytes / (1024 * 1024 * 1024)
}

func BytesToGbRoundUp(bytes int64) int64 {
re := bytes / (1024 * 1024 * 1024)
if (bytes % (1024 * 1024 * 1024)) != 0 {
re++
}
return re
}

func GbToBytes(Gb int64) int64 {
// TODO: Check for overflow
return Gb * 1024 * 1024 * 1024
Expand Down
47 changes: 45 additions & 2 deletions pkg/common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
volIDRegionFmt = "projects/%s/regions/%s/disks/%s"
)

func TestBytesToGb(t *testing.T) {
func TestBytesToGbRoundDown(t *testing.T) {
testCases := []struct {
name string
bytes int64
Expand Down Expand Up @@ -58,7 +58,50 @@ func TestBytesToGb(t *testing.T) {
}
for _, tc := range testCases {
t.Logf("test case: %s", tc.name)
gotGB := BytesToGb(tc.bytes)
gotGB := BytesToGbRoundDown(tc.bytes)

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

}
}

func TestBytesToGbRoundUp(t *testing.T) {
testCases := []struct {
name string
bytes int64
expGB int64
}{
{
name: "normal 5gb",
bytes: 5368709120,
expGB: 5,
},
{
name: "slightly less than 5gb",
bytes: 5368709119,
expGB: 5,
},
{
name: "slightly more than 5gb",
bytes: 5368709121,
expGB: 6,
},
{
name: "1.5Gi",
bytes: 1610612736,
expGB: 2,
},
{
name: "zero",
bytes: 0,
expGB: 0,
},
}
for _, tc := range testCases {
t.Logf("test case: %s", tc.name)
gotGB := BytesToGbRoundUp(tc.bytes)

if gotGB != tc.expGB {
t.Errorf("got GB %v, expected %v", gotGB, tc.expGB)
Expand Down
8 changes: 5 additions & 3 deletions pkg/gce-cloud-provider/compute/fake-gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, volKey *meta.Key

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

disk.setSizeGb(common.BytesToGb(requestBytes))
requestSizGb := common.BytesToGbRoundUp(requestBytes)

return common.BytesToGb(requestBytes), nil
disk.setSizeGb(requestSizGb)

return requestSizGb, nil

}

Expand Down
6 changes: 3 additions & 3 deletions pkg/gce-cloud-provider/compute/gce-compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (cloud *CloudProvider) insertRegionalDisk(

diskToCreate := &computev1.Disk{
Name: volKey.Name,
SizeGb: common.BytesToGb(capBytes),
SizeGb: common.BytesToGbRoundUp(capBytes),
Description: description,
Type: cloud.GetDiskTypeURI(volKey, params.DiskType),
}
Expand Down Expand Up @@ -474,7 +474,7 @@ func (cloud *CloudProvider) insertZonalDisk(

diskToCreate := &computev1.Disk{
Name: volKey.Name,
SizeGb: common.BytesToGb(capBytes),
SizeGb: common.BytesToGbRoundUp(capBytes),
Description: description,
Type: cloud.GetDiskTypeURI(volKey, params.DiskType),
}
Expand Down Expand Up @@ -815,7 +815,7 @@ func (cloud *CloudProvider) ResizeDisk(ctx context.Context, volKey *meta.Key, re
}

sizeGb := cloudDisk.GetSizeGb()
requestGb := common.BytesToGb(requestBytes)
requestGb := common.BytesToGbRoundUp(requestBytes)

// If disk is already of size equal or greater than requested size, we
// simply return the found size
Expand Down
9 changes: 5 additions & 4 deletions pkg/gce-pd-csi-driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre

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

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

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

}

Expand Down Expand Up @@ -979,16 +979,17 @@ func getDefaultZonesInRegion(ctx context.Context, gceCS *GCEControllerServer, ex
return ret, nil
}

func generateCreateVolumeResponse(disk *gce.CloudDisk, capBytes int64, zones []string) *csi.CreateVolumeResponse {
func generateCreateVolumeResponse(disk *gce.CloudDisk, zones []string) *csi.CreateVolumeResponse {
tops := []*csi.Topology{}
for _, zone := range zones {
tops = append(tops, &csi.Topology{
Segments: map[string]string{common.TopologyKeyZone: zone},
})
}
realDiskSizeBytes := common.GbToBytes(disk.GetSizeGb())
createResp := &csi.CreateVolumeResponse{
Volume: &csi.Volume{
CapacityBytes: capBytes,
CapacityBytes: realDiskSizeBytes,
VolumeId: cleanSelfLink(disk.GetSelfLink()),
VolumeContext: nil,
AccessibleTopology: tops,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func GetBlockSizeInGb(instance *remote.InstanceInfo, devicePath string) (int64,
if err != nil {
return -1, fmt.Errorf("failed to parse size %s into int", output)
}
return utilcommon.BytesToGb(n), nil
return utilcommon.BytesToGbRoundDown(n), nil
}

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