Skip to content

Commit b81aa67

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

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

pkg/common/utils.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ func ConvertMiStringToInt64(str string) (int64, error) {
277277
return volumehelpers.RoundUpToMiB(quantity)
278278
}
279279

280-
// ParseMachineTypeFromUrl returns an extracted machineType from a URL, or empty if not found.
280+
// ParseMachineType returns an extracted machineType from a URL, or empty if not found.
281281
// machineTypeUrl: Full or partial URL of the machine type resource, in the format:
282282
//
283283
// zones/zone/machineTypes/machine-type
284-
func ParseMachineFamily(machineTypeUrl string) (string, error) {
284+
func ParseMachineType(machineTypeUrl string) (string, error) {
285285
machineType := machineTypeRegex.FindStringSubmatch(machineTypeUrl)
286286
if machineType == nil {
287-
return "", fmt.Errorf("failed to parse machineTypeUrl. expected suffix zones/{zone}/machineTypes/{machine-type}. Got: %s", machineTypeUrl)
287+
return "", fmt.Errorf("failed to parse machineTypeUrl. Expected suffix: zones/{zone}/machineTypes/{machine-type}. Got: %s", machineTypeUrl)
288288
}
289-
return strings.Split(machineType[1], "-")[0], nil
289+
return machineType[1], nil
290290
}

pkg/common/utils_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -800,27 +800,27 @@ func TestConvertMiStringToInt64(t *testing.T) {
800800
}
801801
}
802802

803-
func TestParseMachineFamily(t *testing.T) {
803+
func TestParseMachineType(t *testing.T) {
804804
tests := []struct {
805805
desc string
806806
inputMachineTypeUrl string
807-
expectedMachineFamily string
807+
expectedMachineType string
808808
expectError bool
809809
}{
810810
{
811-
desc: "full URL machine family",
812-
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4",
813-
expectedMachineFamily: "c3",
811+
desc: "full URL machine type",
812+
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4",
813+
expectedMachineType: "c3-highcpu-4",
814814
},
815815
{
816-
desc: "partial URL machine family",
817-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
818-
expectedMachineFamily: "n2",
816+
desc: "partial URL machine type",
817+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
818+
expectedMachineType: "n2-standard-4",
819819
},
820820
{
821-
desc: "custom partial URL machine family",
822-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
823-
expectedMachineFamily: "e2",
821+
desc: "custom partial URL machine type",
822+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
823+
expectedMachineType: "e2-custom-2-4096",
824824
},
825825
{
826826
desc: "incorrect URL",
@@ -840,15 +840,15 @@ func TestParseMachineFamily(t *testing.T) {
840840
}
841841
for _, tc := range tests {
842842
t.Run(tc.desc, func(t *testing.T) {
843-
actualMachineFamily, err := ParseMachineFamily(tc.inputMachineTypeUrl)
843+
actualMachineFamily, err := ParseMachineType(tc.inputMachineTypeUrl)
844844
if err != nil && !tc.expectError {
845-
t.Errorf("Got error %v parsing machine family %s; expect no error", err, tc.inputMachineTypeUrl)
845+
t.Errorf("Got error %v parsing machine type %s; expect no error", err, tc.inputMachineTypeUrl)
846846
}
847847
if err == nil && tc.expectError {
848-
t.Errorf("Got no error parsing machine family %s; expect an error", tc.inputMachineTypeUrl)
848+
t.Errorf("Got no error parsing machine type %s; expect an error", tc.inputMachineTypeUrl)
849849
}
850-
if err == nil && actualMachineFamily != tc.expectedMachineFamily {
851-
t.Errorf("Got %s parsing machine family; expect %s", actualMachineFamily, tc.expectedMachineFamily)
850+
if err == nil && actualMachineFamily != tc.expectedMachineType {
851+
t.Errorf("Got %s parsing machine type; expect %s", actualMachineFamily, tc.expectedMachineType)
852852
}
853853
})
854854
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,13 @@ func (gceCS *GCEControllerServer) validateControllerPublishVolumeRequest(ctx con
507507
return project, volKey, nil
508508
}
509509

510-
func parseMachineFamily(machineTypeUrl string) string {
511-
machineFamily, parseErr := common.ParseMachineFamily(machineTypeUrl)
510+
func parseMachineType(machineTypeUrl string) string {
511+
machineType, parseErr := common.ParseMachineType(machineTypeUrl)
512512
if parseErr != nil {
513513
// Parse errors represent an unexpected API change with instance.MachineType; log a warning.
514-
klog.Warningf("ParseMachineFamily(%v): %v", machineTypeUrl, parseErr)
514+
klog.Warningf("ParseMachineType(%v): %v", machineTypeUrl, parseErr)
515515
}
516-
return machineFamily
516+
return machineType
517517
}
518518

519519
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
@@ -596,8 +596,8 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
596596
if errors.As(err, &udErr) {
597597
// If we encountered an UnsupportedDiskError, rewrite the error message to be more user friendly.
598598
// The error message from GCE is phrased around disk create on VM creation, not runtime attach.
599-
machineFamily := parseMachineFamily(instance.MachineType)
600-
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)
599+
machineType := parseMachineType(instance.MachineType)
600+
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)
601601
}
602602
return nil, status.Errorf(codes.Internal, "unknown Attach error: %v", err.Error())
603603
}

0 commit comments

Comments
 (0)