Skip to content

Commit 0fca5e7

Browse files
authored
Merge pull request #1813 from k8s-infra-cherrypick-robot/cherry-pick-1720-to-release-1.12
[release-1.12] Return Unavailable for 'connection reset by peer' errors
2 parents dbdaa47 + eda0bb6 commit 0fca5e7

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
@@ -344,6 +344,9 @@ func CodeForError(sourceError error) codes.Code {
344344
if code, err := isContextError(sourceError); err == nil {
345345
return code
346346
}
347+
if code, err := isConnectionResetError(sourceError); err == nil {
348+
return code
349+
}
347350

348351
var apiErr *googleapi.Error
349352
if !errors.As(sourceError, &apiErr) {
@@ -375,6 +378,20 @@ func isContextError(err error) (codes.Code, error) {
375378
return codes.Unknown, fmt.Errorf("Not a context error: %w", err)
376379
}
377380

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

0 commit comments

Comments
 (0)