From ca00d4af80df94b22dd8fd5a997f139ed524b466 Mon Sep 17 00:00:00 2001 From: Prateek Pandey Date: Fri, 6 Dec 2019 12:50:12 +0530 Subject: [PATCH] refact(size): conversion G to Gi (#59) Signed-off-by: prateekpandey14 --- pkg/utils/v1alpha1/maya.go | 27 +++++++++++++--------- pkg/utils/v1alpha1/rounding.go | 41 +++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/pkg/utils/v1alpha1/maya.go b/pkg/utils/v1alpha1/maya.go index 8333b9d4f..841a909e4 100644 --- a/pkg/utils/v1alpha1/maya.go +++ b/pkg/utils/v1alpha1/maya.go @@ -69,14 +69,16 @@ func ProvisionVolume( CVCFinalizer, } - sSize := ByteCount(uint64(size)) + requestGIB := RoundUpGiB(size) + sSize := resource.MustParse(fmt.Sprintf("%dGi", requestGIB)) + cvcObj, err := cvc.NewBuilder(). WithName(volName). WithNamespace(OpenEBSNamespace). WithAnnotations(annotations). WithLabelsNew(labels). WithFinalizers(finalizers). - WithCapacity(sSize). + WithCapacityQty(sSize). WithSource(snapshotID). WithReplicaCount(replicaCount). WithNewVersion(version.Current()). @@ -185,12 +187,14 @@ func ResizeVolume( size int64, ) error { - sSize := ByteCount(uint64(size)) + requestGIB := RoundUpGiB(size) + desiredSize := resource.MustParse(fmt.Sprintf("%dGi", requestGIB)) + cvc, err := getCVC(volumeID) if err != nil { return err } - desiredSize, _ := resource.ParseQuantity(sSize) + cvcDesiredSize := cvc.Spec.Capacity[corev1.ResourceStorage] if (desiredSize).Cmp(cvcDesiredSize) < 0 { @@ -199,7 +203,7 @@ func ResizeVolume( } if cvc.Status.Phase == apismaya.CStorVolumeClaimPhasePending { - return handleResize(cvc, sSize) + return handleResize(cvc, desiredSize) } cvcActualSize := cvc.Status.Capacity[corev1.ResourceStorage] @@ -211,12 +215,12 @@ func ResizeVolume( if (desiredSize).Cmp(cvcActualSize) == 0 { return nil } - return handleResize(cvc, sSize) + return handleResize(cvc, desiredSize) } func handleResize( - cvc *apismaya.CStorVolumeClaim, sSize string, + cvc *apismaya.CStorVolumeClaim, sSize resource.Quantity, ) error { if err := updateCVCSize(cvc, sSize); err != nil { return err @@ -227,14 +231,14 @@ func handleResize( return waitAndReverifyResizeStatus(cvc.Name, sSize) } -func waitAndReverifyResizeStatus(cvcName, sSize string) error { +func waitAndReverifyResizeStatus(cvcName string, sSize resource.Quantity) error { time.Sleep(5 * time.Second) cvcObj, err := getCVC(cvcName) if err != nil { return err } - desiredSize, _ := resource.ParseQuantity(sSize) + desiredSize := sSize cvcActualSize := cvcObj.Status.Capacity[corev1.ResourceStorage] if (desiredSize).Cmp(cvcActualSize) != 0 { return fmt.Errorf("ResizeInProgress from: %v to: %v", @@ -242,9 +246,10 @@ func waitAndReverifyResizeStatus(cvcName, sSize string) error { } return nil } -func updateCVCSize(oldCVCObj *apismaya.CStorVolumeClaim, sSize string) error { + +func updateCVCSize(oldCVCObj *apismaya.CStorVolumeClaim, sSize resource.Quantity) error { newCVCObj, err := cvc.BuildFrom(oldCVCObj.DeepCopy()). - WithCapacity(sSize).Build() + WithCapacityQty(sSize).Build() if err != nil { return err } diff --git a/pkg/utils/v1alpha1/rounding.go b/pkg/utils/v1alpha1/rounding.go index 19d60c67e..5af0175b1 100644 --- a/pkg/utils/v1alpha1/rounding.go +++ b/pkg/utils/v1alpha1/rounding.go @@ -16,10 +16,14 @@ limitations under the License. package utils -import "fmt" +import ( + "fmt" +) const ( unit = 1024 + // GiB ... + GiB = 1024 * 1024 * 1024 ) // ByteCount converts bytes into corresponding unit @@ -35,3 +39,38 @@ func ByteCount(b uint64) string { return fmt.Sprintf("%d%ci", uint64(b)/uint64(div), "KMGTPE"[index]) } + +// RoundUpBytes rounds up the volume size in bytes upto multiplications of GiB +// in the unit of Bytes +func RoundUpBytes(volumeSizeBytes int64) int64 { + return roundUpSize(volumeSizeBytes, GiB) * GiB +} + +// RoundUpGiB rounds up the volume size in bytes upto multiplications of GiB +// in the unit of GiB +func RoundUpGiB(volumeSizeBytes int64) int64 { + return roundUpSize(volumeSizeBytes, GiB) +} + +// BytesToGiB converts Bytes to GiB +func BytesToGiB(volumeSizeBytes int64) int64 { + return volumeSizeBytes / GiB +} + +// GiBToBytes converts GiB to Bytes +func GiBToBytes(volumeSizeGiB int64) int64 { + return volumeSizeGiB * GiB +} + +// roundUpSize calculates how many allocation units are needed to accommodate +// a volume of given size. E.g. when user wants 1500MiB volume, while AWS +// EBS,GKE etc allocates volumes in gibibyte-sized chunks, +// RoundUpSize(1500 * 1024*1024, 1024*1024*1024) returns '2' +// (2 GiB is the smallest allocatable volume that can hold 1500MiB) +func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 { + roundedUp := volumeSizeBytes / allocationUnitBytes + if volumeSizeBytes%allocationUnitBytes > 0 { + roundedUp++ + } + return roundedUp +}