Skip to content

Commit 26f73b6

Browse files
committed
Rewrite PD CSI error message when encountering diskType unsupported error
1 parent fc55b59 commit 26f73b6

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

pkg/common/utils.go

+20
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,19 @@ const (
5959
multiRegionalLocationFmt = "^[a-z]+$"
6060
// Example: us-east1
6161
regionalLocationFmt = "^[a-z]+-[a-z]+[0-9]$"
62+
63+
// Full or partial URL of the machine type resource, in the format:
64+
// zones/zone/machineTypes/machine-type
65+
machineTypePattern = "zones/[^/]+/machineTypes/([^/]+)$"
6266
)
6367

6468
var (
6569
multiRegionalPattern = regexp.MustCompile(multiRegionalLocationFmt)
6670
regionalPattern = regexp.MustCompile(regionalLocationFmt)
71+
72+
// Full or partial URL of the machine type resource, in the format:
73+
// zones/zone/machineTypes/machine-type
74+
machineTypeRegex = regexp.MustCompile(machineTypePattern)
6775
)
6876

6977
func BytesToGbRoundDown(bytes int64) int64 {
@@ -268,3 +276,15 @@ func ConvertMiStringToInt64(str string) (int64, error) {
268276
}
269277
return volumehelpers.RoundUpToMiB(quantity)
270278
}
279+
280+
// ParseMachineTypeFromUrl returns an extracted machineType from a URL, or empty if not found.
281+
// machineTypeUrl: Full or partial URL of the machine type resource, in the format:
282+
//
283+
// zones/zone/machineTypes/machine-type
284+
func ParseMachineFamily(machineTypeUrl string) (string, error) {
285+
machineType := machineTypeRegex.FindStringSubmatch(machineTypeUrl)
286+
if machineType == nil {
287+
return "", fmt.Errorf("failed to parse machineTypeUrl. expected suffix zones/{zone}/machineTypes/{name}. Got: %s", machineTypeUrl)
288+
}
289+
return strings.Split(machineType[1], "-")[0], nil
290+
}

pkg/common/utils_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,57 @@ func TestConvertMiStringToInt64(t *testing.T) {
799799
})
800800
}
801801
}
802+
803+
func TestParseMachineFamily(t *testing.T) {
804+
tests := []struct {
805+
desc string
806+
inputMachineTypeUrl string
807+
expectedMachineFamily string
808+
expectError bool
809+
}{
810+
{
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",
814+
},
815+
{
816+
desc: "partial URL machine family",
817+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
818+
expectedMachineFamily: "n2",
819+
},
820+
{
821+
desc: "custom partial URL machine family",
822+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
823+
expectedMachineFamily: "e2",
824+
},
825+
{
826+
desc: "incorrect URL",
827+
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/psch-gke-dev/zones/us-central1-c",
828+
expectError: true,
829+
},
830+
{
831+
desc: "incorrect partial URL",
832+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/",
833+
expectError: true,
834+
},
835+
{
836+
desc: "missing zone",
837+
inputMachineTypeUrl: "zones//machineTypes/n2-standard-4",
838+
expectError: true,
839+
},
840+
}
841+
for _, tc := range tests {
842+
t.Run(tc.desc, func(t *testing.T) {
843+
actualMachineFamily, err := ParseMachineFamily(tc.inputMachineTypeUrl)
844+
if err != nil && !tc.expectError {
845+
t.Errorf("Got error %v parsing machine family %s; expect no error", err, tc.inputMachineTypeUrl)
846+
}
847+
if err == nil && tc.expectError {
848+
t.Errorf("Got no error converting string to int64 %s; expect an error", tc.inputMachineTypeUrl)
849+
}
850+
if err == nil && actualMachineFamily != tc.expectedMachineFamily {
851+
t.Errorf("Got %s for parsing machine family; expect %s", actualMachineFamily, tc.expectedMachineFamily)
852+
}
853+
})
854+
}
855+
}

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"encoding/json"
2020
"fmt"
21+
"regexp"
2122
"strings"
2223
"time"
2324

