Skip to content

Commit 601f482

Browse files
Change iops params to not handle GB/MB etc.
1 parent 831d1ea commit 601f482

File tree

5 files changed

+60
-44
lines changed

5 files changed

+60
-44
lines changed

pkg/common/parameters.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
152152
p.Labels[labelKey] = labelValue
153153
}
154154
case ParameterKeyProvisionedIOPSOnCreate:
155-
paramProvisionedIOPSOnCreate, err := ConvertGiBStringToInt64(v)
155+
paramProvisionedIOPSOnCreate, err := ConvertStringToInt64(v)
156156
if err != nil {
157157
return p, fmt.Errorf("parameters contain invalid provisionedIOPSOnCreate parameter: %w", err)
158158
}

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

+33-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package common
1919
import (
2020
"fmt"
2121
"regexp"
22+
"strconv"
2223
"strings"
2324

2425
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
@@ -59,6 +60,9 @@ const (
5960
multiRegionalLocationFmt = "^[a-z]+$"
6061
// Example: us-east1
6162
regionalLocationFmt = "^[a-z]+-[a-z]+[0-9]$"
63+
64+
thousand = int64(1000)
65+
million = int64(1000000)
6266
)
6367

6468
var (
@@ -251,15 +255,39 @@ func ValidateSnapshotType(snapshotType string) error {
251255
}
252256
}
253257

254-
// ConvertGiBStringToInt64 converts a GiB string to int64
255-
func ConvertGiBStringToInt64(str string) (int64, error) {
258+
// ConvertStringToInt64 converts a string to int64
259+
func ConvertStringToInt64(str string) (int64, error) {
256260
// Verify regex before
257-
match, _ := regexp.MatchString("^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$", str)
261+
match, _ := regexp.MatchString("^([+-]?[0-9.]+)([mkKM]*[-+]?[0-9]*)$", str)
258262
if !match {
259263
return 0, fmt.Errorf("invalid string %s", str)
260264
}
261-
quantity := resource.MustParse(str)
262-
return volumehelpers.RoundUpToGiB(quantity)
265+
266+
multiplier := int64(1)
267+
value, err := strconv.ParseInt(str, 10, 64)
268+
if err != nil {
269+
// the string ends with m, M or k, K
270+
last := string(str[len(str)-1])
271+
272+
switch last {
273+
case "k":
274+
multiplier = thousand
275+
case "K":
276+
multiplier = thousand
277+
case "m":
278+
multiplier = million
279+
case "M":
280+
multiplier = million
281+
default:
282+
return 0, fmt.Errorf("invalid string %s", str)
283+
}
284+
str = str[:len(str)-1]
285+
value, err = strconv.ParseInt(str, 10, 64)
286+
if err != nil {
287+
return 0, fmt.Errorf("invalid string %s", str)
288+
}
289+
}
290+
return value * multiplier, nil
263291
}
264292

265293
// ConvertMiBStringToInt64 converts a GiB string to int64

pkg/common/utils_test.go

+24-36
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ 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
@@ -588,60 +588,48 @@ func TestConvertGiBStringToInt64(t *testing.T) {
588588
{
589589
"valid number string",
590590
"10000",
591-
1,
591+
10000,
592592
false,
593593
},
594594
{
595-
"round Ki to GiB",
596-
"1000Ki",
597-
1,
595+
"round M to number",
596+
"1M",
597+
1000000,
598598
false,
599599
},
600600
{
601-
"round k to GiB",
602-
"1000k",
603-
1,
601+
"round m to number",
602+
"1m",
603+
1000000,
604604
false,
605605
},
606606
{
607-
"round Mi to GiB",
608-
"1000Mi",
609-
1,
610-
false,
611-
},
612-
{
613-
"round M to GiB",
614-
"1000M",
615-
1,
607+
"round k to number",
608+
"1k",
609+
1000,
616610
false,
617611
},
618612
{
619-
"round G to GiB",
620-
"1000G",
621-
932,
613+
"round K to number",
614+
"2K",
615+
2000,
622616
false,
623617
},
624618
{
625-
"round Gi to GiB",
626-
"10000Gi",
619+
"invalid empty string",
620+
"",
627621
10000,
628-
false,
629-
},
630-
{
631-
"round decimal to GiB",
632-
"1.2Gi",
633-
2,
634-
false,
622+
true,
635623
},
636624
{
637-
"round big value to GiB",
638-
"8191Pi",
639-
8588886016,
640-
false,
625+
"invalid string with duplicate valid alphabets",
626+
"2kk",
627+
10000,
628+
true,
641629
},
642630
{
643-
"invalid empty string",
644-
"",
631+
"invalid string contains valid string",
632+
"10k10",
645633
10000,
646634
true,
647635
},
@@ -654,7 +642,7 @@ func TestConvertGiBStringToInt64(t *testing.T) {
654642
}
655643
for _, tc := range tests {
656644
t.Run(tc.desc, func(t *testing.T) {
657-
actualInt64, err := ConvertGiBStringToInt64(tc.inputStr)
645+
actualInt64, err := ConvertStringToInt64(tc.inputStr)
658646
if err != nil && !tc.expectError {
659647
t.Errorf("Got error %v converting string to int64 %s; expect no error", err, tc.inputStr)
660648
}

test/k8s-integration/config/sc-extreme.yaml

+1-1
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
1111
volumeBindingMode: WaitForFirstConsumer

0 commit comments

Comments
 (0)