Skip to content

Commit

Permalink
refact(size): conversion G to Gi (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: prateekpandey14 <[email protected]>
  • Loading branch information
prateekpandey14 authored and vishnuitta committed Dec 6, 2019
1 parent 90923f3 commit ca00d4a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
27 changes: 16 additions & 11 deletions pkg/utils/v1alpha1/maya.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()).
Expand Down Expand Up @@ -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 {
Expand All @@ -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]

Expand All @@ -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
Expand All @@ -227,24 +231,25 @@ 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",
cvcActualSize, desiredSize)
}
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
}
Expand Down
41 changes: 40 additions & 1 deletion pkg/utils/v1alpha1/rounding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit ca00d4a

Please sign in to comment.