@@ -38,9 +39,11 @@ const (
3839
waitForImageCreationTimeOut = 5 * time.Minute
3940
diskKind = "compute#disk"
4041
cryptoKeyVerDelimiter = "/cryptoKeyVersions"
42+
pdDiskTypeUnsupportedPattern = `\[([a-z-]+)\] features are not compatible for creating instance`
4143
)
4244

4345
var hyperdiskTypes = []string{"hyperdisk-extreme", "hyperdisk-throughput"}
46+
var pdDiskTypeUnsupportedRegex = regexp.MustCompile(pdDiskTypeUnsupportedPattern)
4447

4548
type GCEAPIVersion string
4649

@@ -69,6 +72,15 @@ var WaitForOpBackoff = wait.Backoff{
6972
Steps: 100,
7073
Cap: 0}
7174

75+
// Custom error type to propagate error messages up to clients.
76+
type UnsupportedDiskError struct {
77+
DiskType string
78+
}
79+
80+
func (udErr *UnsupportedDiskError) Error() string {
81+
return ""
82+
}
83+
7284
type GCECompute interface {
7385
// Metadata information
7486
GetDefaultProject() string
@@ -838,12 +850,23 @@ func (cloud *CloudProvider) WaitForAttach(ctx context.Context, project string, v
838850
})
839851
}
840852

853+
func wrapOpErr(name string, opErr *computev1.OperationErrorErrors) error {
854+
if opErr.Code == "UNSUPPORTED_OPERATION" {
855+
if diskType := pdDiskTypeUnsupportedRegex.FindString(opErr.Message); diskType != "" {
856+
return &UnsupportedDiskError{
857+
DiskType: diskType,
858+
}
859+
}
860+
}
861+
return fmt.Errorf("operation %v failed (%v): %v", name, opErr.Code, opErr.Message)
862+
}
863+
841864
func opIsDone(op *computev1.Operation) (bool, error) {
842865
if op == nil || op.Status != operationStatusDone {
843866
return false, nil
844867
}
845868
if op.Error != nil && len(op.Error.Errors) > 0 && op.Error.Errors[0] != nil {
846-
return true, fmt.Errorf("operation %v failed (%v): %v", op.Name, op.Error.Errors[0].Code, op.Error.Errors[0].Message)
869+
return true, wrapOpErr(op.Name, op.Error.Errors[0])
847870
}
848871
return true, nil
849872
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package gceGCEDriver
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021
"math/rand"
2122
"regexp"
@@ -506,6 +507,15 @@ func (gceCS *GCEControllerServer) validateControllerPublishVolumeRequest(ctx con
506507
return project, volKey, nil
507508
}
508509

510+
func parseMachineFamily(machineTypeUrl string) string {
511+
machineFamily, parseErr := common.ParseMachineFamily(machineTypeUrl)
512+
if parseErr != nil {
513+
// Parse errors represent an unexpected API change with instance.MachineType; log a warning.
514+
klog.Warningf("ParseMachineFamily(%v): %v", machineTypeUrl, parseErr)
515+
}
516+
return machineFamily
517+
}
518+
509519
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
510520
project, volKey, err := gceCS.validateControllerPublishVolumeRequest(ctx, req)
511521

@@ -582,6 +592,13 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
582592
}
583593
err = gceCS.CloudProvider.AttachDisk(ctx, project, volKey, readWrite, attachableDiskTypePersistent, instanceZone, instanceName)
584594
if err != nil {
595+
var udErr *gce.UnsupportedDiskError
596+
if errors.As(err, &udErr) {
597+
// If we encountered an UnsupportedDiskError, rewrite the error message to be more user friendly.
598+
// 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.Internal, "'%s' is not a compatible disk type with the machine family '%s', please review the GKE online documentation for available persistent disk options", udErr.DiskType, machineFamily)
601+
}
585602
return nil, status.Errorf(codes.Internal, "unknown Attach error: %v", err.Error())
586603
}
587604

0 commit comments

Comments
 (0)