Skip to content

Commit 22af5be

Browse files
authored
Merge pull request #79 from lpabon/pick78
Made storage shown in PV more readable
2 parents d36115a + af61029 commit 22af5be

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

pkg/controller/controller.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controller
1919
import (
2020
"context"
2121
"fmt"
22+
"math"
2223
"net"
2324
"strings"
2425
"time"
@@ -359,7 +360,6 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
359360
}
360361
return nil, capErr
361362
}
362-
repBytesString := fmt.Sprintf("%v", respCap)
363363
pv := &v1.PersistentVolume{
364364
ObjectMeta: metav1.ObjectMeta{
365365
Name: share,
@@ -368,7 +368,7 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
368368
PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy,
369369
AccessModes: options.PVC.Spec.AccessModes,
370370
Capacity: v1.ResourceList{
371-
v1.ResourceName(v1.ResourceStorage): resource.MustParse(repBytesString),
371+
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(respCap),
372372
},
373373
// TODO wait for CSI VolumeSource API
374374
PersistentVolumeSource: v1.PersistentVolumeSource{
@@ -464,3 +464,23 @@ func getCredentialsFromSecret(k8s kubernetes.Interface, secretName, nameSpace st
464464

465465
return credentials, nil
466466
}
467+
468+
func bytesToGiQuantity(bytes int64) resource.Quantity {
469+
var num int64
470+
var floatBytes, MiB, GiB float64
471+
var suffix string
472+
floatBytes = float64(bytes)
473+
MiB = 1024 * 1024
474+
GiB = MiB * 1024
475+
// Need to give Quantity nice whole numbers or else it
476+
// sometimes spits out the value in milibytes. We round up.
477+
if floatBytes < GiB {
478+
num = int64(math.Ceil(floatBytes / MiB))
479+
suffix = "Mi"
480+
} else {
481+
num = int64(math.Ceil(floatBytes / GiB))
482+
suffix = "Gi"
483+
}
484+
stringQuantity := fmt.Sprintf("%v%s", num, suffix)
485+
return resource.MustParse(stringQuantity)
486+
}

pkg/controller/controller_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,49 @@ func TestGetDriverName(t *testing.T) {
390390
}
391391
}
392392

393+
func TestBytesToQuantity(t *testing.T) {
394+
tests := []struct {
395+
testName string
396+
bytes float64
397+
quantString string
398+
}{
399+
{
400+
"Gibibyte rounding up from above .5",
401+
5.56 * 1024 * 1024 * 1024,
402+
"6Gi",
403+
},
404+
{
405+
"Gibibyte rounding up from below .5",
406+
5.23 * 1024 * 1024 * 1024,
407+
"6Gi",
408+
},
409+
{
410+
"Gibibyte exact",
411+
5 * 1024 * 1024 * 1024,
412+
"5Gi",
413+
},
414+
{
415+
"Mebibyte rounding up from below .5",
416+
5.23 * 1024 * 1024,
417+
"6Mi",
418+
},
419+
{
420+
"Mebibyte/Gibibyte barrier (Quantity type rounds this)",
421+
// (1024 * 1024 * 1024) - 1
422+
1073741823,
423+
"1Gi",
424+
},
425+
}
426+
427+
for _, test := range tests {
428+
q := bytesToGiQuantity(int64(test.bytes))
429+
if q.String() != test.quantString {
430+
t.Errorf("test: %s, expected: %v, got: %v", test.testName, test.quantString, q.String())
431+
}
432+
}
433+
434+
}
435+
393436
func TestCreateDriverReturnsInvalidCapacityDuringProvision(t *testing.T) {
394437
// Set up mocks
395438
var requestedBytes int64 = 100

0 commit comments

Comments
 (0)