Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(zfspv): rounding off the volume size to Gi and Mi #191

Merged
merged 2 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ env:
- MINIKUBE_HOME=$HOME
- CHANGE_MINIKUBE_NONE_USER=true
- KUBECONFIG=$HOME/.kube/config
- OPENEBS_NAMESPACE=openebs
- NodeID=$HOSTNAME
services:
- docker
language: go
Expand Down
1 change: 1 addition & 0 deletions changelogs/unreleased/191-pawanpraka1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rounding off the volume size to Gi and Mi
3 changes: 0 additions & 3 deletions ci/ci-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

set -e

export OPENEBS_NAMESPACE="openebs"
export NodeID=$HOSTNAME

ZFS_OPERATOR=deploy/zfs-operator.yaml
SNAP_CLASS=deploy/sample/zfssnapclass.yaml

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.5.1
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 // indirect
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9
golang.org/x/sys v0.0.0-20190902133755-9109b7679e13
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRci
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/syndtr/gocapability v0.0.0-20160928074757-e7cb7fa329f4/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down
33 changes: 29 additions & 4 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ import (
zfs "github.com/openebs/zfs-localpv/pkg/zfs"
)

// size constants
const (
MB = 1000 * 1000
GB = 1000 * 1000 * 1000
Mi = 1024 * 1024
Gi = 1024 * 1024 * 1024
)

// controller is the server implementation
// for CSI Controller
type controller struct {
Expand Down Expand Up @@ -75,10 +83,26 @@ func sendEventOrIgnore(pvcName, pvName, capacity, stgType, method string) {
}
}

// getRoundedCapacity rounds the capacity on 1024 base
func getRoundedCapacity(size int64) int64 {

/*
* volblocksize and recordsize must be power of 2 from 512B to 1M
* so keeping the size in the form of Gi or Mi should be
* sufficient to make volsize multiple of volblocksize/recordsize.
*/
if size > Gi {
return ((size + Gi - 1) / Gi) * Gi
}

// Keeping minimum allocatable size as 1Mi (1024 * 1024)
return ((size + Mi - 1) / Mi) * Mi
}

// CreateZFSVolume create new zfs volume from csi volume request
func CreateZFSVolume(req *csi.CreateVolumeRequest) (string, error) {
volName := req.GetName()
size := req.GetCapacityRange().RequiredBytes
size := getRoundedCapacity(req.GetCapacityRange().RequiredBytes)

// parameter keys may be mistyped from the CRD specification when declaring
// the storageclass, which kubectl validation will not catch. Because ZFS
Expand Down Expand Up @@ -148,7 +172,7 @@ func CreateZFSClone(req *csi.CreateVolumeRequest, snapshot string) (string, erro
parameters := req.GetParameters()
// lower case keys, cf CreateZFSVolume()
pool := helpers.GetInsensitiveParameter(&parameters, "poolname")
size := req.GetCapacityRange().RequiredBytes
size := getRoundedCapacity(req.GetCapacityRange().RequiredBytes)
volsize := strconv.FormatInt(int64(size), 10)

snapshotID := strings.Split(snapshot, "@")
Expand Down Expand Up @@ -208,7 +232,7 @@ func (cs *controller) CreateVolume(
parameters := req.GetParameters()
// lower case keys, cf CreateZFSVolume()
pool := helpers.GetInsensitiveParameter(&parameters, "poolname")
size := req.GetCapacityRange().RequiredBytes
size := getRoundedCapacity(req.GetCapacityRange().RequiredBytes)
contentSource := req.GetVolumeContentSource()
pvcName := helpers.GetInsensitiveParameter(&parameters, "csi.storage.k8s.io/pvc/name")

Expand Down Expand Up @@ -325,7 +349,8 @@ func (cs *controller) ControllerExpandVolume(
req *csi.ControllerExpandVolumeRequest,
) (*csi.ControllerExpandVolumeResponse, error) {

updatedSize := req.GetCapacityRange().GetRequiredBytes()
/* round off the new size */
updatedSize := getRoundedCapacity(req.GetCapacityRange().GetRequiredBytes())

vol, err := zfs.GetZFSVolume(req.VolumeId)
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2020 The OpenEBS Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package driver

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRoundOff(t *testing.T) {

tests := map[string]struct {
input int64
expected int64
}{
"Minimum allocatable is 1Mi": {input: 1, expected: Mi},
"roundOff to same Mi size": {input: Mi, expected: Mi},
"roundOff to nearest Mi": {input: Mi + 1, expected: Mi * 2},
"roundOff to same Gi size": {input: Gi, expected: Gi},
"roundOff to nearest Gi": {input: Gi + 1, expected: Gi * 2},
kmova marked this conversation as resolved.
Show resolved Hide resolved
"roundOff MB size": {input: 5 * MB, expected: 5 * Mi},
"roundOff GB size": {input: 5 * GB, expected: 5 * Gi},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, getRoundedCapacity(test.input))
})
}
}
27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading