Prepare to introduce websockets for exec and portforward

Refactor the code in remotecommand to better represent the structure of
what is common between portforward and exec.
This commit is contained in:
Clayton Coleman
2017-07-07 17:54:34 -04:00
parent 95a4a5d6eb
commit 12c7874c0d
10 changed files with 151 additions and 126 deletions

View File

@@ -132,7 +132,7 @@ func TestForwardPorts(t *testing.T) {
server := httptest.NewServer(fakePortForwardServer(t, testName, test.serverSends, test.clientSends))
url, _ := url.Parse(server.URL)
exec, err := remotecommand.NewExecutor(&restclient.Config{}, "POST", url)
exec, err := remotecommand.NewSPDYExecutor(&restclient.Config{}, "POST", url)
if err != nil {
t.Fatal(err)
}
@@ -202,7 +202,7 @@ func TestForwardPortsReturnsErrorWhenAllBindsFailed(t *testing.T) {
defer server.Close()
url, _ := url.Parse(server.URL)
exec, err := remotecommand.NewExecutor(&restclient.Config{}, "POST", url)
exec, err := remotecommand.NewSPDYExecutor(&restclient.Config{}, "POST", url)
if err != nil {
t.Fatal(err)
}

View File

@@ -255,7 +255,7 @@ func TestStream(t *testing.T) {
conf := &restclient.Config{
Host: server.URL,
}
e, err := remoteclient.NewExecutor(conf, "POST", req.URL())
e, err := remoteclient.NewSPDYExecutor(conf, "POST", req.URL())
if err != nil {
t.Errorf("%s: unexpected error: %v", name, err)
continue
@@ -352,7 +352,7 @@ func TestDial(t *testing.T) {
called = true
return rt
}
exec, err := remoteclient.NewStreamExecutor(upgrader, testFn, "POST", &url.URL{Host: "something.com", Scheme: "https"})
exec, err := newStreamExecutor(upgrader, testFn, "POST", &url.URL{Host: "something.com", Scheme: "https"})
if err != nil {
t.Fatal(err)
}
@@ -368,3 +368,20 @@ func TestDial(t *testing.T) {
}
_ = protocol
}
// newStreamExecutor upgrades the request so that it supports multiplexed bidirectional
// streams. This method takes a stream upgrader and an optional function that is invoked
// to wrap the round tripper. This method may be used by clients that are lower level than
// Kubernetes clients or need to provide their own upgrade round tripper.
func newStreamExecutor(upgrader httpstream.UpgradeRoundTripper, fn func(http.RoundTripper) http.RoundTripper, method string, url *url.URL) (StreamExecutor, error) {
rt := http.RoundTripper(upgrader)
if fn != nil {
rt = fn(rt)
}
return &streamExecutor{
upgrader: upgrader,
transport: rt,
method: method,
url: url,
}, nil
}