@@ -19,6 +19,7 @@ package controller
1919import (
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+ }
0 commit comments