diff --git a/pkg/gce-pd-csi-driver/node.go b/pkg/gce-pd-csi-driver/node.go index 1ccc8e85d..852ff6c1c 100644 --- a/pkg/gce-pd-csi-driver/node.go +++ b/pkg/gce-pd-csi-driver/node.go @@ -656,7 +656,9 @@ func (ns *GCENodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRe volumeLimits, err := ns.GetVolumeLimits(ctx) if err != nil { - klog.Errorf("GetVolumeLimits failed: %v", err.Error()) + klog.Errorf("GetVolumeLimits failed: %v. The error is ignored so that the driver can register", err.Error()) + // No error should be returned from NodeGetInfo, otherwise the driver will not register + err = nil } resp := &csi.NodeGetInfoResponse{ @@ -843,13 +845,17 @@ func (ns *GCENodeServer) GetVolumeLimits(ctx context.Context) (int64, error) { gen4MachineTypesPrefix := []string{"c4a-", "c4-", "n4-"} for _, gen4Prefix := range gen4MachineTypesPrefix { if strings.HasPrefix(machineType, gen4Prefix) { - cpuString := machineType[strings.LastIndex(machineType, "-")+1:] - cpus, err := strconv.ParseInt(cpuString, 10, 64) - if err != nil { - return volumeLimitSmall, fmt.Errorf("invalid cpuString %s for machine type: %v", cpuString, machineType) + machineTypeSlice := strings.Split(machineType, "-") + if len(machineTypeSlice) > 2 { + cpuString := machineTypeSlice[2] + cpus, err := strconv.ParseInt(cpuString, 10, 64) + if err != nil { + return volumeLimitBig, fmt.Errorf("invalid cpuString %s for machine type: %v", cpuString, machineType) + } + return common.MapNumber(cpus), nil + } else { + return volumeLimitBig, fmt.Errorf("unconventional machine type: %v", machineType) } - return common.MapNumber(cpus), nil - } if strings.HasPrefix(machineType, "x4-") { return x4HyperdiskLimit, nil diff --git a/pkg/gce-pd-csi-driver/node_test.go b/pkg/gce-pd-csi-driver/node_test.go index 058cd4f05..1809c4cc5 100644 --- a/pkg/gce-pd-csi-driver/node_test.go +++ b/pkg/gce-pd-csi-driver/node_test.go @@ -288,15 +288,31 @@ func TestNodeGetVolumeLimits(t *testing.T) { machineType: "n4-standard-16", expVolumeLimit: 31, }, + { + name: "n4-micro", // This type does not exist, but testing edge cases + machineType: "n4-micro", + expVolumeLimit: volumeLimitBig, + expectError: true, + }, { name: "n4-highcpu-4", machineType: "n4-highcpu-4", expVolumeLimit: 15, }, + { + name: "n4-custom-8-12345-ext", + machineType: "n4-custom-8-12345-ext", + expVolumeLimit: 23, + }, + { + name: "n4-custom-16-12345", + machineType: "n4-custom-16-12345", + expVolumeLimit: 31, + }, { name: "invalid gen4 machine type", machineType: "n4-highcpu-4xyz", - expVolumeLimit: volumeLimitSmall, + expVolumeLimit: volumeLimitBig, expectError: true, }, {