Skip to content

Commit 0b0ae6b

Browse files
Change iops params directly convert string to int64
1 parent 7059368 commit 0b0ae6b

File tree

6 files changed

+153
-117
lines changed

6 files changed

+153
-117
lines changed

Diff for: pkg/common/parameters.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ 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
}
151151
p.ProvisionedIOPSOnCreate = paramProvisionedIOPSOnCreate
152152
case ParameterKeyProvisionedThroughputOnCreate:
153-
paramProvisionedThroughputOnCreate, err := ConvertMiBStringToInt64(v)
153+
paramProvisionedThroughputOnCreate, err := ConvertMiStringToInt64(v)
154154
if err != nil {
155155
return p, fmt.Errorf("parameters contain invalid provisionedThroughputOnCreate parameter: %w", err)
156156
}

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

+11-15
Original file line numberDiff line numberDiff line change
@@ -271,24 +271,20 @@ func ParseMachineType(machineTypeUrl string) (string, error) {
271271
return machineType[1], nil
272272
}
273273

274-
// ConvertGiBStringToInt64 converts a GiB string to int64
275-
func ConvertGiBStringToInt64(str string) (int64, error) {
276-
// Verify regex before
277-
match, _ := regexp.MatchString("^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$", str)
278-
if !match {
279-
return 0, fmt.Errorf("invalid string %s", str)
274+
// ConvertStringToInt64 converts a string to int64
275+
func ConvertStringToInt64(str string) (int64, error) {
276+
quantity, err := resource.ParseQuantity(str)
277+
if err != nil {
278+
return -1, err
280279
}
281-
quantity := resource.MustParse(str)
282-
return volumehelpers.RoundUpToGiB(quantity)
280+
return volumehelpers.RoundUpToB(quantity)
283281
}
284282

285-
// ConvertMiBStringToInt64 converts a GiB string to int64
286-
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)
283+
// ConvertMiStringToInt64 converts a GiB string to int64
284+
func ConvertMiStringToInt64(str string) (int64, error) {
285+
quantity, err := resource.ParseQuantity(str)
286+
if err != nil {
287+
return -1, err
291288
}
292-
quantity := resource.MustParse(str)
293289
return volumehelpers.RoundUpToMiB(quantity)
294290
}

Diff for: pkg/common/utils_test.go

+134-92
Original file line numberDiff line numberDiff line change
@@ -632,83 +632,113 @@ func TestParseMachineType(t *testing.T) {
632632
}
633633
}
634634

