util: use MiB and GiB constants from k8s helpers

Signed-off-by: wilmardo <info@wilmardenouden.nl>
This commit is contained in:
wilmardo 2020-01-17 16:44:06 +01:00 committed by mergify[bot]
parent 4d60e76241
commit 157e8f4c75
2 changed files with 9 additions and 15 deletions

View File

@ -38,6 +38,7 @@ import (
"github.com/pborman/uuid" "github.com/pborman/uuid"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cloud-provider/volume/helpers"
"k8s.io/klog" "k8s.io/klog"
) )
@ -168,7 +169,7 @@ func createImage(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) er
defer ioctx.Destroy() defer ioctx.Destroy()
err = librbd.CreateImage(ioctx, pOpts.RbdImageName, err = librbd.CreateImage(ioctx, pOpts.RbdImageName,
uint64(util.RoundOffVolSize(pOpts.VolSize)*util.MiB), options) uint64(util.RoundOffVolSize(pOpts.VolSize)*helpers.MiB), options)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to create rbd image") return errors.Wrapf(err, "failed to create rbd image")
} }

View File

@ -28,23 +28,16 @@ import (
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/cloud-provider/volume/helpers"
"k8s.io/klog" "k8s.io/klog"
"k8s.io/utils/mount" "k8s.io/utils/mount"
) )
// remove this once kubernetes v1.14.0 release is done
// https://github.com/kubernetes/cloud-provider/blob/master/volume/helpers/rounding.go
const (
// MiB - MebiByte size
MiB = 1024 * 1024
GiB = MiB * 1024
)
// RoundOffVolSize rounds up given quantity upto chunks of MiB/GiB // RoundOffVolSize rounds up given quantity upto chunks of MiB/GiB
func RoundOffVolSize(size int64) int64 { func RoundOffVolSize(size int64) int64 {
size = RoundOffBytes(size) size = RoundOffBytes(size)
// convert size back to MiB for rbd CLI // convert size back to MiB for rbd CLI
return size / MiB return size / helpers.MiB
} }
// RoundOffBytes converts roundoff the size // RoundOffBytes converts roundoff the size
@ -54,12 +47,12 @@ func RoundOffBytes(bytes int64) int64 {
var num int64 var num int64
floatBytes := float64(bytes) floatBytes := float64(bytes)
// round off the value if its in decimal // round off the value if its in decimal
if floatBytes < GiB { if floatBytes < helpers.GiB {
num = int64(math.Ceil(floatBytes / MiB)) num = int64(math.Ceil(floatBytes / helpers.MiB))
num *= MiB num *= helpers.MiB
} else { } else {
num = int64(math.Ceil(floatBytes / GiB)) num = int64(math.Ceil(floatBytes / helpers.GiB))
num *= GiB num *= helpers.GiB
} }
return num return num
} }