Skip to content

Commit c81e21e

Browse files
Address pr feedbacks
1 parent 442bdde commit c81e21e

File tree

8 files changed

+98
-22
lines changed

8 files changed

+98
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ See Github [Issues](https://github.com/kubernetes-sigs/gcp-compute-persistent-di
6464
| replication-type | `none` OR `regional-pd` | `none` | Replication type allows you to choose between Zonal Persistent Disks or Regional Persistent Disks |
6565
| disk-encryption-kms-key | Fully qualified resource identifier for the key to use to encrypt new disks. | Empty string. | Encrypt disk using Customer Managed Encryption Key (CMEK). See [GKE Docs](https://cloud.google.com/kubernetes-engine/docs/how-to/using-cmek#create_a_cmek_protected_attached_disk) for details. |
6666
| labels | `key1=value1,key2=value2` | | Labels allow you to assign custom [GCE Disk labels](https://cloud.google.com/compute/docs/labeling-resources). |
67-
| provisioned-iops-on-create | string (int64 format). Values must be between 10,000 and 120,000 | | Indicates how many IOPS to provision for the disk. See the [Extreme persistent disk documentation](https://cloud.google.com/compute/docs/disks/extreme-persistent-disk). |
67+
| provisioned-iops-on-create | string (int64 format). Values typically between 10,000 and 120,000 | | Indicates how many IOPS to provision for the disk. See the [Extreme persistent disk documentation](https://cloud.google.com/compute/docs/disks/extreme-persistent-disk) for details, including valid ranges for IOPS. |
6868

6969

7070
### Topology

pkg/common/parameters.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type DiskParameters struct {
7878
Labels map[string]string
7979
// Values: {string}
8080
// Default: ""
81-
ProvisionedIOPSOnCreate string
81+
ProvisionedIOPSOnCreate int64
8282
}
8383

8484
// SnapshotParameters contains normalized and defaulted parameters for snapshots
@@ -96,12 +96,11 @@ type SnapshotParameters struct {
9696
// parameters, any matching extraVolumeLabels will be overridden.
9797
func ExtractAndDefaultParameters(parameters map[string]string, driverName string, extraVolumeLabels map[string]string) (DiskParameters, error) {
9898
p := DiskParameters{
99-
DiskType: "pd-standard", // Default
100-
ReplicationType: replicationTypeNone, // Default
101-
DiskEncryptionKMSKey: "", // Default
102-
Tags: make(map[string]string), // Default
103-
Labels: make(map[string]string), // Default
104-
ProvisionedIOPSOnCreate: "", // Default
99+
DiskType: "pd-standard", // Default
100+
ReplicationType: replicationTypeNone, // Default
101+
DiskEncryptionKMSKey: "", // Default
102+
Tags: make(map[string]string), // Default
103+
Labels: make(map[string]string), // Default
105104
}
106105

107106
for k, v := range extraVolumeLabels {
@@ -141,7 +140,11 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
141140
p.Labels[labelKey] = labelValue
142141
}
143142
case ParameterKeyProvisionedIOPSOnCreate:
144-
p.ProvisionedIOPSOnCreate = v
143+
paramProvisionedIOPSOnCreate, err := ConvertStringToInt64(v)
144+
if err != nil {
145+
return p, fmt.Errorf("parameters contain invalid provisionedIOPSOnCreate parameter: %w", err)
146+
}
147+
p.ProvisionedIOPSOnCreate = paramProvisionedIOPSOnCreate
145148
default:
146149
return p, fmt.Errorf("parameters contains invalid option %q", k)
147150
}

pkg/common/parameters_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestExtractAndDefaultParameters(t *testing.T) {
8787
"key1": "value1",
8888
"key2": "value2",
8989
},
90-
ProvisionedIOPSOnCreate: "10000",
90+
ProvisionedIOPSOnCreate: 10000,
9191
},
9292
},
9393
{

pkg/common/utils.go

+10
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"
@@ -248,3 +249,12 @@ func ValidateSnapshotType(snapshotType string) error {
248249
return fmt.Errorf("invalid snapshot type %s", snapshotType)
249250
}
250251
}
252+
253+
// ConvertStringToInt64 converts a string to int64
254+
func ConvertStringToInt64(str string) (int64, error) {
255+
int64Value, err := strconv.ParseInt(str, 10, 64)
256+
if err != nil {
257+
return -1, fmt.Errorf("Int64 value %s is invalid", str)
258+
}
259+
return int64Value, nil
260+
}

pkg/common/utils_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,39 @@ func TestSnapshotStorageLocations(t *testing.T) {
577577
})
578578
}
579579
}
580+
581+
func TestConvertStringToInt64(t *testing.T) {
582+
tests := []struct {
583+
desc string
584+
inputStr string
585+
expInt64 int64
586+
expectError bool
587+
}{
588+
{
589+
"valid string",
590+
"10000",
591+
10000,
592+
false,
593+
},
594+
{
595+
"invalid string",
596+
"ew%65",
597+
10000,
598+
true,
599+
},
600+
}
601+
for _, tc := range tests {
602+
t.Run(tc.desc, func(t *testing.T) {
603+
actualInt64, err := ConvertStringToInt64(tc.inputStr)
604+
if err != nil && !tc.expectError {
605+
t.Errorf("Got error %v converting string to int64 %s; expect no error", err, tc.inputStr)
606+
}
607+
if err == nil && tc.expectError {
608+
t.Errorf("Got no error converting string to int64 %s; expect an error", tc.inputStr)
609+
}
610+
if err == nil && actualInt64 != tc.expInt64 {
611+
t.Errorf("Got %d for converting string to int64; expect %d", actualInt64, tc.expInt64)
612+
}
613+
})
614+
}
615+
}

pkg/gce-cloud-provider/compute/fake-gce.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,14 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string,
196196
}
197197

