diff --git a/internal/cephfs/controllerserver.go b/internal/cephfs/controllerserver.go index f638f3ef6..648bcba04 100644 --- a/internal/cephfs/controllerserver.go +++ b/internal/cephfs/controllerserver.go @@ -277,7 +277,7 @@ func (cs *ControllerServer) CreateVolume( defer volOptions.Destroy() if req.GetCapacityRange() != nil { - volOptions.Size = util.RoundOffBytes(req.GetCapacityRange().GetRequiredBytes()) + volOptions.Size = util.RoundOffCephFSVolSize(req.GetCapacityRange().GetRequiredBytes()) } parentVol, pvID, sID, err := checkContentSource(ctx, req, cr) @@ -672,7 +672,8 @@ func (cs *ControllerServer) ControllerExpandVolume( return nil, status.Error(codes.InvalidArgument, "cannot expand snapshot-backed volume") } - RoundOffSize := util.RoundOffBytes(req.GetCapacityRange().GetRequiredBytes()) + RoundOffSize := util.RoundOffCephFSVolSize(req.GetCapacityRange().GetRequiredBytes()) + volClient := core.NewSubVolume(volOptions.GetConnection(), &volOptions.SubVolume, volOptions.ClusterID) if err = volClient.ResizeVolume(ctx, RoundOffSize); err != nil { log.ErrorLog(ctx, "failed to expand volume %s: %v", fsutil.VolumeID(volIdentifier.FsSubvolName), err) diff --git a/internal/util/util.go b/internal/util/util.go index 857b7b19d..4c36e3679 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -58,6 +58,22 @@ func RoundOffBytes(bytes int64) int64 { return num } +// RoundOffCephFSVolSize rounds up the bytes to 4MiB if the request is less +// than 4MiB or if its greater it rounds up to multiple of 4MiB. +func RoundOffCephFSVolSize(bytes int64) int64 { + // Minimum supported size is 1MiB in CephCSI, if the request is <4MiB, + // round off to 4MiB. + if bytes < helpers.MiB { + return 4 * helpers.MiB + } + + bytes /= helpers.MiB + + bytes = int64(math.Ceil(float64(bytes)/4) * 4) + + return RoundOffBytes(bytes * helpers.MiB) +} + // variables which will be set during the build time. var ( // GitCommit tell the latest git commit image is built from. diff --git a/internal/util/util_test.go b/internal/util/util_test.go index d4cb39680..6e2e0a13d 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -352,3 +352,52 @@ func TestCheckKernelSupport(t *testing.T) { } } } + +func TestRoundOffCephFSVolSize(t *testing.T) { + t.Parallel() + tests := []struct { + name string + size int64 + want int64 + }{ + { + "1000kiB conversion", + 1000, + 4194304, // 4 MiB + }, + { + "1MiB conversions", + 1048576, + 4194304, // 4 MiB + }, + { + "1.5Mib conversion", + 1677722, + 4194304, // 4 MiB + }, + { + "1023MiB conversion", + 1072693248, + 1073741824, // 1024 MiB + }, + { + "1.5GiB conversion", + 1585446912, + 2147483648, // 2 GiB + }, + { + "1555MiB conversion", + 1630535680, + 2147483648, // 2 GiB + }, + } + for _, tt := range tests { + ts := tt + t.Run(ts.name, func(t *testing.T) { + t.Parallel() + if got := RoundOffCephFSVolSize(ts.size); got != ts.want { + t.Errorf("RoundOffCephFSVolSize() = %v, want %v", got, ts.want) + } + }) + } +}