Skip to content

Commit 2f66350

Browse files
committed
filter out gce operation error codes caused by user errors
1 parent 26fe389 commit 2f66350

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -856,14 +856,36 @@ func (cloud *CloudProvider) WaitForAttach(ctx context.Context, project string, v
856856
}
857857

858858
func wrapOpErr(name string, opErr *computev1.OperationErrorErrors) error {
859+
if opErr == nil {
860+
return nil
861+
}
862+
859863
if opErr.Code == "UNSUPPORTED_OPERATION" {
860864
if diskType := pdDiskTypeUnsupportedRegex.FindStringSubmatch(opErr.Message); diskType != nil {
861865
return &UnsupportedDiskError{
862866
DiskType: diskType[1],
863867
}
864868
}
865869
}
866-
return fmt.Errorf("operation %v failed (%v): %v", name, opErr.Code, opErr.Message)
870+
grpcErrCode := codeForGCEOpError(*opErr)
871+
return status.Errorf(grpcErrCode, "operation %v failed (%v): %w", name, opErr.Code, opErr.Message)
872+
}
873+
874+
// codeForGCEOpError return the grpc error code for the passed in
875+
// gce operation error.
876+
func codeForGCEOpError(err computev1.OperationErrorErrors) codes.Code {
877+
userErrors := map[string]codes.Code{
878+
"RESOURCE_NOT_FOUND": codes.NotFound,
879+
"RESOURCE_ALREADY_EXISTS": codes.AlreadyExists,
880+
"OPERATION_CANCELED_BY_USER": codes.Aborted,
881+
"REGION_QUOTA_EXCEEDED": codes.ResourceExhausted,
882+
"QUOTA_EXCEEDED": codes.ResourceExhausted,
883+
"INVALID_USAGE": codes.InvalidArgument,
884+
}
885+
if code, ok := userErrors[err.Code]; ok {
886+
return code
887+
}
888+
return codes.Internal
867889
}
868890

869891
func opIsDone(op *computev1.Operation) (bool, error) {

0 commit comments

Comments
 (0)