Skip to content

Commit

Permalink
Merge pull request #541 from Jiawei0227/mismatch
Browse files Browse the repository at this point in the history
fix volume size mismatch between driver and PV
  • Loading branch information
k8s-ci-robot authored Dec 17, 2020
2 parents 41af8a3 + 875f274 commit 10a49df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 54 deletions.
24 changes: 4 additions & 20 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"math"
"os"
"strings"
"time"
Expand Down Expand Up @@ -810,7 +809,7 @@ func (p *csiProvisioner) Provision(ctx context.Context, options controller.Provi
AccessModes: options.PVC.Spec.AccessModes,
MountOptions: options.StorageClass.MountOptions,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(respCap),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(respCap),
},
// TODO wait for CSI VolumeSource API
PersistentVolumeSource: v1.PersistentVolumeSource{
Expand Down Expand Up @@ -1641,24 +1640,9 @@ func getCredentials(ctx context.Context, k8s kubernetes.Interface, ref *v1.Secre
return credentials, nil
}

func bytesToGiQuantity(bytes int64) resource.Quantity {
var num int64
var floatBytes, MiB, GiB float64
var suffix string
floatBytes = float64(bytes)
MiB = 1024 * 1024
GiB = MiB * 1024
// Need to give Quantity nice whole numbers or else it
// sometimes spits out the value in milibytes. We round up.
if floatBytes < GiB {
num = int64(math.Ceil(floatBytes / MiB))
suffix = "Mi"
} else {
num = int64(math.Ceil(floatBytes / GiB))
suffix = "Gi"
}
stringQuantity := fmt.Sprintf("%v%s", num, suffix)
return resource.MustParse(stringQuantity)
func bytesToQuantity(bytes int64) resource.Quantity {
quantity := resource.NewQuantity(bytes, resource.BinarySI)
return *quantity
}

func deprecationWarning(deprecatedParam, newParam, removalVersion string) string {
Expand Down
68 changes: 34 additions & 34 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,35 +361,35 @@ func TestBytesToQuantity(t *testing.T) {
quantString string
}{
{
"Gibibyte rounding up from above .5",
"Gibibyte that cannot be put into any nice format without loss precision",
5.56 * 1024 * 1024 * 1024,
"6Gi",
"5970004541",
},
{
"Gibibyte rounding up from below .5",
5.23 * 1024 * 1024 * 1024,
"6Gi",
"Gibibyte that can be parsed nicer",
5.5 * 1024 * 1024 * 1024,
"5632Mi",
},
{
"Gibibyte exact",
5 * 1024 * 1024 * 1024,
"5Gi",
},
{
"Mebibyte rounding up from below .5",
"Mebibyte that cannot be parsed nicer",
5.23 * 1024 * 1024,
"6Mi",
"5484052",
},
{
"Mebibyte/Gibibyte barrier (Quantity type rounds this)",
// (1024 * 1024 * 1024) - 1
1073741823,
"1Gi",
"Kibibyte that can be parsed nicer",
// (100 * 1024)
102400,
"100Ki",
},
}

for _, test := range tests {
q := bytesToGiQuantity(int64(test.bytes))
q := bytesToQuantity(int64(test.bytes))
if q.String() != test.quantString {
t.Errorf("test: %s, expected: %v, got: %v", test.testName, test.quantString, q.String())
}
Expand Down Expand Up @@ -907,7 +907,7 @@ func TestFSTypeProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -935,7 +935,7 @@ func TestFSTypeProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -966,7 +966,7 @@ func TestFSTypeProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -996,7 +996,7 @@ func TestFSTypeProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1049,7 +1049,7 @@ func TestProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1078,7 +1078,7 @@ func TestProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1133,7 +1133,7 @@ func TestProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1179,7 +1179,7 @@ func TestProvision(t *testing.T) {
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteMany},
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1231,7 +1231,7 @@ func TestProvision(t *testing.T) {
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadOnlyMany},
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1283,7 +1283,7 @@ func TestProvision(t *testing.T) {
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1335,7 +1335,7 @@ func TestProvision(t *testing.T) {
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadOnlyMany, v1.ReadWriteOnce},
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1378,7 +1378,7 @@ func TestProvision(t *testing.T) {
expectedPVSpec: &pvSpec{
Name: "test-testi",
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
CSIPVS: &v1.CSIPersistentVolumeSource{
Expand Down Expand Up @@ -1429,7 +1429,7 @@ func TestProvision(t *testing.T) {
expectedPVSpec: &pvSpec{
Name: "test-testi",
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
CSIPVS: &v1.CSIPersistentVolumeSource{
Expand Down Expand Up @@ -1480,7 +1480,7 @@ func TestProvision(t *testing.T) {
expectedPVSpec: &pvSpec{
Name: "test-testi",
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
CSIPVS: &v1.CSIPersistentVolumeSource{
Expand Down Expand Up @@ -1522,7 +1522,7 @@ func TestProvision(t *testing.T) {
expectedPVSpec: &pvSpec{
Name: "test-testi",
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
VolumeMode: &volumeModeFileSystem,
Expand All @@ -1549,7 +1549,7 @@ func TestProvision(t *testing.T) {
expectedPVSpec: &pvSpec{
Name: "test-testi",
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
VolumeMode: &volumeModeBlock,
Expand Down Expand Up @@ -1673,7 +1673,7 @@ func TestProvision(t *testing.T) {
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
MountOptions: []string{"foo=bar", "baz=qux"},
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1780,7 +1780,7 @@ func TestProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1847,7 +1847,7 @@ func TestProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -1926,7 +1926,7 @@ func TestProvision(t *testing.T) {
Name: "test-testi",
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -2320,7 +2320,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down Expand Up @@ -3766,7 +3766,7 @@ func TestProvisionFromPVC(t *testing.T) {
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(requestedBytes),
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
},
CSIPVS: &v1.CSIPersistentVolumeSource{
Driver: "test-driver",
Expand Down

0 comments on commit 10a49df

Please sign in to comment.