Fresh dep ensure

This commit is contained in:
Mike Cronce
2018-11-26 13:23:56 -05:00
parent 93cb8a04d7
commit 407478ab9a
9016 changed files with 551394 additions and 279685 deletions

View File

@ -11,8 +11,8 @@ go_test(
srcs = ["proxy_server_test.go"],
embed = [":go_default_library"],
deps = [
"//vendor/k8s.io/apimachinery/pkg/util/proxy:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/proxy:go_default_library",
"//staging/src/k8s.io/client-go/rest:go_default_library",
],
)
@ -22,11 +22,11 @@ go_library(
importpath = "k8s.io/kubernetes/pkg/kubectl/proxy",
deps = [
"//pkg/kubectl/util:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/proxy:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
"//vendor/k8s.io/client-go/transport:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/proxy:go_default_library",
"//staging/src/k8s.io/client-go/rest:go_default_library",
"//staging/src/k8s.io/client-go/transport:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
)

View File

@ -26,11 +26,11 @@ import (
"strings"
"time"
"github.com/golang/glog"
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/proxy"
"k8s.io/client-go/rest"
"k8s.io/client-go/transport"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/kubectl/util"
)
@ -87,7 +87,7 @@ func MakeRegexpArray(str string) ([]*regexp.Regexp, error) {
func MakeRegexpArrayOrDie(str string) []*regexp.Regexp {
result, err := MakeRegexpArray(str)
if err != nil {
glog.Fatalf("Error compiling re: %v", err)
klog.Fatalf("Error compiling re: %v", err)
}
return result
}
@ -95,7 +95,7 @@ func MakeRegexpArrayOrDie(str string) []*regexp.Regexp {
func matchesRegexp(str string, regexps []*regexp.Regexp) bool {
for _, re := range regexps {
if re.MatchString(str) {
glog.V(6).Infof("%v matched %s", str, re)
klog.V(6).Infof("%v matched %s", str, re)
return true
}
}
@ -135,13 +135,12 @@ func extractHost(header string) (host string) {
func (f *FilterServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
host := extractHost(req.Host)
if f.accept(req.Method, req.URL.Path, host) {
glog.V(3).Infof("Filter accepting %v %v %v", req.Method, req.URL.Path, host)
klog.V(3).Infof("Filter accepting %v %v %v", req.Method, req.URL.Path, host)
f.delegate.ServeHTTP(rw, req)
return
}
glog.V(3).Infof("Filter rejecting %v %v %v", req.Method, req.URL.Path, host)
rw.WriteHeader(http.StatusForbidden)
rw.Write([]byte("<h3>Unauthorized</h3>"))
klog.V(3).Infof("Filter rejecting %v %v %v", req.Method, req.URL.Path, host)
http.Error(rw, http.StatusText(http.StatusForbidden), http.StatusForbidden)
}
// Server is a http.Handler which proxies Kubernetes APIs to remote API server.
@ -152,13 +151,13 @@ type Server struct {
type responder struct{}
func (r *responder) Error(w http.ResponseWriter, req *http.Request, err error) {
glog.Errorf("Error while proxying request: %v", err)
klog.Errorf("Error while proxying request: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// makeUpgradeTransport creates a transport that explicitly bypasses HTTP2 support
// for proxy connections that must upgrade.
func makeUpgradeTransport(config *rest.Config) (proxy.UpgradeRequestRoundTripper, error) {
func makeUpgradeTransport(config *rest.Config, keepalive time.Duration) (proxy.UpgradeRequestRoundTripper, error) {
transportConfig, err := config.TransportConfig()
if err != nil {
return nil, err
@ -169,7 +168,12 @@ func makeUpgradeTransport(config *rest.Config) (proxy.UpgradeRequestRoundTripper
}
rt := utilnet.SetOldTransportDefaults(&http.Transport{
TLSClientConfig: tlsConfig,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: keepalive,
}).DialContext,
})
upgrader, err := transport.HTTPWrappersForConfig(transportConfig, proxy.MirrorRequest)
if err != nil {
return nil, err
@ -179,7 +183,7 @@ func makeUpgradeTransport(config *rest.Config) (proxy.UpgradeRequestRoundTripper
// NewServer creates and installs a new Server.
// 'filter', if non-nil, protects requests to the api only.
func NewServer(filebase string, apiProxyPrefix string, staticPrefix string, filter *FilterServer, cfg *rest.Config) (*Server, error) {
func NewServer(filebase string, apiProxyPrefix string, staticPrefix string, filter *FilterServer, cfg *rest.Config, keepalive time.Duration) (*Server, error) {
host := cfg.Host
if !strings.HasSuffix(host, "/") {
host = host + "/"
@ -194,7 +198,7 @@ func NewServer(filebase string, apiProxyPrefix string, staticPrefix string, filt
if err != nil {
return nil, err
}
upgradeTransport, err := makeUpgradeTransport(cfg)
upgradeTransport, err := makeUpgradeTransport(cfg, keepalive)
if err != nil {
return nil, err
}
@ -252,18 +256,6 @@ func newFileHandler(prefix, base string) http.Handler {
return http.StripPrefix(prefix, http.FileServer(http.Dir(base)))
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
// like http.StripPrefix, but always leaves an initial slash. (so that our
// regexps will work.)
func stripLeaveSlash(prefix string, h http.Handler) http.Handler {

View File

@ -33,6 +33,7 @@ import (
func TestAccept(t *testing.T) {
tests := []struct {
name string
acceptPaths string
rejectPaths string
acceptHosts string
@ -44,6 +45,7 @@ func TestAccept(t *testing.T) {
}{
{
name: "test1",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -54,6 +56,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test2",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -64,6 +67,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test3",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -74,6 +78,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test4",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -84,6 +89,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test5",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -94,6 +100,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test7",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -104,6 +111,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test8",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -114,6 +122,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test9",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -124,6 +133,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test10",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -134,6 +144,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test11",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -144,6 +155,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test12",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -154,6 +166,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test13",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -164,6 +177,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test14",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -174,6 +188,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test15",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -184,6 +199,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test16",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -194,6 +210,7 @@ func TestAccept(t *testing.T) {
expectAccept: true,
},
{
name: "test17",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -204,6 +221,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test18",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -214,6 +232,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test19",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -224,6 +243,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test20",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -234,6 +254,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test21",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -244,6 +265,7 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
{
name: "test22",
acceptPaths: DefaultPathAcceptRE,
rejectPaths: DefaultPathRejectRE,
acceptHosts: DefaultHostAcceptRE,
@ -254,52 +276,61 @@ func TestAccept(t *testing.T) {
expectAccept: false,
},
}
for _, test := range tests {
filter := &FilterServer{
AcceptPaths: MakeRegexpArrayOrDie(test.acceptPaths),
RejectPaths: MakeRegexpArrayOrDie(test.rejectPaths),
AcceptHosts: MakeRegexpArrayOrDie(test.acceptHosts),
RejectMethods: MakeRegexpArrayOrDie(test.rejectMethods),
}
accept := filter.accept(test.method, test.path, test.host)
if accept != test.expectAccept {
t.Errorf("expected: %v, got %v for %#v", test.expectAccept, accept, test)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filter := &FilterServer{
AcceptPaths: MakeRegexpArrayOrDie(tt.acceptPaths),
RejectPaths: MakeRegexpArrayOrDie(tt.rejectPaths),
AcceptHosts: MakeRegexpArrayOrDie(tt.acceptHosts),
RejectMethods: MakeRegexpArrayOrDie(tt.rejectMethods),
}
accept := filter.accept(tt.method, tt.path, tt.host)
if accept != tt.expectAccept {
t.Errorf("expected: %v, got %v for %#v", tt.expectAccept, accept, tt)
}
})
}
}
func TestRegexpMatch(t *testing.T) {
tests := []struct {
name string
str string
regexps string
expectMatch bool
}{
{
name: "test1",
str: "foo",
regexps: "bar,.*",
expectMatch: true,
},
{
name: "test2",
str: "foo",
regexps: "bar,fo.*",
expectMatch: true,
},
{
name: "test3",
str: "bar",
regexps: "bar,fo.*",
expectMatch: true,
},
{
name: "test4",
str: "baz",
regexps: "bar,fo.*",
expectMatch: false,
},
}
for _, test := range tests {
match := matchesRegexp(test.str, MakeRegexpArrayOrDie(test.regexps))
if test.expectMatch != match {
t.Errorf("expected: %v, found: %v, for %s and %v", test.expectMatch, match, test.str, test.regexps)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
match := matchesRegexp(tt.str, MakeRegexpArrayOrDie(tt.regexps))
if tt.expectMatch != match {
t.Errorf("expected: %v, found: %v, for %s and %v", tt.expectMatch, match, tt.str, tt.regexps)
}
})
}
}
@ -363,29 +394,31 @@ func TestAPIRequests(t *testing.T) {
target.Path = "/"
proxy := newProxy(target)
tests := []struct{ method, body string }{
{"GET", ""},
{"DELETE", ""},
{"POST", "test payload"},
{"PUT", "test payload"},
tests := []struct{ name, method, body string }{
{"test1", "GET", ""},
{"test2", "DELETE", ""},
{"test3", "POST", "test payload"},
{"test4", "PUT", "test payload"},
}
const path = "/api/test?fields=ID%3Dfoo&labels=key%3Dvalue"
for i, tt := range tests {
r, err := http.NewRequest(tt.method, path, strings.NewReader(tt.body))
if err != nil {
t.Errorf("error creating request: %v", err)
continue
}
w := httptest.NewRecorder()
proxy.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("%d: proxy.ServeHTTP w.Code = %d; want %d", i, w.Code, http.StatusOK)
}
want := strings.Join([]string{tt.method, path, tt.body}, " ")
if w.Body.String() != want {
t.Errorf("%d: response body = %q; want %q", i, w.Body.String(), want)
}
t.Run(tt.name, func(t *testing.T) {
r, err := http.NewRequest(tt.method, path, strings.NewReader(tt.body))
if err != nil {
t.Errorf("error creating request: %v", err)
return
}
w := httptest.NewRecorder()
proxy.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("%d: proxy.ServeHTTP w.Code = %d; want %d", i, w.Code, http.StatusOK)
}
want := strings.Join([]string{tt.method, path, tt.body}, " ")
if w.Body.String() != want {
t.Errorf("%d: response body = %q; want %q", i, w.Body.String(), want)
}
})
}
}
@ -396,48 +429,49 @@ func TestPathHandling(t *testing.T) {
defer ts.Close()
table := []struct {
name string
prefix string
reqPath string
expectPath string
}{
{"/api/", "/metrics", "404 page not found\n"},
{"/api/", "/api/metrics", "/api/metrics"},
{"/api/", "/api/v1/pods/", "/api/v1/pods/"},
{"/", "/metrics", "/metrics"},
{"/", "/api/v1/pods/", "/api/v1/pods/"},
{"/custom/", "/metrics", "404 page not found\n"},
{"/custom/", "/api/metrics", "404 page not found\n"},
{"/custom/", "/api/v1/pods/", "404 page not found\n"},
{"/custom/", "/custom/api/metrics", "/api/metrics"},
{"/custom/", "/custom/api/v1/pods/", "/api/v1/pods/"},
{"test1", "/api/", "/metrics", "404 page not found\n"},
{"test2", "/api/", "/api/metrics", "/api/metrics"},
{"test3", "/api/", "/api/v1/pods/", "/api/v1/pods/"},
{"test4", "/", "/metrics", "/metrics"},
{"test5", "/", "/api/v1/pods/", "/api/v1/pods/"},
{"test6", "/custom/", "/metrics", "404 page not found\n"},
{"test7", "/custom/", "/api/metrics", "404 page not found\n"},
{"test8", "/custom/", "/api/v1/pods/", "404 page not found\n"},
{"test9", "/custom/", "/custom/api/metrics", "/api/metrics"},
{"test10", "/custom/", "/custom/api/v1/pods/", "/api/v1/pods/"},
}
cc := &rest.Config{
Host: ts.URL,
}
for _, item := range table {
func() {
p, err := NewServer("", item.prefix, "/not/used/for/this/test", nil, cc)
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
p, err := NewServer("", tt.prefix, "/not/used/for/this/test", nil, cc, 0)
if err != nil {
t.Fatalf("%#v: %v", item, err)
t.Fatalf("%#v: %v", tt, err)
}
pts := httptest.NewServer(p.handler)
defer pts.Close()
r, err := http.Get(pts.URL + item.reqPath)
r, err := http.Get(pts.URL + tt.reqPath)
if err != nil {
t.Fatalf("%#v: %v", item, err)
t.Fatalf("%#v: %v", tt, err)
}
body, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
t.Fatalf("%#v: %v", item, err)
t.Fatalf("%#v: %v", tt, err)
}
if e, a := item.expectPath, string(body); e != a {
t.Errorf("%#v: Wanted %q, got %q", item, e, a)
if e, a := tt.expectPath, string(body); e != a {
t.Errorf("%#v: Wanted %q, got %q", tt, e, a)
}
}()
})
}
}