diff --git a/pkg/rbd/controllerserver.go b/pkg/rbd/controllerserver.go index 598451f9e..6206aef57 100644 --- a/pkg/rbd/controllerserver.go +++ b/pkg/rbd/controllerserver.go @@ -24,7 +24,7 @@ import ( "strings" "syscall" - "github.com/ceph/ceph-csi/pkg/csi-common" + csicommon "github.com/ceph/ceph-csi/pkg/csi-common" "github.com/ceph/ceph-csi/pkg/util" "github.com/container-storage-interface/spec/lib/go/csi" @@ -118,7 +118,8 @@ func parseVolCreateRequest(req *csi.CreateVolumeRequest) (*rbdVolume, error) { if req.GetCapacityRange() != nil { volSizeBytes = req.GetCapacityRange().GetRequiredBytes() } - rbdVol.VolSize = volSizeBytes + + rbdVol.VolSize = util.RoundUpToMiB(volSizeBytes) return rbdVol, nil } @@ -175,10 +176,8 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol return nil, err } - volSizeGB := int(rbdVol.VolSize / 1024 / 1024 / 1024) - // Check if there is already RBD image with requested name - err = cs.checkRBDStatus(rbdVol, req, volSizeGB) + err = cs.checkRBDStatus(rbdVol, req, int(rbdVol.VolSize)) if err != nil { return nil, err } @@ -192,13 +191,13 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol return &csi.CreateVolumeResponse{ Volume: &csi.Volume{ VolumeId: rbdVol.VolID, - CapacityBytes: rbdVol.VolSize, + CapacityBytes: rbdVol.VolSize * util.MiB, VolumeContext: req.GetParameters(), }, }, nil } -func (cs *ControllerServer) checkRBDStatus(rbdVol *rbdVolume, req *csi.CreateVolumeRequest, volSizeGB int) error { +func (cs *ControllerServer) checkRBDStatus(rbdVol *rbdVolume, req *csi.CreateVolumeRequest, volSizeMiB int) error { var err error // Check if there is already RBD image with requested name found, _, _ := rbdStatus(rbdVol, rbdVol.UserID, req.GetSecrets()) // #nosec @@ -209,7 +208,7 @@ func (cs *ControllerServer) checkRBDStatus(rbdVol *rbdVolume, req *csi.CreateVol return err } } else { - err = createRBDImage(rbdVol, volSizeGB, rbdVol.AdminID, req.GetSecrets()) + err = createRBDImage(rbdVol, volSizeMiB, rbdVol.AdminID, req.GetSecrets()) if err != nil { klog.Warningf("failed to create volume: %v", err) return err diff --git a/pkg/rbd/rbd_util.go b/pkg/rbd/rbd_util.go index 36f655ec5..25c7a7b27 100644 --- a/pkg/rbd/rbd_util.go +++ b/pkg/rbd/rbd_util.go @@ -121,18 +121,18 @@ func createRBDImage(pOpts *rbdVolume, volSz int, adminID string, credentials map } image := pOpts.VolName - volSzGB := fmt.Sprintf("%dG", volSz) + volSzMiB := fmt.Sprintf("%dM", volSz) key, err := getRBDKey(adminID, credentials) if err != nil { return err } if pOpts.ImageFormat == rbdImageFormat2 { - klog.V(4).Infof("rbd: create %s size %s format %s (features: %s) using mon %s, pool %s ", image, volSzGB, pOpts.ImageFormat, pOpts.ImageFeatures, mon, pOpts.Pool) + klog.V(4).Infof("rbd: create %s size %s format %s (features: %s) using mon %s, pool %s ", image, volSzMiB, pOpts.ImageFormat, pOpts.ImageFeatures, mon, pOpts.Pool) } else { - klog.V(4).Infof("rbd: create %s size %s format %s using mon %s, pool %s", image, volSzGB, pOpts.ImageFormat, mon, pOpts.Pool) + klog.V(4).Infof("rbd: create %s size %s format %s using mon %s, pool %s", image, volSzMiB, pOpts.ImageFormat, mon, pOpts.Pool) } - args := []string{"create", image, "--size", volSzGB, "--pool", pOpts.Pool, "--id", adminID, "-m", mon, "--key=" + key, "--image-format", pOpts.ImageFormat} + args := []string{"create", image, "--size", volSzMiB, "--pool", pOpts.Pool, "--id", adminID, "-m", mon, "--key=" + key, "--image-format", pOpts.ImageFormat} if pOpts.ImageFormat == rbdImageFormat2 { args = append(args, "--image-feature", pOpts.ImageFeatures) } diff --git a/pkg/util/util.go b/pkg/util/util.go index 2382f87d7..ef8f37d50 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -23,6 +23,27 @@ import ( "k8s.io/klog" ) +// 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 +) + +// RoundUpToMiB rounds up given quantity upto chunks of MiB +func RoundUpToMiB(size int64) int64 { + requestBytes := size + return roundUpSize(requestBytes, MiB) +} + +func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 { + roundedUp := volumeSizeBytes / allocationUnitBytes + if volumeSizeBytes%allocationUnitBytes > 0 { + roundedUp++ + } + return roundedUp +} + // CreatePersistanceStorage creates storage path and initializes new cache func CreatePersistanceStorage(sPath, metaDataStore, driverName string) (CachePersister, error) { var err error