File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -433,6 +433,9 @@ func CodeForError(sourceError error) codes.Code {
433
433
if code , err := isContextError (sourceError ); err == nil {
434
434
return code
435
435
}
436
+ if code , err := isConnectionResetError (sourceError ); err == nil {
437
+ return code
438
+ }
436
439
437
440
var apiErr * googleapi.Error
438
441
if ! errors .As (sourceError , & apiErr ) {
@@ -464,6 +467,20 @@ func isContextError(err error) (codes.Code, error) {
464
467
return codes .Unknown , fmt .Errorf ("Not a context error: %w" , err )
465
468
}
466
469
470
+ // isConnectionResetError returns the grpc error code Unavailable if the
471
+ // passed in error contains the "connection reset by peer" string.
472
+ func isConnectionResetError (err error ) (codes.Code , error ) {
473
+ if err == nil {
474
+ return codes .Unknown , fmt .Errorf ("null error" )
475
+ }
476
+
477
+ errStr := err .Error ()
478
+ if strings .Contains (errStr , "connection reset by peer" ) {
479
+ return codes .Unavailable , nil
480
+ }
481
+ return codes .Unknown , fmt .Errorf ("Not a connection reset error: %w" , err )
482
+ }
483
+
467
484
// isUserMultiAttachError returns an InvalidArgument if the error is
468
485
// multi-attach detected from the API server. If we get this error from the API
469
486
// server, it means that the kubelet doesn't know about the multiattch so it is
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"net/http"
24
24
"reflect"
25
+ "syscall"
25
26
"testing"
26
27
27
28
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
@@ -1235,6 +1236,16 @@ func TestCodeForError(t *testing.T) {
1235
1236
inputErr : context .DeadlineExceeded ,
1236
1237
expCode : codes .DeadlineExceeded ,
1237
1238
},
1239
+ {
1240
+ name : "connection reset error" ,
1241
+ inputErr : fmt .Errorf ("failed to getDisk: connection reset by peer" ),
1242
+ expCode : codes .Unavailable ,
1243
+ },
1244
+ {
1245
+ name : "wrapped connection reset error" ,
1246
+ inputErr : fmt .Errorf ("received error: %v" , syscall .ECONNRESET ),
1247
+ expCode : codes .Unavailable ,
1248
+ },
1238
1249
{
1239
1250
name : "status error with Aborted error code" ,
1240
1251
inputErr : status .Error (codes .Aborted , "aborted error" ),
You can’t perform that action at this time.
0 commit comments