Skip to content

Commit 2cb0f07

Browse files
Change iops params directly convert string to int64
1 parent e621764 commit 2cb0f07

File tree

6 files changed

+90
-64
lines changed

6 files changed

+90
-64
lines changed

pkg/common/parameters.go

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

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",

pkg/common/utils.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -259,24 +259,20 @@ func ValidateSnapshotType(snapshotType string) error {
259259
}
260260
}
261261

262-
// ConvertGiBStringToInt64 converts a GiB string to int64
263-
func ConvertGiBStringToInt64(str string) (int64, error) {
264-
// Verify regex before
265-
match, _ := regexp.MatchString("^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$", str)
266-
if !match {
267-
return 0, fmt.Errorf("invalid string %s", str)
262+
// ConvertStringToInt64 converts a string to int64
263+
func ConvertStringToInt64(str string) (int64, error) {
264+
quantity, err := resource.ParseQuantity(str)
265+
if err != nil {
266+
return -1, err
268267
}
269-
quantity := resource.MustParse(str)
270-
return volumehelpers.RoundUpToGiB(quantity)
268+
return volumehelpers.RoundUpToB(quantity)
271269
}
272270

273271
// ConvertMiBStringToInt64 converts a GiB string to int64
274272
func ConvertMiBStringToInt64(str string) (int64, error) {
275-
// Verify regex before
276-
match, _ := regexp.MatchString("^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$", str)
277-
if !match {
278-
return 0, fmt.Errorf("invalid string %s", str)
273+
quantity, err := resource.ParseQuantity(str)
274+
if err != nil {
275+
return -1, err
279276
}
280-
quantity := resource.MustParse(str)
281277
return volumehelpers.RoundUpToMiB(quantity)
282278
}

pkg/common/utils_test.go

+76-46
Original file line numberDiff line numberDiff line change
@@ -578,83 +578,113 @@ func TestSnapshotStorageLocations(t *testing.T) {
578578
}
579579
}
580580

581-
func TestConvertGiBStringToInt64(t *testing.T) {
581+
func TestConvertStringToInt64(t *testing.T) {
582582
tests := []struct {
583583
desc string
584584
inputStr string
585585
expInt64 int64
586586
expectError bool
587587
}{
588588
{
589-
"valid number string",
590-
"10000",
591-
1,
592-
false,
589+
desc: "valid number string",
590+
inputStr: "10000",
591+
expInt64: 10000,
592+
expectError: false,
593593
},
594594
{
595-
"round Ki to GiB",
596-
"1000Ki",
597-
1,
598-
false,
595+
desc: "round M to number",
596+
inputStr: "1M",
597+
expInt64: 1000000,
598+
expectError: false,
599599
},
600600
{
601-
"round k to GiB",
602-
"1000k",
603-
1,
604-
false,
601+
desc: "round m to number",
602+
inputStr: "1m",
603+
expInt64: 1,
604+
expectError: false,
605605
},
606606
{
607-
"round Mi to GiB",
608-
"1000Mi",
609-
1,
610-
false,
607+
desc: "round k to number",
608+
inputStr: "1k",
609+
expInt64: 1000,
610+
expectError: false,
611611
},
612612
{
613-
"round M to GiB",
614-
"1000M",
615-
1,
616-
false,
613+
desc: "invalid empty string",
614+
inputStr: "",
615+
expInt64: 10000,
616+
expectError: true,
617617
},
618618
{
619-
"round G to GiB",
620-
"1000G",
621-
932,
622-
false,
619+
desc: "invalid string",
620+
inputStr: "ew%65",
621+
expInt64: 10000,
622+
expectError: true,
623623
},
624624
{
625-
"round Gi to GiB",
626-
"10000Gi",
627-
10000,
628-
false,
625+
desc: "invalid KiB string",
626+
inputStr: "10KiB",
627+
expInt64: 10000,
628+
expectError: true,
629629
},
630630
{
631-
"round decimal to GiB",
632-
"1.2Gi",
633-
2,
634-
false,
631+
desc: "invalid GB string",
632+
inputStr: "10GB",
633+
expInt64: 10000,
634+
expectError: true,
635635
},
636636
{
637-
"round big value to GiB",
638-
"8191Pi",
639-
8588886016,
640-
false,
637+
desc: "round Ki to number",
638+
inputStr: "1Ki",
639+
expInt64: 1024,
640+
expectError: false,
641641
},
642642
{
643-
"invalid empty string",
644-
"",
645-
10000,
646-
true,
643+
desc: "round k to number",
644+
inputStr: "10k",
645+
expInt64: 10000,
646+
expectError: false,
647647
},
648648
{
649-
"invalid string",
650-
"ew%65",
651-
10000,
652-
true,
649+
desc: "round Mi to number",
650+
inputStr: "10Mi",
651+
expInt64: 10485760,
652+
expectError: false,
653+
},
654+
{
655+
desc: "round M to number",
656+
inputStr: "10M",
657+
expInt64: 10000000,
658+
expectError: false,
659+
},
660+
{
661+
desc: "round G to number",
662+
inputStr: "10G",
663+
expInt64: 10000000000,
664+
expectError: false,
665+
},
666+
{
667+
desc: "round Gi to number",
668+
inputStr: "100Gi",
669+
expInt64: 107374182400,
670+
expectError: false,
671+
},
672+
{
673+
desc: "round decimal to number",
674+
inputStr: "1.2Gi",
675+
expInt64: 1288490189,
676+
expectError: false,
677+
},
678+
{
679+
desc: "round big value to number",
680+
inputStr: "8191Pi",
681+
expInt64: 9222246136947933184,
682+
expectError: false,
653683
},
654684
}
655685
for _, tc := range tests {
656686
t.Run(tc.desc, func(t *testing.T) {
657-
actualInt64, err := ConvertGiBStringToInt64(tc.inputStr)
687+
actualInt64, err := ConvertStringToInt64(tc.inputStr)
658688
if err != nil && !tc.expectError {
659689
t.Errorf("Got error %v converting string to int64 %s; expect no error", err, tc.inputStr)
660690
}

test/e2e/tests/single_zone_e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141

4242
const (
4343
testNamePrefix = "gcepd-csi-e2e-"
44-
44+
4545
defaultSizeGb int64 = 5
4646
defaultRepdSizeGb int64 = 200
4747
defaultMwSizeGb int64 = 200

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)