Skip to content

Return Unavailable for 'connection reset by peer' errors #1720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ func CodeForError(sourceError error) codes.Code {
if code, err := isContextError(sourceError); err == nil {
return code
}
if code, err := isConnectionResetError(sourceError); err == nil {
return code
}

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

// isConnectionResetError returns the grpc error code Unavailable if the
// passed in error contains the "connection reset by peer" string.
func isConnectionResetError(err error) (codes.Code, error) {
if err == nil {
return codes.Unknown, fmt.Errorf("null error")
}

errStr := err.Error()
if strings.Contains(errStr, "connection reset by peer") {
return codes.Unavailable, nil
}
return codes.Unknown, fmt.Errorf("Not a connection reset error: %w", err)
}

// isUserMultiAttachError returns an InvalidArgument if the error is
// multi-attach detected from the API server. If we get this error from the API
// server, it means that the kubelet doesn't know about the multiattch so it is
Expand Down
11 changes: 11 additions & 0 deletions pkg/common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net/http"
"reflect"
"syscall"
"testing"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
Expand Down Expand Up @@ -1235,6 +1236,16 @@ func TestCodeForError(t *testing.T) {
inputErr: context.DeadlineExceeded,
expCode: codes.DeadlineExceeded,
},
{
name: "connection reset error",
inputErr: fmt.Errorf("failed to getDisk: connection reset by peer"),
expCode: codes.Unavailable,
},
{
name: "wrapped connection reset error",
inputErr: fmt.Errorf("received error: %v", syscall.ECONNRESET),
expCode: codes.Unavailable,
},
{
name: "status error with Aborted error code",
inputErr: status.Error(codes.Aborted, "aborted error"),
Expand Down