Skip to content

Commit 76722c0

Browse files
Change iops params directly convert string to int64
1 parent 7059368 commit 76722c0

File tree

6 files changed

+104
-64
lines changed

6 files changed

+104
-64
lines changed

Diff for: pkg/common/parameters.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
144144
p.Labels[labelKey] = labelValue
145145
}
146146
case ParameterKeyProvisionedIOPSOnCreate:
147-
paramProvisionedIOPSOnCreate, err := ConvertGiBStringToInt64(v)
147+
paramProvisionedIOPSOnCreate, err := ConvertStringToInt64(v)
148148
if err != nil {
149149
return p, fmt.Errorf("parameters contain invalid provisionedIOPSOnCreate parameter: %w", err)
150150
}

Diff for: pkg/common/parameters_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestExtractAndDefaultParameters(t *testing.T) {
7676
},
7777
{
7878
name: "values from parameters, checking pd-extreme",
79-
parameters: map[string]string{ParameterKeyType: "pd-extreme", ParameterKeyReplicationType: "none", ParameterKeyDiskEncryptionKmsKey: "foo/key", ParameterKeyLabels: "key1=value1,key2=value2", ParameterKeyProvisionedIOPSOnCreate: "10000Gi"},
79+
parameters: map[string]string{ParameterKeyType: "pd-extreme", ParameterKeyReplicationType: "none", ParameterKeyDiskEncryptionKmsKey: "foo/key", ParameterKeyLabels: "key1=value1,key2=value2", ParameterKeyProvisionedIOPSOnCreate: "10k"},
8080
labels: map[string]string{},
8181
expectParams: DiskParameters{
8282
DiskType: "pd-extreme",

Diff for: pkg/common/utils.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ func ValidateSnapshotType(snapshotType string) error {
259259
}
260260
}
261261

262+
<<<<<<< HEAD
262263
// ParseMachineType returns an extracted machineType from a URL, or empty if not found.
263264
// machineTypeUrl: Full or partial URL of the machine type resource, in the format:
264265
//
@@ -277,18 +278,22 @@ func ConvertGiBStringToInt64(str string) (int64, error) {
277278
match, _ := regexp.MatchString("^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$", str)
278279
if !match {
279280
return 0, fmt.Errorf("invalid string %s", str)
281+
=======
282+
// ConvertStringToInt64 converts a string to int64
283+
func ConvertStringToInt64(str string) (int64, error) {
284+
quantity, err := resource.ParseQuantity(str)
285+
if err != nil {
286+
return -1, err
287+
>>>>>>> 50853f3c (Change iops params directly convert string to int64)
280288
}
281-
quantity := resource.MustParse(str)
282-
return volumehelpers.RoundUpToGiB(quantity)
289+
return volumehelpers.RoundUpToB(quantity)
283290
}
284291

285292
// ConvertMiBStringToInt64 converts a GiB string to int64
286293
func ConvertMiBStringToInt64(str string) (int64, error) {
287-
// Verify regex before
288-
match, _ := regexp.MatchString("^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$", str)
289-
if !match {
290-
return 0, fmt.Errorf("invalid string %s", str)
294+
quantity, err := resource.ParseQuantity(str)
295+
if err != nil {
296+
return -1, err
291297
}
292-
quantity := resource.MustParse(str)
293298
return volumehelpers.RoundUpToMiB(quantity)
294299
}

Diff for: pkg/common/utils_test.go

+79-45
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ func TestSnapshotStorageLocations(t *testing.T) {
578578
}
579579
}
580580

581+
<<<<<<< HEAD
581582
func TestParseMachineType(t *testing.T) {
582583
tests := []struct {
583584
desc string
@@ -633,82 +634,115 @@ func TestParseMachineType(t *testing.T) {
633634
}
634635

635636
func TestConvertGiBStringToInt64(t *testing.T) {
637+
=======
638+
func TestConvertStringToInt64(t *testing.T) {
639+
>>>>>>> 50853f3c (Change iops params directly convert string to int64)
636640
tests := []struct {
637641
desc string
638642
inputStr string
639643
expInt64 int64
640644
expectError bool
641645
}{
642646
{
643-
"valid number string",
644-
"10000",
645-
1,
646-
false,
647+
desc: "valid number string",
648+
inputStr: "10000",
649+
expInt64: 10000,
650+
expectError: false,
647651
},
648652
{
649-
"round Ki to GiB",
650-
"1000Ki",
651-
1,
652-
false,
653+
desc: "round M to number",
654+
inputStr: "1M",
655+
expInt64: 1000000,
656+
expectError: false,
653657
},
654658
{
655-
"round k to GiB",
656-
"1000k",
657-
1,
658-
false,
659+
desc: "round m to number",
660+
inputStr: "1m",
661+
expInt64: 1,
662+
expectError: false,
659663
},
660664
{
661-
"round Mi to GiB",
662-
"1000Mi",
663-
1,
664-
false,
665+
desc: "round k to number",
666+
inputStr: "1k",
667+
expInt64: 1000,
668+
expectError: false,
665669
},
666670
{
667-
"round M to GiB",
668-
"1000M",
669-
1,
670-
false,
671+
desc: "invalid empty string",
672+
inputStr: "",
673+
expInt64: 10000,
674+
expectError: true,
671675
},
672676
{
673-
"round G to GiB",
674-
"1000G",
675-
932,
676-
false,
677+
desc: "invalid string",
678+
inputStr: "ew%65",
679+
expInt64: 10000,
680+
expectError: true,
677681
},
678682
{
679-
"round Gi to GiB",
680-
"10000Gi",
681-
10000,
682-
false,
683+
desc: "invalid KiB string",
684+
inputStr: "10KiB",
685+
expInt64: 10000,
686+
expectError: true,
683687
},
684688
{
685-
"round decimal to GiB",
686-
"1.2Gi",
687-
2,
688-
false,
689+
desc: "invalid GB string",
690+
inputStr: "10GB",
691+
expInt64: 10000,
692+
expectError: true,
689693
},
690694
{
691-
"round big value to GiB",
692-
"8191Pi",
693-
8588886016,
694-
false,
695+
desc: "round Ki to number",
696+
inputStr: "1Ki",
697+
expInt64: 1024,
698+
expectError: false,
695699
},
696700
{
697-
"invalid empty string",
698-
"",
699-
10000,
700-
true,
701+
desc: "round k to number",
702+
inputStr: "10k",
703+
expInt64: 10000,
704+
expectError: false,
701705
},
702706
{
703-
"invalid string",
704-
"ew%65",
705-
10000,
706-
true,
707+
desc: "round Mi to number",
708+
inputStr: "10Mi",
709+
expInt64: 10485760,
710+
expectError: false,
711+
},
712+
{
713+
desc: "round M to number",
714+
inputStr: "10M",
715+
expInt64: 10000000,
716+
expectError: false,
717+
},
718+
{
719+
desc: "round G to number",
720+
inputStr: "10G",
721+
expInt64: 10000000000,
722+
expectError: false,
723+
},
724+
{
725+
desc: "round Gi to number",
726+
inputStr: "100Gi",
727+
expInt64: 107374182400,
728+
expectError: false,
729+
},
730+
{
731+
desc: "round decimal to number",
732+
inputStr: "1.2Gi",
733+
expInt64: 1288490189,
734+
expectError: false,
735+
},
736+
{
737+
desc: "round big value to number",
738+
inputStr: "8191Pi",
739+
expInt64: 9222246136947933184,
740+
expectError: false,
707741
},
708742
}
709743
for _, tc := range tests {
710744
t.Run(tc.desc, func(t *testing.T) {
711-
actualInt64, err := ConvertGiBStringToInt64(tc.inputStr)
745+
actualInt64, err := ConvertStringToInt64(tc.inputStr)
712746
if err != nil && !tc.expectError {
713747
t.Errorf("Got error %v converting string to int64 %s; expect no error", err, tc.inputStr)
714748
}

Diff for: test/e2e/tests/single_zone_e2e_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ import (
4141

4242
const (
4343
testNamePrefix = "gcepd-csi-e2e-"
44-
45-
defaultSizeGb int64 = 5
46-
defaultRepdSizeGb int64 = 200
47-
defaultMwSizeGb int64 = 200
48-
readyState = "READY"
49-
standardDiskType = "pd-standard"
50-
defaultVolumeLimit int64 = 127
51-
44+
defaultSizeGb int64 = 5
45+
defaultRepdSizeGb int64 = 200
46+
defaultMwSizeGb int64 = 200
47+
defaultVolumeLimit int64 = 127
48+
readyState = "READY"
49+
standardDiskType = "pd-standard"
50+
extremeDiskType = "pd-extreme"
51+
provisionedIOPSOnCreate = "100000"
52+
provisionedIOPSOnCreateInt = int64(100000)
5253
defaultEpsilon = 500000000 // 500M
5354
)
5455

Diff for: test/k8s-integration/config/sc-extreme.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
provisioner: pd.csi.storage.gke.io
66
parameters:
77
type: pd-extreme
8-
provisioned-iops-on-create: '10000Gi'
8+
provisioned-iops-on-create: '10000'
99
# Add labels for testing.
1010
labels: key1=value1,key2=value2
11-
volumeBindingMode: WaitForFirstConsumer
11+
volumeBindingMode: WaitForFirstConsumer

0 commit comments

Comments
 (0)