Skip to content

Commit 2597f43

Browse files
committed
Print machineType, rather than just the machine family when encountering an incompatible disk attach
1 parent 45bb326 commit 2597f43

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
@@ -277,14 +277,14 @@ func ConvertMiBStringToInt64(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

+19-19
Original file line numberDiff line numberDiff line change
@@ -788,27 +788,27 @@ func TestConvertMiBStringToInt64(t *testing.T) {
788788
}
789789
}
790790

791-
func TestParseMachineFamily(t *testing.T) {
791+
func TestParseMachineType(t *testing.T) {
792792
tests := []struct {
793-
desc string
794-
inputMachineTypeUrl string
795-
expectedMachineFamily string
796-
expectError bool
793+
desc string
794+
inputMachineTypeUrl string
795+
expectedMachineType string
796+
expectError bool
797797
}{
798798
{
799-
desc: "full URL machine family",
800-
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4",
801-
expectedMachineFamily: "c3",
799+
desc: "full URL machine type",
800+
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4",
801+
expectedMachineType: "c3-highcpu-4",
802802
},
803803
{
804-
desc: "partial URL machine family",
805-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
806-
expectedMachineFamily: "n2",
804+
desc: "partial URL machine type",
805+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
806+
expectedMachineType: "n2-standard-4",
807807
},
808808
{
809-
desc: "custom partial URL machine family",
810-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
811-
expectedMachineFamily: "e2",
809+
desc: "custom partial URL machine type",
810+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
811+
expectedMachineType: "e2-custom-2-4096",
812812
},
813813
{
814814
desc: "incorrect URL",
@@ -828,15 +828,15 @@ func TestParseMachineFamily(t *testing.T) {
828828
}
829829
for _, tc := range tests {
830830
t.Run(tc.desc, func(t *testing.T) {
831-
actualMachineFamily, err := ParseMachineFamily(tc.inputMachineTypeUrl)
831+
actualMachineFamily, err := ParseMachineType(tc.inputMachineTypeUrl)
832832
if err != nil && !tc.expectError {
833-
t.Errorf("Got error %v parsing machine family %s; expect no error", err, tc.inputMachineTypeUrl)
833+
t.Errorf("Got error %v parsing machine type %s; expect no error", err, tc.inputMachineTypeUrl)
834834
}
835835
if err == nil && tc.expectError {
836-
t.Errorf("Got no error parsing machine family %s; expect an error", tc.inputMachineTypeUrl)
836+
t.Errorf("Got no error parsing machine type %s; expect an error", tc.inputMachineTypeUrl)
837837
}
838-
if err == nil && actualMachineFamily != tc.expectedMachineFamily {
839-
t.Errorf("Got %s parsing machine family; expect %s", actualMachineFamily, tc.expectedMachineFamily)
838+
if err == nil && actualMachineFamily != tc.expectedMachineType {
839+
t.Errorf("Got %s parsing machine type; expect %s", actualMachineFamily, tc.expectedMachineType)
840840
}
841841
})
842842
}

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)