Repository: kubernetes-sigs/gcp-compute-persistent-disk-csi-driver
Pull request #684 (2020-12-16 00:04:45)

Round up pdcsi driver size in CreateVolume
<!--  Thanks for sending a pull request!  Here are some tips for you:

1. If this is your first time, please read our contributor guidelines: https://git.k8s.io/community/contributors/guide#your-first-contribution and developer guide https://git.k8s.io/community/contributors/devel/development.md#development-guide
2. Follow the instructions for writing a release note: https://git.k8s.io/community/contributors/guide/release-notes.md
3. If the PR is unfinished, see how to mark it: https://git.k8s.io/community/contributors/guide/pull-requests.md#marking-unfinished-pull-requests
-->

**What type of PR is this?**
/kind bug

**What this PR does / why we need it**:
Currently if we create a PVC of size 1.5Gi, the pdcsi driver will convert it to 1Gi but the provisioner will say it is 2Gi[1]. And both of them are not accurate. From the CSI spec, we know that we should provide size larger than the requested size. So if user ask for 1.5Gi, we should give them at least 1.5Gi. 

This PR changes the RoundDown behavior of CreateVolume to RoundUp. This is to align with the CreateVolume CSI spec that when the required_bytes is set, the driver should provision something bigger.

[1] https://github.com/kubernetes-csi/external-provisioner/issues/199

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note
Fix a bug that CreateVolume should round up the request_bytes.
```

/cc @msau42 
/cc @saikat-royc 
/assign @mattcary 


k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/pull/684#" title="Author self-approved">Jiawei0227</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **verult** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @verult` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fgcp-compute-persistent-disk-csi-driver).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["verult"]} -->
k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/pull/684#" title="Author self-approved">Jiawei0227</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **mattcary** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @mattcary` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fgcp-compute-persistent-disk-csi-driver).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["mattcary"]} -->

Patch:
diff --git a/pkg/common/utils.go b/pkg/common/utils.go
index d5fa8f89..c48045e3 100644
--- a/pkg/common/utils.go
+++ b/pkg/common/utils.go
@@ -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
diff --git a/pkg/gce-cloud-provider/compute/fake-gce.go b/pkg/gce-cloud-provider/compute/fake-gce.go
index 93da6fb5..910d6bcf 100644
--- a/pkg/gce-cloud-provider/compute/fake-gce.go
+++ b/pkg/gce-cloud-provider/compute/fake-gce.go
@@ -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,
@@ -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
 
 }
 
diff --git a/pkg/gce-cloud-provider/compute/gce-compute.go b/pkg/gce-cloud-provider/compute/gce-compute.go
index afca4bba..760f5a40 100644
--- a/pkg/gce-cloud-provider/compute/gce-compute.go
+++ b/pkg/gce-cloud-provider/compute/gce-compute.go
@@ -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),
 	}
@@ -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),
 	}
@@ -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
diff --git a/pkg/gce-pd-csi-driver/controller.go b/pkg/gce-pd-csi-driver/controller.go
index 6974b18a..5d164db2 100644
--- a/pkg/gce-pd-csi-driver/controller.go
+++ b/pkg/gce-pd-csi-driver/controller.go
@@ -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 := ""
@@ -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
 
 }
 
@@ -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,


Test patch:
diff --git a/pkg/common/utils_test.go b/pkg/common/utils_test.go
index 240d0c3b..57de58c3 100644
--- a/pkg/common/utils_test.go
+++ b/pkg/common/utils_test.go
@@ -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
@@ -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)
diff --git a/test/e2e/utils/utils.go b/test/e2e/utils/utils.go
index f13c3195..c901e235 100644
--- a/test/e2e/utils/utils.go
+++ b/test/e2e/utils/utils.go
@@ -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 {
diff --git a/test/k8s-integration/main.go b/test/k8s-integration/main.go
index e2dcfb30..83680fea 100644
--- a/test/k8s-integration/main.go
+++ b/test/k8s-integration/main.go
@@ -513,12 +513,6 @@ func generateGCETestSkip(testParams *testParameters) string {
 	if v.LessThan(apimachineryversion.MustParseSemantic("1.16.0")) {
 		skipString = skipString + "|volumeMode\\sshould\\snot\\smount\\s/\\smap\\sunused\\svolumes\\sin\\sa\\spod"
 	}
-
-	// ExpandCSIVolumes feature is beta in k8s 1.16
-	if v.LessThan(apimachineryversion.MustParseSemantic("1.16.0")) {
-		skipString = skipString + "|allowExpansion"
-	}
-
 	if v.LessThan(apimachineryversion.MustParseSemantic("1.17.0")) {
 		skipString = skipString + "|VolumeSnapshotDataSource"
 	}
@@ -542,7 +536,7 @@ func generateGKETestSkip(testParams *testParameters) string {
 	// "volumeMode should not mount / map unused volumes in a pod" tests a
 	// (https://github.com/kubernetes/kubernetes/pull/81163)
 	// bug-fix introduced in 1.16
-	if curVer.lessThan(mustParseVersion("1.16.0")) || (nodeVer != nil && nodeVer.lessThan(mustParseVersion("1.16.0"))) {
+	if curVer.lessThan(mustParseVersion("1.16.0")) {
 		skipString = skipString + "|volumeMode\\sshould\\snot\\smount\\s/\\smap\\sunused\\svolumes\\sin\\sa\\spod"
 	}
 
@@ -551,11 +545,9 @@ func generateGKETestSkip(testParams *testParameters) string {
 		skipString = skipString + "|fsgroupchangepolicy"
 	}
 
-	// ExpandCSIVolumes feature is beta in k8s 1.16
 	// For GKE deployed PD CSI driver, resizer sidecar is enabled in 1.16.8-gke.3
 	if (testParams.useGKEManagedDriver && curVer.lessThan(mustParseVersion("1.16.8-gke.3"))) ||
-		(!testParams.useGKEManagedDriver && curVer.lessThan(mustParseVersion("1.16.0")) ||
-			(nodeVer != nil && nodeVer.lessThan(mustParseVersion("1.16.0")))) {
+		(!testParams.useGKEManagedDriver && curVer.lessThan(mustParseVersion("1.16.0"))) {
 		skipString = skipString + "|allowExpansion"
 	}