@@ -26,6 +26,7 @@ import (
26
26
"time"
27
27
28
28
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
29
+ "github.com/googleapis/gax-go/v2/apierror"
29
30
"golang.org/x/time/rate"
30
31
"google.golang.org/api/googleapi"
31
32
"google.golang.org/grpc/codes"
@@ -423,7 +424,6 @@ func CodeForError(sourceError error) codes.Code {
423
424
if sourceError == nil {
424
425
return codes .Internal
425
426
}
426
-
427
427
if code , err := isUserMultiAttachError (sourceError ); err == nil {
428
428
return code
429
429
}
@@ -433,12 +433,7 @@ func CodeForError(sourceError error) codes.Code {
433
433
if code , err := isContextError (sourceError ); err == nil {
434
434
return code
435
435
}
436
-
437
- var apiErr * googleapi.Error
438
- if ! errors .As (sourceError , & apiErr ) {
439
- return codes .Internal
440
- }
441
- if code , ok := userErrorCodeMap [apiErr .Code ]; ok {
436
+ if code , err := isGoogleAPIError (sourceError ); err == nil {
442
437
return code
443
438
}
444
439
@@ -475,16 +470,64 @@ func isUserMultiAttachError(err error) (codes.Code, error) {
475
470
return codes .Unknown , fmt .Errorf ("Not a user multiattach error: %w" , err )
476
471
}
477
472
473
+ // existingErrorCode returns the existing gRPC Status error code for the given error, if one exists,
474
+ // or Unknown if one doesn't exist. Since github.com/googleapis/gax-go/v2/apierror now wraps googleapi
475
+ // errors (returned from GCE API calls), and sets their status error code to Unknown, we now have to
476
+ // make sure we only return existing error codes from errors that are either TemporaryErrors, or errors
477
+ // that do not wrap googleAPI errors. Otherwise, we will return Unknown for all GCE API calls that
478
+ // return googleapi errors.
478
479
func existingErrorCode (err error ) (codes.Code , error ) {
479
480
if err == nil {
480
481
return codes .Unknown , fmt .Errorf ("null error" )
481
482
}
482
- if status , ok := status .FromError (err ); ok {
483
- return status .Code (), nil
483
+ var tmpError * TemporaryError
484
+ // This explicitly checks our error is a temporary error before extracting its
485
+ // status, as there can be other errors that can qualify as statusable
486
+ // while not necessarily being temporary.
487
+ if errors .As (err , & tmpError ) {
488
+ if status , ok := status .FromError (err ); ok {
489
+ return status .Code (), nil
490
+ }
491
+ }
492
+ // We want to make sure we catch other error types that are statusable.
493
+ // (eg. grpc-go/internal/status/status.go Error struct that wraps a status)
494
+ var googleErr * googleapi.Error
495
+ if ! errors .As (err , & googleErr ) {
496
+ if status , ok := status .FromError (err ); ok {
497
+ return status .Code (), nil
498
+ }
484
499
}
500
+
485
501
return codes .Unknown , fmt .Errorf ("no existing error code for %w" , err )
486
502
}
487
503
504
+ // isGoogleAPIError returns the gRPC status code for the given googleapi error by mapping
505
+ // the googleapi error's HTTP code to the corresponding gRPC error code. If the error is
506
+ // wrapped in an APIError (github.com/googleapis/gax-go/v2/apierror), it maps the wrapped
507
+ // googleAPI error's HTTP code to the corresponding gRPC error code. Returns an error if
508
+ // the given error is not a googleapi error.
509
+ func isGoogleAPIError (err error ) (codes.Code , error ) {
510
+ var googleErr * googleapi.Error
511
+ if ! errors .As (err , & googleErr ) {
512
+ return codes .Unknown , fmt .Errorf ("error %w is not a googleapi.Error" , err )
513
+ }
514
+ var sourceCode int
515
+ var apiErr * apierror.APIError
516
+ if errors .As (err , & apiErr ) {
517
+ // When googleapi.Err is used as a wrapper, we return the error code of the wrapped contents.
518
+ sourceCode = apiErr .HTTPCode ()
519
+ } else {
520
+ // Rely on error code in googleapi.Err when it is our primary error.
521
+ sourceCode = googleErr .Code
522
+ }
523
+ // Map API error code to user error code.
524
+ if code , ok := userErrorCodeMap [sourceCode ]; ok {
525
+ return code , nil
526
+ }
527
+
528
+ return codes .Unknown , fmt .Errorf ("googleapi.Error %w does not map to any known errors" , err )
529
+ }
530
+
488
531
func LoggedError (msg string , err error ) error {
489
532
klog .Errorf (msg + "%v" , err .Error ())
490
533
return status .Errorf (CodeForError (err ), msg + "%v" , err .Error ())
0 commit comments