Skip to content
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

Made storage shown in PV more readable #78

Merged
merged 1 commit into from
Apr 20, 2018
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
24 changes: 22 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"context"
"fmt"
"math"
"net"
"strings"
"time"
Expand Down Expand Up @@ -359,7 +360,6 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
}
return nil, capErr
}
repBytesString := fmt.Sprintf("%v", respCap)
pv := &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: share,
Expand All @@ -368,7 +368,7 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy,
AccessModes: options.PVC.Spec.AccessModes,
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): resource.MustParse(repBytesString),
v1.ResourceName(v1.ResourceStorage): bytesToGiQuantity(respCap),
},
// TODO wait for CSI VolumeSource API
PersistentVolumeSource: v1.PersistentVolumeSource{
Expand Down Expand Up @@ -464,3 +464,23 @@ func getCredentialsFromSecret(k8s kubernetes.Interface, secretName, nameSpace st

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)
}
43 changes: 43 additions & 0 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,49 @@ func TestGetDriverName(t *testing.T) {
}
}

func TestBytesToQuantity(t *testing.T) {
tests := []struct {
testName string
bytes float64
quantString string
}{
{
"Gibibyte rounding up from above .5",
5.56 * 1024 * 1024 * 1024,
"6Gi",
},
{
"Gibibyte rounding up from below .5",
5.23 * 1024 * 1024 * 1024,
"6Gi",
},
{
"Gibibyte exact",
5 * 1024 * 1024 * 1024,
"5Gi",
},
{
"Mebibyte rounding up from below .5",
5.23 * 1024 * 1024,
"6Mi",
},
{
"Mebibyte/Gibibyte barrier (Quantity type rounds this)",
// (1024 * 1024 * 1024) - 1
1073741823,
"1Gi",
},
}

for _, test := range tests {
q := bytesToGiQuantity(int64(test.bytes))
if q.String() != test.quantString {
t.Errorf("test: %s, expected: %v, got: %v", test.testName, test.quantString, q.String())
}
}

}

func TestCreateDriverReturnsInvalidCapacityDuringProvision(t *testing.T) {
// Set up mocks
var requestedBytes int64 = 100
Expand Down