Skip to content

Commit aa9d06d

Browse files
committed
test/e2e/framework:move OpenWebSocketForURL to subpackage(new) websocket
1 parent c441a1a commit aa9d06d

File tree

9 files changed

+115
-59
lines changed

9 files changed

+115
-59
lines changed

test/e2e/common/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ go_library(
7878
"//test/e2e/framework/rc:go_default_library",
7979
"//test/e2e/framework/skipper:go_default_library",
8080
"//test/e2e/framework/volume:go_default_library",
81+
"//test/e2e/framework/websocket:go_default_library",
8182
"//test/utils:go_default_library",
8283
"//test/utils/image:go_default_library",
8384
"//vendor/github.com/onsi/ginkgo:go_default_library",

test/e2e/common/pods.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"k8s.io/kubernetes/test/e2e/framework"
4545
e2ekubelet "k8s.io/kubernetes/test/e2e/framework/kubelet"
4646
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
47+
e2ewebsocket "k8s.io/kubernetes/test/e2e/framework/websocket"
4748
imageutils "k8s.io/kubernetes/test/utils/image"
4849

4950
"github.com/onsi/ginkgo"
@@ -588,7 +589,7 @@ var _ = framework.KubeDescribe("Pods", func() {
588589
Param("command", "remote execution test")
589590

590591
url := req.URL()
591-
ws, err := framework.OpenWebSocketForURL(url, config, []string{"channel.k8s.io"})
592+
ws, err := e2ewebsocket.OpenWebSocketForURL(url, config, []string{"channel.k8s.io"})
592593
if err != nil {
593594
framework.Failf("Failed to open websocket to %s: %v", url.String(), err)
594595
}
@@ -667,7 +668,7 @@ var _ = framework.KubeDescribe("Pods", func() {
667668

668669
url := req.URL()
669670

670-
ws, err := framework.OpenWebSocketForURL(url, config, []string{"binary.k8s.io"})
671+
ws, err := e2ewebsocket.OpenWebSocketForURL(url, config, []string{"binary.k8s.io"})
671672
if err != nil {
672673
framework.Failf("Failed to open websocket to %s: %v", url.String(), err)
673674
}

test/e2e/framework/.import-restrictions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
"k8s.io/kubernetes/test/e2e/framework/service",
259259
"k8s.io/kubernetes/test/e2e/framework/ssh",
260260
"k8s.io/kubernetes/test/e2e/framework/testfiles",
261+
"k8s.io/kubernetes/test/e2e/framework/websocket",
261262
"k8s.io/kubernetes/test/e2e/manifest",
262263
"k8s.io/kubernetes/test/e2e/perftype",
263264
"k8s.io/kubernetes/test/e2e/storage/utils",

test/e2e/framework/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ go_library(
7878
"//vendor/github.com/onsi/gomega:go_default_library",
7979
"//vendor/github.com/onsi/gomega/types:go_default_library",
8080
"//vendor/github.com/pkg/errors:go_default_library",
81-
"//vendor/golang.org/x/net/websocket:go_default_library",
8281
"//vendor/k8s.io/klog:go_default_library",
8382
"//vendor/k8s.io/utils/exec:go_default_library",
8483
],
@@ -143,6 +142,7 @@ filegroup(
143142
"//test/e2e/framework/testfiles:all-srcs",
144143
"//test/e2e/framework/timer:all-srcs",
145144
"//test/e2e/framework/volume:all-srcs",
145+
"//test/e2e/framework/websocket:all-srcs",
146146
],
147147
tags = ["automanaged"],
148148
visibility = ["//visibility:public"],

test/e2e/framework/util.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import (
3737
"syscall"
3838
"time"
3939

40-
"golang.org/x/net/websocket"
41-
4240
"github.com/onsi/ginkgo"
4341
"github.com/onsi/gomega"
4442
gomegatypes "github.com/onsi/gomega/types"
@@ -1340,59 +1338,6 @@ func WaitForControllerManagerUp() error {
13401338
return fmt.Errorf("waiting for controller-manager timed out")
13411339
}
13421340

1343-
type extractRT struct {
1344-
http.Header
1345-
}
1346-
1347-
func (rt *extractRT) RoundTrip(req *http.Request) (*http.Response, error) {
1348-
rt.Header = req.Header
1349-
return &http.Response{}, nil
1350-
}
1351-
1352-
// headersForConfig extracts any http client logic necessary for the provided
1353-
// config.
1354-
func headersForConfig(c *restclient.Config, url *url.URL) (http.Header, error) {
1355-
extract := &extractRT{}
1356-
rt, err := restclient.HTTPWrappersForConfig(c, extract)
1357-
if err != nil {
1358-
return nil, err
1359-
}
1360-
request, err := http.NewRequest("GET", url.String(), nil)
1361-
if err != nil {
1362-
return nil, err
1363-
}
1364-
if _, err := rt.RoundTrip(request); err != nil {
1365-
return nil, err
1366-
}
1367-
return extract.Header, nil
1368-
}
1369-
1370-
// OpenWebSocketForURL constructs a websocket connection to the provided URL, using the client
1371-
// config, with the specified protocols.
1372-
func OpenWebSocketForURL(url *url.URL, config *restclient.Config, protocols []string) (*websocket.Conn, error) {
1373-
tlsConfig, err := restclient.TLSConfigFor(config)
1374-
if err != nil {
1375-
return nil, fmt.Errorf("Failed to create tls config: %v", err)
1376-
}
1377-
if url.Scheme == "https" {
1378-
url.Scheme = "wss"
1379-
} else {
1380-
url.Scheme = "ws"
1381-
}
1382-
headers, err := headersForConfig(config, url)
1383-
if err != nil {
1384-
return nil, fmt.Errorf("Failed to load http headers: %v", err)
1385-
}
1386-
cfg, err := websocket.NewConfig(url.String(), "http://localhost")
1387-
if err != nil {
1388-
return nil, fmt.Errorf("Failed to create websocket config: %v", err)
1389-
}
1390-
cfg.Header = headers
1391-
cfg.TlsConfig = tlsConfig
1392-
cfg.Protocol = protocols
1393-
return websocket.DialConfig(cfg)
1394-
}
1395-
13961341
// LookForStringInLog looks for the given string in the log of a specific pod container
13971342
func LookForStringInLog(ns, podName, container, expectedString string, timeout time.Duration) (result string, err error) {
13981343
return lookForString(expectedString, timeout, func() string {

test/e2e/framework/websocket/BUILD

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["websocket_util.go"],
6+
importpath = "k8s.io/kubernetes/test/e2e/framework/websocket",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//staging/src/k8s.io/client-go/rest:go_default_library",
10+
"//vendor/golang.org/x/net/websocket:go_default_library",
11+
],
12+
)
13+
14+
filegroup(
15+
name = "package-srcs",
16+
srcs = glob(["**"]),
17+
tags = ["automanaged"],
18+
visibility = ["//visibility:private"],
19+
)
20+
21+
filegroup(
22+
name = "all-srcs",
23+
srcs = [":package-srcs"],
24+
tags = ["automanaged"],
25+
visibility = ["//visibility:public"],
26+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package websocket
18+
19+
import (
20+
"fmt"
21+
"net/http"
22+
"net/url"
23+
24+
restclient "k8s.io/client-go/rest"
25+
26+
"golang.org/x/net/websocket"
27+
)
28+
29+
type extractRT struct {
30+
http.Header
31+
}
32+
33+
func (rt *extractRT) RoundTrip(req *http.Request) (*http.Response, error) {
34+
rt.Header = req.Header
35+
return &http.Response{}, nil
36+
}
37+
38+
// OpenWebSocketForURL constructs a websocket connection to the provided URL, using the client
39+
// config, with the specified protocols.
40+
func OpenWebSocketForURL(url *url.URL, config *restclient.Config, protocols []string) (*websocket.Conn, error) {
41+
tlsConfig, err := restclient.TLSConfigFor(config)
42+
if err != nil {
43+
return nil, fmt.Errorf("Failed to create tls config: %v", err)
44+
}
45+
if url.Scheme == "https" {
46+
url.Scheme = "wss"
47+
} else {
48+
url.Scheme = "ws"
49+
}
50+
headers, err := headersForConfig(config, url)
51+
if err != nil {
52+
return nil, fmt.Errorf("Failed to load http headers: %v", err)
53+
}
54+
cfg, err := websocket.NewConfig(url.String(), "http://localhost")
55+
if err != nil {
56+
return nil, fmt.Errorf("Failed to create websocket config: %v", err)
57+
}
58+
cfg.Header = headers
59+
cfg.TlsConfig = tlsConfig
60+
cfg.Protocol = protocols
61+
return websocket.DialConfig(cfg)
62+
}
63+
64+
// headersForConfig extracts any http client logic necessary for the provided
65+
// config.
66+
func headersForConfig(c *restclient.Config, url *url.URL) (http.Header, error) {
67+
extract := &extractRT{}
68+
rt, err := restclient.HTTPWrappersForConfig(c, extract)
69+
if err != nil {
70+
return nil, err
71+
}
72+
request, err := http.NewRequest("GET", url.String(), nil)
73+
if err != nil {
74+
return nil, err
75+
}
76+
if _, err := rt.RoundTrip(request); err != nil {
77+
return nil, err
78+
}
79+
return extract.Header, nil
80+
}

test/e2e/kubectl/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ go_library(
3838
"//test/e2e/framework/pod:go_default_library",
3939
"//test/e2e/framework/service:go_default_library",
4040
"//test/e2e/framework/testfiles:go_default_library",
41+
"//test/e2e/framework/websocket:go_default_library",
4142
"//test/e2e/scheduling:go_default_library",
4243
"//test/utils:go_default_library",
4344
"//test/utils/crd:go_default_library",

test/e2e/kubectl/portforward.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"k8s.io/kubernetes/test/e2e/framework"
4040
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
4141
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
42+
e2ewebsocket "k8s.io/kubernetes/test/e2e/framework/websocket"
4243
testutils "k8s.io/kubernetes/test/utils"
4344
imageutils "k8s.io/kubernetes/test/utils/image"
4445

@@ -378,7 +379,7 @@ func doTestOverWebSockets(bindAddress string, f *framework.Framework) {
378379
Param("ports", "80")
379380

380381
url := req.URL()
381-
ws, err := framework.OpenWebSocketForURL(url, config, []string{"v4.channel.k8s.io"})
382+
ws, err := e2ewebsocket.OpenWebSocketForURL(url, config, []string{"v4.channel.k8s.io"})
382383
if err != nil {
383384
framework.Failf("Failed to open websocket to %s: %v", url.String(), err)
384385
}

0 commit comments

Comments
 (0)