Skip to content

Commit

Permalink
client-go: transform watchErrorStream to wrap the underlying erro…
Browse files Browse the repository at this point in the history
…r (#129765)

* `client-go`: transform `watchErrorStream` to wrap the underlying error

This PR transforms the `client-go`'s `watchErrorStream` to wrap the error instead of transforming it into a single string. This enables clients to use `errors.Is/As/Unwrap` with the errors that come out of `StreamWithContext`

Fixes kubernetes/kubernetes#129763

* adjust unit tests

Kubernetes-commit: 067012f5844b7390e7279f575342ae0536f80520
  • Loading branch information
tigrato authored and k8s-publishing-bot committed Jan 23, 2025
1 parent 3b09c13 commit 9f1cce4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tools/remotecommand/errorstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func watchErrorStream(errorStream io.Reader, d errorStreamDecoder) chan error {
message, err := io.ReadAll(errorStream)
switch {
case err != nil && err != io.EOF:
errorChan <- fmt.Errorf("error reading from error stream: %s", err)
errorChan <- fmt.Errorf("error reading from error stream: %w", err)
case len(message) > 0:
errorChan <- d.decode(message)
default:
Expand Down
38 changes: 28 additions & 10 deletions tools/remotecommand/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package remotecommand
import (
"errors"
"io"
"net"
"net/http"
"strings"
"testing"
"time"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/httpstream"
"k8s.io/apimachinery/pkg/util/wait"
)
Expand Down Expand Up @@ -181,17 +182,34 @@ func TestV2ErrorStreamReading(t *testing.T) {
tests := []struct {
name string
stream io.Reader
expectedError error
expectedError func(*testing.T, error)
}{
{
name: "error reading from stream",
stream: &fakeReader{errors.New("foo")},
expectedError: errors.New("error reading from error stream: foo"),
name: "error reading from stream",
stream: &fakeReader{errors.New("foo")},
expectedError: func(t *testing.T, err error) {
if e, a := "error reading from error stream: foo", err.Error(); e != a {
t.Errorf("expected '%s', got '%s'", e, a)
}
},
},
{
name: "stream returns an error",
stream: strings.NewReader("some error"),
expectedError: errors.New("error executing remote command: some error"),
name: "stream returns an error",
stream: strings.NewReader("some error"),
expectedError: func(t *testing.T, err error) {
if e, a := "error executing remote command: some error", err.Error(); e != a {
t.Errorf("expected '%s', got '%s'", e, a)
}
},
},
{
name: "typed error",
stream: &fakeReader{net.ErrClosed},
expectedError: func(t *testing.T, err error) {
if !errors.Is(err, net.ErrClosed) {
t.Errorf("expected errors.Is(err, net.ErrClosed), failed on %#v", err)
}
},
},
}

Expand All @@ -214,8 +232,8 @@ func TestV2ErrorStreamReading(t *testing.T) {
if test.expectedError != nil {
if err == nil {
t.Errorf("%s: expected an error", test.name)
} else if e, a := test.expectedError, err; e.Error() != a.Error() {
t.Errorf("%s: expected %q, got %q", test.name, e, a)
} else {
test.expectedError(t, err)
}
continue
}
Expand Down

0 comments on commit 9f1cce4

Please sign in to comment.