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 @@ -346,6 +346,9 @@ func CodeForError(sourceError error) codes.Code {
346
346
if code , err := isContextError (sourceError ); err == nil {
347
347
return code
348
348
}
349
+ if code , err := isConnectionResetError (sourceError ); err == nil {
350
+ return code
351
+ }
349
352
350
353
var apiErr * googleapi.Error
351
354
if ! errors .As (sourceError , & apiErr ) {
@@ -377,6 +380,20 @@ func isContextError(err error) (codes.Code, error) {
377
380
return codes .Unknown , fmt .Errorf ("Not a context error: %w" , err )
378
381
}
379
382
383
+ // isConnectionResetError returns the grpc error code Unavailable if the
384
+ // passed in error contains the "connection reset by peer" string.
385
+ func isConnectionResetError (err error ) (codes.Code , error ) {
386
+ if err == nil {
387
+ return codes .Unknown , fmt .Errorf ("null error" )
388
+ }
389
+
390
+ errStr := err .Error ()
391
+ if strings .Contains (errStr , "connection reset by peer" ) {
392
+ return codes .Unavailable , nil
393
+ }
394
+ return codes .Unknown , fmt .Errorf ("Not a connection reset error: %w" , err )
395
+ }
396
+
380
397
// isUserMultiAttachError returns an InvalidArgument if the error is
381
398
// multi-attach detected from the API server. If we get this error from the API
382
399
// 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"
@@ -1006,6 +1007,16 @@ func TestCodeForError(t *testing.T) {
1006
1007
inputErr : context .DeadlineExceeded ,
1007
1008
expCode : codes .DeadlineExceeded ,
1008
1009
},
1010
+ {
1011
+ name : "connection reset error" ,
1012
+ inputErr : fmt .Errorf ("failed to getDisk: connection reset by peer" ),
1013
+ expCode : codes .Unavailable ,
1014
+ },
1015
+ {
1016
+ name : "wrapped connection reset error" ,
1017
+ inputErr : fmt .Errorf ("received error: %v" , syscall .ECONNRESET ),
1018
+ expCode : codes .Unavailable ,
1019
+ },
1009
1020
{
1010
1021
name : "status error with Aborted error code" ,
1011
1022
inputErr : status .Error (codes .Aborted , "aborted error" ),
You can’t perform that action at this time.
0 commit comments