Skip to content

Commit decc0c9

Browse files
authored
Merge pull request #1222 from pwschuurman/automated-cherry-pick-of-#1212-upstream-release-1.9
Automated cherry pick of #1212: Rewrite PD CSI error message when encountering diskType
2 parents 68d70b8 + 2597f43 commit decc0c9

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

pkg/common/utils.go

Lines changed: 20 additions & 0 deletions
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 ConvertMiBStringToInt64(str string) (int64, error) {
268276
}
269277
return volumehelpers.RoundUpToMiB(quantity)
270278
}
279+
280+
// ParseMachineType 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 ParseMachineType(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/{machine-type}. Got: %s", machineTypeUrl)
288+
}
289+
return machineType[1], nil
290+
}

pkg/common/utils_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,57 @@ func TestConvertMiBStringToInt64(t *testing.T) {
787787
})
788788
}
789789
}
790+
791+
func TestParseMachineType(t *testing.T) {
792+
tests := []struct {
793+
desc string
794+
inputMachineTypeUrl string
795+
expectedMachineType string
796+
expectError bool
797+
}{
798+
{
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",
802+
},
803+
{
804+
desc: "partial URL machine type",
805+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
806+
expectedMachineType: "n2-standard-4",
807+
},
808+
{
809+
desc: "custom partial URL machine type",
810+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
811+
expectedMachineType: "e2-custom-2-4096",
812+
},
813+
{
814+
desc: "incorrect URL",
815+
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/psch-gke-dev/zones/us-central1-c",
816+
expectError: true,
817+
},
818+
{
819+
desc: "incorrect partial URL",
820+
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/",
821+
expectError: true,
822+
},
823+
{
824+
desc: "missing zone",
825+
inputMachineTypeUrl: "zones//machineTypes/n2-standard-4",
826+
expectError: true,
827+
},
828+
}
829+
for _, tc := range tests {
830+
t.Run(tc.desc, func(t *testing.T) {
831+
actualMachineFamily, err := ParseMachineType(tc.inputMachineTypeUrl)
832+
if err != nil && !tc.expectError {
833+
t.Errorf("Got error %v parsing machine type %s; expect no error", err, tc.inputMachineTypeUrl)
834+
}
835+
if err == nil && tc.expectError {
836+
t.Errorf("Got no error parsing machine type %s; expect an error", tc.inputMachineTypeUrl)
837+
}
838+
if err == nil && actualMachineFamily != tc.expectedMachineType {
839+
t.Errorf("Got %s parsing machine type; expect %s", actualMachineFamily, tc.expectedMachineType)
840+
}
841+
})
842+
}
843+
}

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

Lines changed: 25 additions & 1 deletion
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,12 @@ const (
3839
waitForImageCreationTimeOut = 5 * time.Minute
3940
diskKind = "compute#disk"
4041
cryptoKeyVerDelimiter = "/cryptoKeyVersions"
42+
// Example message: "[pd-standard] features are not compatible for creating instance"
43+
pdDiskTypeUnsupportedPattern = `\[([a-z-]+)\] features are not compatible for creating instance`
4144
)
4245

4346
var hyperdiskTypes = []string{"hyperdisk-extreme", "hyperdisk-throughput"}
47+
var pdDiskTypeUnsupportedRegex = regexp.MustCompile(pdDiskTypeUnsupportedPattern)
4448

4549
type GCEAPIVersion string
4650

@@ -69,6 +73,15 @@ var WaitForOpBackoff = wait.Backoff{
6973
Steps: 100,
7074
Cap: 0}
7175

76+
// Custom error type to propagate error messages up to clients.
77+
type UnsupportedDiskError struct {
78+
DiskType string
79+
}
80+
81+
func (udErr *UnsupportedDiskError) Error() string {
82+
return ""
83+
}
84+
7285
type GCECompute interface {
7386
// Metadata information
7487
GetDefaultProject() string
@@ -838,12 +851,23 @@ func (cloud *CloudProvider) WaitForAttach(ctx context.Context, project string, v
838851
})
839852
}
840853

854+
func wrapOpErr(name string, opErr *computev1.OperationErrorErrors) error {
855+
if opErr.Code == "UNSUPPORTED_OPERATION" {
856+
if diskType := pdDiskTypeUnsupportedRegex.FindStringSubmatch(opErr.Message); diskType != nil {
857+
return &UnsupportedDiskError{
858+
DiskType: diskType[1],
859+
}
860+
}
861+
}
862+
return fmt.Errorf("operation %v failed (%v): %v", name, opErr.Code, opErr.Message)
863+
}
864+
841865
func opIsDone(op *computev1.Operation) (bool, error) {
842866
if op == nil || op.Status != operationStatusDone {
843867
return false, nil
844868
}
845869
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)
870+
return true, wrapOpErr(op.Name, op.Error.Errors[0])
847871
}
848872
return true, nil
849873
}

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

Lines changed: 17 additions & 0 deletions
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"
@@ -449,6 +450,15 @@ func (gceCS *GCEControllerServer) validateControllerPublishVolumeRequest(ctx con
449450
return project, volKey, nil
450451
}
451452

453+
func parseMachineType(machineTypeUrl string) string {
454+
machineType, parseErr := common.ParseMachineType(machineTypeUrl)
455+
if parseErr != nil {
456+
// Parse errors represent an unexpected API change with instance.MachineType; log a warning.
457+
klog.Warningf("ParseMachineType(%v): %v", machineTypeUrl, parseErr)
458+
}
459+
return machineType
460+
}
461+
452462
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
453463
project, volKey, err := gceCS.validateControllerPublishVolumeRequest(ctx, req)
454464

@@ -525,6 +535,13 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
525535
}
526536
err = gceCS.CloudProvider.AttachDisk(ctx, project, volKey, readWrite, attachableDiskTypePersistent, instanceZone, instanceName)
527537
if err != nil {
538+
var udErr *gce.UnsupportedDiskError
539+
if errors.As(err, &udErr) {
540+
// If we encountered an UnsupportedDiskError, rewrite the error message to be more user friendly.
541+
// The error message from GCE is phrased around disk create on VM creation, not runtime attach.
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)
544+
}
528545
return nil, status.Errorf(codes.Internal, "unknown Attach error: %v", err.Error())
529546
}
530547

0 commit comments

Comments
 (0)