test/e2e/framework:move OpenWebSocketForURL to subpackage(new) websocket

This commit is contained in:
tanjunchen 2020-03-24 22:54:35 +08:00
parent c441a1a7dc
commit aa9d06d79b
9 changed files with 115 additions and 59 deletions

View File

@ -78,6 +78,7 @@ go_library(
"//test/e2e/framework/rc:go_default_library",
"//test/e2e/framework/skipper:go_default_library",
"//test/e2e/framework/volume:go_default_library",
"//test/e2e/framework/websocket:go_default_library",
"//test/utils:go_default_library",
"//test/utils/image:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",

View File

@ -44,6 +44,7 @@ import (
"k8s.io/kubernetes/test/e2e/framework"
e2ekubelet "k8s.io/kubernetes/test/e2e/framework/kubelet"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
e2ewebsocket "k8s.io/kubernetes/test/e2e/framework/websocket"
imageutils "k8s.io/kubernetes/test/utils/image"
"github.com/onsi/ginkgo"
@ -588,7 +589,7 @@ var _ = framework.KubeDescribe("Pods", func() {
Param("command", "remote execution test")
url := req.URL()
ws, err := framework.OpenWebSocketForURL(url, config, []string{"channel.k8s.io"})
ws, err := e2ewebsocket.OpenWebSocketForURL(url, config, []string{"channel.k8s.io"})
if err != nil {
framework.Failf("Failed to open websocket to %s: %v", url.String(), err)
}
@ -667,7 +668,7 @@ var _ = framework.KubeDescribe("Pods", func() {
url := req.URL()
ws, err := framework.OpenWebSocketForURL(url, config, []string{"binary.k8s.io"})
ws, err := e2ewebsocket.OpenWebSocketForURL(url, config, []string{"binary.k8s.io"})
if err != nil {
framework.Failf("Failed to open websocket to %s: %v", url.String(), err)
}

View File

@ -258,6 +258,7 @@
"k8s.io/kubernetes/test/e2e/framework/service",
"k8s.io/kubernetes/test/e2e/framework/ssh",
"k8s.io/kubernetes/test/e2e/framework/testfiles",
"k8s.io/kubernetes/test/e2e/framework/websocket",
"k8s.io/kubernetes/test/e2e/manifest",
"k8s.io/kubernetes/test/e2e/perftype",
"k8s.io/kubernetes/test/e2e/storage/utils",

View File

@ -78,7 +78,6 @@ go_library(
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/github.com/onsi/gomega/types:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/golang.org/x/net/websocket:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/k8s.io/utils/exec:go_default_library",
],
@ -143,6 +142,7 @@ filegroup(
"//test/e2e/framework/testfiles:all-srcs",
"//test/e2e/framework/timer:all-srcs",
"//test/e2e/framework/volume:all-srcs",
"//test/e2e/framework/websocket:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],

View File

@ -37,8 +37,6 @@ import (
"syscall"
"time"
"golang.org/x/net/websocket"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
@ -1340,59 +1338,6 @@ func WaitForControllerManagerUp() error {
return fmt.Errorf("waiting for controller-manager timed out")
}
type extractRT struct {
http.Header
}
func (rt *extractRT) RoundTrip(req *http.Request) (*http.Response, error) {
rt.Header = req.Header
return &http.Response{}, nil
}
// headersForConfig extracts any http client logic necessary for the provided
// config.
func headersForConfig(c *restclient.Config, url *url.URL) (http.Header, error) {
extract := &extractRT{}
rt, err := restclient.HTTPWrappersForConfig(c, extract)
if err != nil {
return nil, err
}
request, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
if _, err := rt.RoundTrip(request); err != nil {
return nil, err
}
return extract.Header, nil
}
// OpenWebSocketForURL constructs a websocket connection to the provided URL, using the client
// config, with the specified protocols.
func OpenWebSocketForURL(url *url.URL, config *restclient.Config, protocols []string) (*websocket.Conn, error) {
tlsConfig, err := restclient.TLSConfigFor(config)
if err != nil {
return nil, fmt.Errorf("Failed to create tls config: %v", err)
}
if url.Scheme == "https" {
url.Scheme = "wss"
} else {
url.Scheme = "ws"
}
headers, err := headersForConfig(config, url)
if err != nil {
return nil, fmt.Errorf("Failed to load http headers: %v", err)
}
cfg, err := websocket.NewConfig(url.String(), "http://localhost")
if err != nil {
return nil, fmt.Errorf("Failed to create websocket config: %v", err)
}
cfg.Header = headers
cfg.TlsConfig = tlsConfig
cfg.Protocol = protocols
return websocket.DialConfig(cfg)
}
// LookForStringInLog looks for the given string in the log of a specific pod container
func LookForStringInLog(ns, podName, container, expectedString string, timeout time.Duration) (result string, err error) {
return lookForString(expectedString, timeout, func() string {

View File

@ -0,0 +1,26 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["websocket_util.go"],
importpath = "k8s.io/kubernetes/test/e2e/framework/websocket",
visibility = ["//visibility:public"],
deps = [
"//staging/src/k8s.io/client-go/rest:go_default_library",
"//vendor/golang.org/x/net/websocket:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,80 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package websocket
import (
"fmt"
"net/http"
"net/url"
restclient "k8s.io/client-go/rest"
"golang.org/x/net/websocket"
)
type extractRT struct {
http.Header
}
func (rt *extractRT) RoundTrip(req *http.Request) (*http.Response, error) {
rt.Header = req.Header
return &http.Response{}, nil
}
// OpenWebSocketForURL constructs a websocket connection to the provided URL, using the client
// config, with the specified protocols.
func OpenWebSocketForURL(url *url.URL, config *restclient.Config, protocols []string) (*websocket.Conn, error) {
tlsConfig, err := restclient.TLSConfigFor(config)
if err != nil {
return nil, fmt.Errorf("Failed to create tls config: %v", err)
}
if url.Scheme == "https" {
url.Scheme = "wss"
} else {
url.Scheme = "ws"
}
headers, err := headersForConfig(config, url)
if err != nil {
return nil, fmt.Errorf("Failed to load http headers: %v", err)
}
cfg, err := websocket.NewConfig(url.String(), "http://localhost")
if err != nil {
return nil, fmt.Errorf("Failed to create websocket config: %v", err)
}
cfg.Header = headers
cfg.TlsConfig = tlsConfig
cfg.Protocol = protocols
return websocket.DialConfig(cfg)
}
// headersForConfig extracts any http client logic necessary for the provided
// config.
func headersForConfig(c *restclient.Config, url *url.URL) (http.Header, error) {
extract := &extractRT{}
rt, err := restclient.HTTPWrappersForConfig(c, extract)
if err != nil {
return nil, err
}
request, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
if _, err := rt.RoundTrip(request); err != nil {
return nil, err
}
return extract.Header, nil
}

View File

@ -38,6 +38,7 @@ go_library(
"//test/e2e/framework/pod:go_default_library",
"//test/e2e/framework/service:go_default_library",
"//test/e2e/framework/testfiles:go_default_library",
"//test/e2e/framework/websocket:go_default_library",
"//test/e2e/scheduling:go_default_library",
"//test/utils:go_default_library",
"//test/utils/crd:go_default_library",

View File

@ -39,6 +39,7 @@ import (
"k8s.io/kubernetes/test/e2e/framework"
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
e2ewebsocket "k8s.io/kubernetes/test/e2e/framework/websocket"
testutils "k8s.io/kubernetes/test/utils"
imageutils "k8s.io/kubernetes/test/utils/image"
@ -378,7 +379,7 @@ func doTestOverWebSockets(bindAddress string, f *framework.Framework) {
Param("ports", "80")
url := req.URL()
ws, err := framework.OpenWebSocketForURL(url, config, []string{"v4.channel.k8s.io"})
ws, err := e2ewebsocket.OpenWebSocketForURL(url, config, []string{"v4.channel.k8s.io"})
if err != nil {
framework.Failf("Failed to open websocket to %s: %v", url.String(), err)
}