635-
func TestConvertGiBStringToInt64(t *testing.T) {
635+
func TestConvertStringToInt64(t *testing.T) {
636636
tests := []struct {
637637
desc string
638638
inputStr string
639639
expInt64 int64
640640
expectError bool
641641
}{
642642
{
643-
"valid number string",
644-
"10000",
645-
1,
646-
false,
643+
desc: "valid number string",
644+
inputStr: "10000",
645+
expInt64: 10000,
646+
expectError: false,
647647
},
648648
{
649-
"round Ki to GiB",
650-
"1000Ki",
651-
1,
652-
false,
649+
desc: "round M to number",
650+
inputStr: "1M",
651+
expInt64: 1000000,
652+
expectError: false,
653653
},
654654
{
655-
"round k to GiB",
656-
"1000k",
657-
1,
658-
false,
655+
desc: "round m to number",
656+
inputStr: "1m",
657+
expInt64: 1,
658+
expectError: false,
659659
},
660660
{
661-
"round Mi to GiB",
662-
"1000Mi",
663-
1,
664-
false,
661+
desc: "round k to number",
662+
inputStr: "1k",
663+
expInt64: 1000,
664+
expectError: false,
665665
},
666666
{
667-
"round M to GiB",
668-
"1000M",
669-
1,
670-
false,
667+
desc: "invalid empty string",
668+
inputStr: "",
669+
expInt64: 0,
670+
expectError: true,
671671
},
672672
{
673-
"round G to GiB",
674-
"1000G",
675-
932,
676-
false,
673+
desc: "invalid string",
674+
inputStr: "ew%65",
675+
expInt64: 0,
676+
expectError: true,
677677
},
678678
{
679-
"round Gi to GiB",
680-
"10000Gi",
681-
10000,
682-
false,
679+
desc: "invalid KiB string",
680+
inputStr: "10KiB",
681+
expInt64: 10000,
682+
expectError: true,
683683
},
684684
{
685-
"round decimal to GiB",
686-
"1.2Gi",
687-
2,
688-
false,
685+
desc: "invalid GB string",
686+
inputStr: "10GB",
687+
expInt64: 0,
688+
expectError: true,
689689
},
690690
{
691-
"round big value to GiB",
692-
"8191Pi",
693-
8588886016,
694-
false,
691+
desc: "round Ki to number",
692+
inputStr: "1Ki",
693+
expInt64: 1024,
694+
expectError: false,
695695
},
696696
{
697-
"invalid empty string",
698-
"",
699-
10000,
700-
true,
697+
desc: "round k to number",
698+
inputStr: "10k",
699+
expInt64: 10000,
700+
expectError: false,
701701
},
702702
{
703-
"invalid string",
704-
"ew%65",
705-
10000,
706-
true,
703+
desc: "round Mi to number",
704+
inputStr: "10Mi",
705+
expInt64: 10485760,
706+
expectError: false,
707+
},
708+
{
709+
desc: "round M to number",
710+
inputStr: "10M",
711+
expInt64: 10000000,
712+
expectError: false,
713+
},
714+
{
715+
desc: "round G to number",
716+
inputStr: "10G",
717+
expInt64: 10000000000,
718+
expectError: false,
719+
},
720+
{
721+
desc: "round Gi to number",
722+
inputStr: "100Gi",
723+
expInt64: 107374182400,
724+
expectError: false,
725+
},
726+
{
727+
desc: "round decimal to number",
728+
inputStr: "1.2Gi",
729+
expInt64: 1288490189,
730+
expectError: false,
731+
},
732+
{
733+
desc: "round big value to number",
734+
inputStr: "8191Pi",
735+
expInt64: 9222246136947933184,
736+
expectError: false,
707737
},
708738
}
709739
for _, tc := range tests {
710740
t.Run(tc.desc, func(t *testing.T) {
711-
actualInt64, err := ConvertGiBStringToInt64(tc.inputStr)
741+
actualInt64, err := ConvertStringToInt64(tc.inputStr)
712742
if err != nil && !tc.expectError {
713743
t.Errorf("Got error %v converting string to int64 %s; expect no error", err, tc.inputStr)
714744
}
@@ -722,83 +752,95 @@ func TestConvertGiBStringToInt64(t *testing.T) {
722752
}
723753
}
724754

725-
func TestConvertMiBStringToInt64(t *testing.T) {
755+
func TestConvertMiStringToInt64(t *testing.T) {
726756
tests := []struct {
727757
desc string
728758
inputStr string
729759
expInt64 int64
730760
expectError bool
731761
}{
732762
{
733-
"valid number string",
734-
"10000",
735-
1,
736-
false,
763+
desc: "valid number string",
764+
inputStr: "10000",
765+
expInt64: 1,
766+
expectError: false,
737767
},
738768
{
739-
"round Ki to MiB",
740-
"1000Ki",
741-
1,
742-
false,
769+
desc: "round Ki to MiB",
770+
inputStr: "1000Ki",
771+
expInt64: 1,
772+
expectError: false,
743773
},
744774
{
745-
"round k to MiB",
746-
"1000k",
747-
1,
748-
false,
775+
desc: "round k to MiB",
776+
inputStr: "1000k",
777+
expInt64: 1,
778+
expectError: false,
749779
},
750780
{
751-
"round Mi to MiB",
752-
"1000Mi",
753-
1000,
754-
false,
781+
desc: "round Mi to MiB",
782+
inputStr: "1000Mi",
783+
expInt64: 1000,
784+
expectError: false,
755785
},
756786
{
757-
"round M to MiB",
758-
"1000M",
759-
954,
760-
false,
787+
desc: "round M to MiB",
788+
inputStr: "1000M",
789+
expInt64: 954,
790+
expectError: false,
761791
},
762792
{
763-
"round G to MiB",
764-
"1000G",
765-
953675,
766-
false,
793+
desc: "round G to MiB",
794+
inputStr: "1000G",
795+
expInt64: 953675,
796+
expectError: false,
767797
},
768798
{
769-
"round Gi to MiB",
770-
"10000Gi",
771-
10240000,
772-
false,
799+
desc: "round Gi to MiB",
800+
inputStr: "10000Gi",
801+
expInt64: 10240000,
802+
expectError: false,
773803
},
774804
{
775-
"round decimal to MiB",
776-
"1.2Gi",
777-
1229,
778-
false,
805+
desc: "round decimal to MiB",
806+
inputStr: "1.2Gi",
807+
expInt64: 1229,
808+
expectError: false,
779809
},
780810
{
781-
"round big value to MiB",
782-
"8191Pi",
783-
8795019280384,
784-
false,
811+
desc: "round big value to MiB",
812+
inputStr: "8191Pi",
813+
expInt64: 8795019280384,
814+
expectError: false,
785815
},
786816
{
787-
"invalid empty string",
788-
"",
789-
10000,
790-
true,
817+
desc: "invalid empty string",
818+
inputStr: "",
819+
expInt64: 0,
820+
expectError: true,
791821
},
792822
{
793-
"invalid string",
794-
"ew%65",
795-
10000,
796-
true,
823+
desc: "invalid KiB string",
824+
inputStr: "10KiB",
825+
expInt64: 10000,
826+
expectError: true,
827+
},
828+
{
829+
desc: "invalid GB string",
830+
inputStr: "10GB",
831+
expInt64: 0,
832+
expectError: true,
833+
},
834+
{
835+
desc: "invalid string",
836+
inputStr: "ew%65",
837+
expInt64: 0,
838+
expectError: true,
797839
},
798840
}
799841
for _, tc := range tests {
800842
t.Run(tc.desc, func(t *testing.T) {
801-
actualInt64, err := ConvertMiBStringToInt64(tc.inputStr)
843+
actualInt64, err := ConvertMiStringToInt64(tc.inputStr)
802844
if err != nil && !tc.expectError {
803845
t.Errorf("Got error %v converting string to int64 %s; expect no error", err, tc.inputStr)
804846
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ import (
4040
)
4141

4242
const (
43-
testNamePrefix = "gcepd-csi-e2e-"
44-
43+
testNamePrefix = "gcepd-csi-e2e-"
4544
defaultSizeGb int64 = 5
4645
defaultRepdSizeGb int64 = 200
4746
defaultMwSizeGb int64 = 200
47+
defaultVolumeLimit int64 = 127
4848
readyState = "READY"
4949
standardDiskType = "pd-standard"
50-
defaultVolumeLimit int64 = 127
51-
52-
defaultEpsilon = 500000000 // 500M
50+
defaultEpsilon = 500000000 // 500M
5351
)
5452

5553
var _ = Describe("GCE PD CSI Driver", func() {

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)