198198
computeDisk := &computev1.Disk{
199-
Name: volKey.Name,
200-
SizeGb: common.BytesToGbRoundUp(capBytes),
201-
Description: "Disk created by GCE-PD CSI Driver",
202-
Type: cloud.GetDiskTypeURI(project, volKey, params.DiskType),
203-
SourceDiskId: volumeContentSourceVolumeID,
204-
Status: cloud.mockDiskStatus,
205-
Labels: params.Labels,
199+
Name: volKey.Name,
200+
SizeGb: common.BytesToGbRoundUp(capBytes),
201+
Description: "Disk created by GCE-PD CSI Driver",
202+
Type: cloud.GetDiskTypeURI(project, volKey, params.DiskType),
203+
SourceDiskId: volumeContentSourceVolumeID,
204+
Status: cloud.mockDiskStatus,
205+
Labels: params.Labels,
206+
ProvisionedIops: params.ProvisionedIOPSOnCreate,
206207
}
207208

208209
if snapshotID != "" {

pkg/gce-cloud-provider/compute/gce-compute.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,12 @@ func (cloud *CloudProvider) insertRegionalDisk(
429429
}
430430

431431
diskToCreate := &computev1.Disk{
432-
Name: volKey.Name,
433-
SizeGb: common.BytesToGbRoundUp(capBytes),
434-
Description: description,
435-
Type: cloud.GetDiskTypeURI(cloud.project, volKey, params.DiskType),
436-
Labels: params.Labels,
432+
Name: volKey.Name,
433+
SizeGb: common.BytesToGbRoundUp(capBytes),
434+
Description: description,
435+
Type: cloud.GetDiskTypeURI(cloud.project, volKey, params.DiskType),
436+
Labels: params.Labels,
437+
ProvisionedIops: params.ProvisionedIOPSOnCreate,
437438
}
438439
if snapshotID != "" {
439440
_, snapshotType, _, err := common.SnapshotIDToProjectKey(snapshotID)

pkg/gce-pd-csi-driver/controller_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,31 @@ func TestCreateVolumeArguments(t *testing.T) {
792792
},
793793
expErrCode: codes.InvalidArgument,
794794
},
795+
{
796+
name: "success with provisionedIops parameter",
797+
req: &csi.CreateVolumeRequest{
798+
Name: name,
799+
CapacityRange: stdCapRange,
800+
VolumeCapabilities: stdVolCaps,
801+
Parameters: map[string]string{"labels": "key1=value1,key2=value2", "provisioned-iops-on-create": 10000},
802+
},
803+
expVol: &csi.Volume{
804+
CapacityBytes: common.GbToBytes(20),
805+
VolumeId: testVolumeID,
806+
VolumeContext: nil,
807+
AccessibleTopology: stdTopology,
808+
},
809+
},
810+
{
811+
name: "fail with malformed provisionedIops parameter",
812+
req: &csi.CreateVolumeRequest{
813+
Name: name,
814+
CapacityRange: stdCapRange,
815+
VolumeCapabilities: stdVolCaps,
816+
Parameters: map[string]string{"labels": "key1=value1,key2=value2", "provisioned-iops-on-create": "dsfo3"},
817+
},
818+
expErrCode: codes.InvalidArgument,
819+
},
795820
}
796821

797822
// Run test cases

0 commit comments

Comments
 (0)