Skip to content

Commit 31081aa

Browse files
authored
Merge pull request #1724 from k8s-infra-cherrypick-robot/cherry-pick-1720-to-release-1.13
[release-1.13] Return Unavailable for 'connection reset by peer' errors
2 parents 9675b36 + cd0f788 commit 31081aa

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
@@ -346,6 +346,9 @@ func CodeForError(sourceError error) codes.Code {
346346
if code, err := isContextError(sourceError); err == nil {
347347
return code
348348
}
349+
if code, err := isConnectionResetError(sourceError); err == nil {
350+
return code
351+
}
349352

350353
var apiErr *googleapi.Error
351354
if !errors.As(sourceError, &apiErr) {
@@ -377,6 +380,20 @@ func isContextError(err error) (codes.Code, error) {
377380
return codes.Unknown, fmt.Errorf("Not a context error: %w", err)
378381
}
379382

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+
380397
// isUserMultiAttachError returns an InvalidArgument if the error is
381398
// multi-attach detected from the API server. If we get this error from the API
382399
// 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"
@@ -1006,6 +1007,16 @@ func TestCodeForError(t *testing.T) {
10061007
inputErr: context.DeadlineExceeded,
10071008
expCode: codes.DeadlineExceeded,
10081009
},
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+
},
10091020
{
10101021
name: "status error with Aborted error code",
10111022
inputErr: status.Error(codes.Aborted, "aborted error"),

0 commit comments

Comments
 (0)