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