Skip to content

Commit bfb5139

Browse files
committed
Print machineType, rather than just the machine family when encountering an incompatible disk attach
1 parent f3a9051 commit bfb5139

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

pkg/common/utils.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ func ValidateSnapshotType(snapshotType string) error {
257257
}
258258
}
259259

260-
// ParseMachineTypeFromUrl returns an extracted machineType from a URL, or empty if not found.
260+
// ParseMachineType returns an extracted machineType from a URL, or empty if not found.
261261
// machineTypeUrl: Full or partial URL of the machine type resource, in the format:
262262
//
263263
// zones/zone/machineTypes/machine-type
264-
func ParseMachineFamily(machineTypeUrl string) (string, error) {
264+
func ParseMachineType(machineTypeUrl string) (string, error) {
265265
machineType := machineTypeRegex.FindStringSubmatch(machineTypeUrl)
266266
if machineType == nil {
267-
return "", fmt.Errorf("failed to parse machineTypeUrl. expected suffix zones/{zone}/machineTypes/{machine-type}. Got: %s", machineTypeUrl)
267+
return "", fmt.Errorf("failed to parse machineTypeUrl. Expected suffix: zones/{zone}/machineTypes/{machine-type}. Got: %s", machineTypeUrl)
268268
}
269-
return strings.Split(machineType[1], "-")[0], nil
269+
return machineType[1], nil
270270
}

pkg/common/utils_test.go

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

581-
func TestParseMachineFamily(t *testing.T) {
581+
func TestParseMachineType(t *testing.T) {
582582
tests := []struct {
583-
desc string
584-
inputMachineTypeUrl string
585-
expectedMachineFamily string
586-
expectError bool
583+
desc string
584+
inputMachineTypeUrl string
585+
expectedMachineType string
586+
expectError bool
587587
}{
588588
{
589-
desc: "full URL machine family",
590-
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4",
591-
expectedMachineFamily: "c3",
589+
desc: "full URL machine type",
590+
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4",
591+
expectedMachineType: "c3-highcpu-4",
592592
},
593593
{
594-
desc: "partial URL machine family",
595-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
596-
expectedMachineFamily: "n2",
594+
desc: "partial URL machine type",
595+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
596+
expectedMachineType: "n2-standard-4",
597597
},
598598
{
599-
desc: "custom partial URL machine family",
600-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
601-
expectedMachineFamily: "e2",
599+
desc: "custom partial URL machine type",
600+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
601+
expectedMachineType: "e2-custom-2-4096",
602602
},
603603
{
604604
desc: "incorrect URL",
@@ -618,15 +618,15 @@ func TestParseMachineFamily(t *testing.T) {
618618
}
619619
for _, tc := range tests {
620620
t.Run(tc.desc, func(t *testing.T) {
621-
actualMachineFamily, err := ParseMachineFamily(tc.inputMachineTypeUrl)
621+
actualMachineFamily, err := ParseMachineType(tc.inputMachineTypeUrl)
622622
if err != nil && !tc.expectError {
623-
t.Errorf("Got error %v parsing machine family %s; expect no error", err, tc.inputMachineTypeUrl)
623+
t.Errorf("Got error %v parsing machine type %s; expect no error", err, tc.inputMachineTypeUrl)
624624
}
625625
if err == nil && tc.expectError {
626-
t.Errorf("Got no error parsing machine family %s; expect an error", tc.inputMachineTypeUrl)
626+
t.Errorf("Got no error parsing machine type %s; expect an error", tc.inputMachineTypeUrl)
627627
}
628-
if err == nil && actualMachineFamily != tc.expectedMachineFamily {
629-
t.Errorf("Got %s parsing machine family; expect %s", actualMachineFamily, tc.expectedMachineFamily)
628+
if err == nil && actualMachineFamily != tc.expectedMachineType {
629+
t.Errorf("Got %s parsing machine type; expect %s", actualMachineFamily, tc.expectedMachineType)
630630
}
631631
})
632632
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,13 @@ func (gceCS *GCEControllerServer) validateControllerPublishVolumeRequest(ctx con
450450
return project, volKey, nil
451451
}
452452

453-
func parseMachineFamily(machineTypeUrl string) string {
454-
machineFamily, parseErr := common.ParseMachineFamily(machineTypeUrl)
453+
func parseMachineType(machineTypeUrl string) string {
454+
machineType, parseErr := common.ParseMachineType(machineTypeUrl)
455455
if parseErr != nil {
456456
// Parse errors represent an unexpected API change with instance.MachineType; log a warning.
457-
klog.Warningf("ParseMachineFamily(%v): %v", machineTypeUrl, parseErr)
457+
klog.Warningf("ParseMachineType(%v): %v", machineTypeUrl, parseErr)
458458
}
459-
return machineFamily
459+
return machineType
460460
}
461461

462462
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
@@ -539,8 +539,8 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
539539
if errors.As(err, &udErr) {
540540
// If we encountered an UnsupportedDiskError, rewrite the error message to be more user friendly.
541541
// The error message from GCE is phrased around disk create on VM creation, not runtime attach.
542-
machineFamily := parseMachineFamily(instance.MachineType)
543-
return nil, status.Errorf(codes.InvalidArgument, "'%s' is not a compatible disk type with the machine family '%s', please review the GCP online documentation for available persistent disk options", udErr.DiskType, machineFamily)
542+
machineType := parseMachineType(instance.MachineType)
543+
return nil, status.Errorf(codes.InvalidArgument, "'%s' is not a compatible disk type with the machine type %s, please review the GCP online documentation for available persistent disk options", udErr.DiskType, machineType)
544544
}
545545
return nil, status.Errorf(codes.Internal, "unknown Attach error: %v", err.Error())
546546
}

0 commit comments

Comments
 (0)