Skip to content

Commit f3f566e

Browse files
authored
Merge pull request #1720 from amacaskill/filter-out-connection-reset
Return Unavailable for 'connection reset by peer' errors
2 parents 629ddec + 094cfbd commit f3f566e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/common/utils.go

+17
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ func CodeForError(sourceError error) codes.Code {
433433
if code, err := isContextError(sourceError); err == nil {
434434
return code
435435
}
436+
if code, err := isConnectionResetError(sourceError); err == nil {
437+
return code
438+
}
436439

437440
var apiErr *googleapi.Error
438441
if !errors.As(sourceError, &apiErr) {
@@ -464,6 +467,20 @@ func isContextError(err error) (codes.Code, error) {
464467
return codes.Unknown, fmt.Errorf("Not a context error: %w", err)
465468
}
466469

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+
467484
// isUserMultiAttachError returns an InvalidArgument if the error is
468485
// multi-attach detected from the API server. If we get this error from the API
469486
// server, it means that the kubelet doesn't know about the multiattch so it is

pkg/common/utils_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net/http"
2424
"reflect"
25+
"syscall"
2526
"testing"
2627

2728
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
@@ -1235,6 +1236,16 @@ func TestCodeForError(t *testing.T) {
12351236
inputErr: context.DeadlineExceeded,
12361237
expCode: codes.DeadlineExceeded,
12371238
},
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+
},
12381249
{
12391250
name: "status error with Aborted error code",
12401251
inputErr: status.Error(codes.Aborted, "aborted error"),

0 commit comments

Comments
 (0)